Advertisement
Guest User

yt.rb

a guest
Dec 16th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.33 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'eat'
  4.  
  5. abort "Usage: yt.rb <search> <max>" if ARGV[0].nil?
  6.  
  7. class String
  8.   { :reset          =>  0,
  9.     :bold           =>  1,
  10.     :dark           =>  2,
  11.     :underline      =>  4,
  12.     :blink          =>  5,
  13.     :negative       =>  7,
  14.     :black          => 30,
  15.     :green          => 32,
  16.     :yellow         => 33,
  17.     :blue           => 34,
  18.     :cyan           => 36,
  19.     :white          => 37,
  20.     :red            => 31,
  21.     :magenta        => 35,
  22.     :bg_black       => 40,
  23.     :bg_red         => 41,
  24.     :bg_green       => 42,
  25.     :bg_brown       => 43,
  26.     :bg_blue        => 44,
  27.     :bg_magenta     => 45,
  28.     :bg_cyan        => 46,
  29.     :bg_gray        => 47
  30.   }.each do |color, value|
  31.     define_method color do
  32.       "\e[#{value}m" + self + "\e[0m"
  33.     end
  34.   end
  35. end
  36.  
  37. error   = false
  38. query   = "https://www.youtube.com/results?q=#{ARGV[0].downcase.strip.gsub(/\s+/m, "+")}&hl=en" rescue (error = true)
  39. max     = ARGV[1].to_i || 15
  40. search  = eat(query)
  41. vinfo   = {}
  42. vinfo_a =
  43. [
  44.   search.scan(/a href="\/watch\?v=(.*?)"/).collect { |i| i.join },
  45.   search.scan(/a href="\/watch\?v=.*?\> - Duration: (.*?)\.\</).collect { |i| i.join },
  46.  search.scan(/a href="\/watch\?v=.*?dir="ltr"\>(.*?)\</).collect { |i| i.join },
  47.   search.scan(/a href="\/user\/(.*?)\"/).collect { |i| i.join },
  48.  search.scan(/a href="\/watch\?v=.*?\/li\>\<li\>(.*?)\s*views\</).collect { |i| i.join },
  49.   search.scan(/a href="\/watch\?v=.*?li\>(.*?)\</).collect { |i| i.join }
  50. ] if !error
  51.  
  52. vinfo_a[0].each_with_index { |video, index|
  53.  vinfo[video] =
  54.  {
  55.    "time"     => vinfo_a[1][index],
  56.    "title"    => vinfo_a[2][index],
  57.    "user"     => vinfo_a[3][index],
  58.    "views"    => vinfo_a[4][index],
  59.    "created"  => vinfo_a[5][index]
  60.  }
  61. }
  62.  
  63. videos = []
  64.  
  65. vinfo.keys[0..(max-1)].each_with_index { |video, i|
  66.  info = vinfo[video]
  67.  videos << video
  68.  print "%s [%s] %s\n└── (%s)(%s views)\n\s\s\s└── https://youtube.com/v/%s \n\n" %
  69.    [
  70.      (-~i).to_s.bg_magenta,
  71.      info["user"].to_s,
  72.      info["title"].to_s.bg_magenta,
  73.      info["time"][-5..-1].to_s.cyan,
  74.      info["views"].to_s.gsub(/\,/, '.').cyan,
  75.      video.cyan
  76.    ]
  77. }
  78.  
  79. print "Select a video (number): "
  80. num = STDIN.gets.chop.to_i
  81. num = (num == 1) ? 0 : (num - 1)
  82. system "mpv https://youtube.com/v/#{videos[num]}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement