]>
Commit | Line | Data |
---|---|---|
d65a1dcf JN |
1 | import |
2 | htmlparser, | |
3 | httpClient, | |
e9f0c7d0 | 4 | os, |
d65a1dcf JN |
5 | osproc, |
6 | sequtils, | |
7 | sugar, | |
8 | strformat, | |
9 | std/[terminal], | |
e9f0c7d0 | 10 | strformat, |
d65a1dcf JN |
11 | strtabs, |
12 | strutils, | |
6f161e0b | 13 | tables, |
d65a1dcf JN |
14 | uri, |
15 | xmltree | |
16 | ||
17 | import config | |
18 | ||
19 | type | |
6f161e0b | 20 | Options* = Table[string, bool] |
e9f0c7d0 | 21 | SearchResult* = tuple[title: string, url: string] |
6f161e0b | 22 | CommandLineOptions* = tuple[searchQuery: string, options: Options] |
e6561dc9 | 23 | SelectionRange* = tuple[begin: int, until: int] |
d65a1dcf | 24 | |
e9f0c7d0 | 25 | # poEchoCmd can be added to options for debugging |
e4a68706 | 26 | let processOptions = {poStdErrToStdOut, poUsePath} |
9e6b8568 | 27 | |
d65a1dcf JN |
28 | proc selectMediaPlayer*(): string = |
29 | let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0) | |
30 | if len(availablePlayers) == 0: | |
31 | stderr.writeLine &"Please install one of the supported media players: {supportedPlayers}" | |
32 | raise newException(OSError, "No supported media player found") | |
33 | else: | |
34 | return availablePlayers[0] | |
35 | ||
36 | proc getYoutubePage*(searchQuery: string): string = | |
37 | let queryParam = encodeUrl(searchQuery) | |
38 | let client = newHttpClient() | |
39 | let response = get(client, &"https://www.youtube.com/results?hl=en&search_query={queryParam}") | |
40 | return $response.body | |
41 | ||
4a0587e2 JN |
42 | func extractTitlesAndUrls*(html: string): seq[SearchResult] = |
43 | {.noSideEffect.}: | |
44 | parseHtml(html).findAll("a"). | |
45 | filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title"). | |
72720bec | 46 | map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"])) |
d65a1dcf JN |
47 | |
48 | proc presentVideoOptions*(searchResults: seq[SearchResult]) = | |
17955bba | 49 | eraseScreen() |
d65a1dcf JN |
50 | for index, (title, url) in searchResults: |
51 | styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n" | |
52 | ||
9e6b8568 | 53 | proc play*(player: string, args: openArray[string], title: string) = |
9e6b8568 | 54 | styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, title |
e71f26f3 JN |
55 | if "--no-video" in args: |
56 | discard execShellCmd(&"{player} {args.join(\" \")}") | |
57 | else: | |
58 | discard execProcess(player, args=args, options=processOptions) | |
d65a1dcf | 59 | |
9a8ef4ad JN |
60 | func buildMusicDownloadArgs*(url: string): seq[string] = |
61 | {.noSideEffect.}: | |
62 | var args = @["--ignore-errors", "-f", "bestaudio", "--extract-audio", "--audio-format", "mp3", "--audio-quality", "0", "-o"] | |
63 | let downloadLocation = &"'{expandTilde(musicDownloadDirectory)}/%(title)s.%(ext)s'" | |
64 | args.add(downloadLocation) | |
65 | args.add(url) | |
66 | return args | |
67 | ||
68 | func buildVideoDownloadArgs*(url: string): seq[string] = | |
69 | {.noSideEffect.}: | |
70 | var args = @["-f", "best", "-o"] | |
71 | let downloadLocation = &"'{expandTilde(videoDownloadDirectory)}/%(title)s.%(ext)s'" | |
72 | args.add(downloadLocation) | |
73 | args.add(url) | |
74 | return args | |
75 | ||
e9f0c7d0 JN |
76 | proc download*(args: openArray[string], title: string) = |
77 | styledEcho "\n", fgGreen, "Downloading ", styleBright, fgMagenta, title | |
78 | discard execShellCmd(&"youtube-dl {args.join(\" \")}") | |
79 | ||
d65a1dcf JN |
80 | func urlLongen(url: string): string = |
81 | url.replace("youtu.be/", "www.youtube.com/watch?v=") | |
82 | ||
83 | func stripZshEscaping(url: string): string = | |
84 | url.replace("\\", "") | |
85 | ||
86 | func sanitizeURL*(url: string): string = | |
87 | urlLongen(stripZshEscaping(url)) | |
88 | ||
811928a7 | 89 | proc directPlay*(url: string, player: string, musicOnly: bool) = |
9a8ef4ad | 90 | if url.startswith("magnet:"): |
811928a7 JN |
91 | if musicOnly: |
92 | discard execShellCmd(&"peerflix '{url}' -a --{player} -- --no-video") | |
93 | else: | |
94 | discard execProcess("peerflix", args=[url, &"--{player}"], options=processOptions) | |
fe1a5856 | 95 | else: |
811928a7 JN |
96 | if musicOnly: |
97 | discard execShellCmd(&"{player} --no-video {url}") | |
98 | else: | |
99 | discard execProcess(player, args=[url], options=processOptions) | |
100 | ||
101 | proc directDownload*(url: string, musicOnly: bool) = | |
102 | let args = | |
103 | if musicOnly: buildMusicDownloadArgs(url) | |
104 | else: buildVideoDownloadArgs(url) | |
9a8ef4ad | 105 | discard execShellCmd(&"youtube-dl {args.join(\" \")}") |