# 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'
+```
--- /dev/null
+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))))