From: Joseph Nuthalapati Date: Sat, 5 Oct 2019 00:19:52 +0000 (+0530) Subject: Implement video search X-Git-Tag: 0.1.0~70 X-Git-Url: https://njoseph.me/gitweb/nimcoon.git/commitdiff_plain/44978125178962c833bed6f53853b74863a93c99 Implement video search Signed-off-by: Joseph Nuthalapati --- diff --git a/README.md b/README.md index de431e3..1d1ed04 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,36 @@ # CLI Tube -Play videos from YouTube from the command line. -This application is currently implemented in Nim language. +Play videos from YouTube (and maybe other sources in the future) from the +command line using your preferred desktop media player. + +This application is implemented in [Nim language](https://nim-lang.org). + +## Features + +- [x] Search for videos using keywords +- [ ] Download or watch videos +- [ ] Configuration options + +## Usage + +### As a script + +You must have nim installed locally for this to work. + +```sh +nim c -d:ssl -r cli_tube.nim emacs + +# If your search query has multiple words, use quotes +nim c -d:ssl -r cli_tube.nim 'nim lang' +``` + +### Using the binary + +Please use the binary in the repository for now. +Will setup GitLab CI to publish proper binaries later. + +```sh +./cli_tube emacs + +./cli_tube 'nim lang' +``` diff --git a/cli_tube.nim b/cli_tube.nim new file mode 100644 index 0000000..d7a3d91 --- /dev/null +++ b/cli_tube.nim @@ -0,0 +1,44 @@ +import htmlparser +import httpClient +import os +import osproc +import strtabs +import strutils +import uri +import xmltree +import sequtils, sugar + +# TODO Pretty colors in terminal +# import terminal + +# Supported video players in order of preference +# TODO Should go into a config file +let supported_players = @["mpv", "mplayer", "vlc"] + +proc find_supported_player(): string = + for player in supported_players: + let player_bin = execProcess("which " & player) + if player_bin.len != 0: + return strip(player_bin) + +proc get_youtube_page(search_query: string): string = + let query_param = encodeUrl(search_query) + var client = newHttpClient() + let response = get(client, "https://www.youtube.com/results?hl=en&search_query=" & query_param) + # TODO Get rid of temp file or make one temp file per user + writeFile("/tmp/cli-tube-page.html", response.body) + return "/tmp/cli-tube-page.html" + +proc extract_titles_and_urls(html_file: string): seq[tuple[title: string, url: string]] = + loadHtml(html_file).findAll("a"). + filter(a => "watch" in a.attrs["href"] and a.attrs.hasKey "title"). + map(a => (a.attrs["title"], "https://www.youtube.com" & a.attrs["href"])) + +proc present_video_options(titles_and_urls: seq[tuple[title: string, url: string]]) = + for number, (title, url) in titles_and_urls: + echo number, ". ", title, "\n", url, "\n" + +present_video_options( + extract_titles_and_urls( + get_youtube_page( + paramStr(1))))