Last active
December 16, 2017 21:55
-
-
Save b-coimbra/034cf417f36ebd332c4d92a4355ad2b1 to your computer and use it in GitHub Desktop.
mpv script - youtube search on the terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'eat' | |
abort "Usage: yt.rb <search> <max>" if ARGV[0].nil? | |
class String | |
{ :reset => 0, | |
:bold => 1, | |
:dark => 2, | |
:underline => 4, | |
:blink => 5, | |
:negative => 7, | |
:black => 30, | |
:green => 32, | |
:yellow => 33, | |
:blue => 34, | |
:cyan => 36, | |
:white => 37, | |
:red => 31, | |
:magenta => 35, | |
:bg_black => 40, | |
:bg_red => 41, | |
:bg_green => 42, | |
:bg_brown => 43, | |
:bg_blue => 44, | |
:bg_magenta => 45, | |
:bg_cyan => 46, | |
:bg_gray => 47 | |
}.each do |color, value| | |
define_method color do | |
"\e[#{value}m" + self + "\e[0m" | |
end | |
end | |
end | |
error = false | |
query = "https://www.youtube.com/results?q=#{ARGV[0].downcase.strip.gsub(/\s+/m, "+")}&hl=en" rescue (error = true) | |
max = ARGV[1].to_i || 15 | |
search = eat(query) | |
vinfo = {} | |
vinfo_a = | |
[ | |
search.scan(/a href="\/watch\?v=(.*?)"/).collect { |i| i.join }, | |
search.scan(/a href="\/watch\?v=.*?\> - Duration: (.*?)\.\</).collect { |i| i.join }, | |
search.scan(/a href="\/watch\?v=.*?dir="ltr"\>(.*?)\</).collect { |i| i.join }, | |
search.scan(/a href="\/user\/(.*?)\"/).collect { |i| i.join }, | |
search.scan(/a href="\/watch\?v=.*?\/li\>\<li\>(.*?)\s*views\</).collect { |i| i.join }, | |
search.scan(/a href="\/watch\?v=.*?li\>(.*?)\</).collect { |i| i.join } | |
] if !error | |
vinfo_a[0].each_with_index { |video, index| | |
vinfo[video] = | |
{ | |
"time" => vinfo_a[1][index], | |
"title" => vinfo_a[2][index], | |
"user" => vinfo_a[3][index], | |
"views" => vinfo_a[4][index], | |
"created" => vinfo_a[5][index] | |
} | |
} | |
videos = [] | |
vinfo.keys[0..(max-1)].each_with_index { |video, i| | |
info = vinfo[video] | |
videos << video | |
print "%s [%s] %s\n└── (%s) ─ (%s views)\n\s\s\s└── https://youtube.com/v/%s \n\n" % | |
[ | |
(-~i).to_s.bg_magenta, | |
info["user"].to_s, | |
info["title"].to_s.bg_magenta, | |
info["time"][-5..-1].to_s.cyan, | |
info["views"].to_s.gsub(/\,/, '.').cyan, | |
video.cyan | |
] | |
} | |
print "Select a video (number): " | |
num = STDIN.gets.chop.to_i | |
num = (num == 1) ? 0 : (num - 1) | |
system "mpv https://youtube.com/v/#{videos[num]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment