From 4827df7ac6cc2b6cbc55894f95509c2c6a26c095 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Fri, 13 Dec 2019 18:28:31 +0530 Subject: [PATCH] Add option to play music only Signed-off-by: Joseph Nuthalapati --- README.md | 10 +++++++++- clitube.nim | 27 +++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b851c09..d0fdffa 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ only the standard library. - [x] Search for videos using keywords - [x] Stream videos -- [ ] Stream music +- [x] Stream music - [ ] Download videos - [ ] Configuration options - [ ] Choice of video players @@ -47,6 +47,14 @@ clitube emacs clitube 'nim lang' ``` +### Commandline arguments + +|----------------|---------------------------| +| ***Arguments** | **Explanation** | +|----------------|---------------------------| +| -m, --music | Play Music only, no video | +|----------------|---------------------------| + ## Development One-liner for compiling and running diff --git a/clitube.nim b/clitube.nim index 0c21a94..3714c13 100644 --- a/clitube.nim +++ b/clitube.nim @@ -1,6 +1,6 @@ import htmlparser import httpClient -import os +import parseopt import osproc import sequtils, sugar import strformat @@ -38,14 +38,29 @@ proc presentVideoOptions(searchResults: seq[SearchResult]) = for index, (title, url) in searchResults: styledEcho $index, ". ", styleBright, fgMagenta, title, "\n", resetStyle, fgCyan, url, "\n" -let input = paramStr(1) let player = selectMediaPlayer() -if "https://www.youtube.com" in input: - discard execProcess(&"{player} {input}") +var searchQuery = "" +var musicOnly = false + +for kind, key, value in getopt(): + case kind + of cmdArgument: + searchQuery = key + of cmdShortOption, cmdLongOption: + case key + of "m", "music": musicOnly = true + of cmdEnd: + discard + +let noVideo = if musicOnly: "--no-video" else: "" + +if "https://www.youtube.com" in searchQuery: + discard execProcess(&"{player} {searchQuery}") quit(0) -let searchResults = extractTitlesAndUrls(getYoutubePage(input)) + +let searchResults = extractTitlesAndUrls(getYoutubePage(searchQuery)) presentVideoOptions(searchResults) @@ -55,4 +70,4 @@ let number = parseInt(readLine(stdin)) styledEcho "\n", fgGreen, "Playing ", styleBright, fgMagenta, searchResults[number].title # Play the video using the preferred/available media player -discard execProcess(&"{player} {searchResults[number].url}") +discard execProcess(&"{player} {noVideo} {searchResults[number].url}") -- 2.43.0