...
 
Commits (2)
import
htmlparser,
httpClient,
logging,
parseopt,
osproc,
sequtils,
......@@ -18,6 +19,9 @@ type
SearchResult = tuple[title: string, url: string]
CommandLineOptions = tuple[searchQuery: string, musicOnly: bool, feelingLucky: bool]
let logger = newConsoleLogger()
setLogFilter(lvlInfo)
proc selectMediaPlayer(): string =
let availablePlayers = filterIt(supportedPlayers, execProcess("which " & it).len != 0)
if len(availablePlayers) == 0:
......@@ -61,21 +65,24 @@ proc presentVideoOptions(searchResults: seq[SearchResult]) =
for index, (title, url) in searchResults:
styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n"
proc play(command: string) =
logger.log(lvlDebug, &"Executing: ${command}")
discard execProcess(command)
quit(0)
proc directPlay(searchQuery: string, player: string) =
styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchQuery
if "watch?" in searchQuery or "videos/watch" in searchQuery :
discard execProcess(&"{player} {searchQuery}")
quit(0)
play(&"{player} {searchQuery}")
elif searchQuery.startswith("magnet:"):
discard execProcess(&"peerflix \"{searchQuery}\" --{player}")
quit(0)
play(&"peerflix \"{searchQuery}\" --{player}")
proc main() =
let
player = selectMediaPlayer()
(searchQuery, musicOnly, feelingLucky) = parseOptions()
directPlay(searchQuery, player)
if searchQuery.startswith("https:") or searchQuery.startswith("magnet:"):
directPlay(searchQuery, player)
let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery))
......@@ -94,6 +101,6 @@ proc main() =
command.add("--no-video")
# Play the video using the preferred/available media player
discard execProcess(command.join(" "))
play(command.join(" "))
main()