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