Class: Twitchy
- Inherits:
-
Object
- Object
- Twitchy
- Defined in:
- lib/twitchy/twitchy.rb
Instance Attribute Summary collapse
-
#online ⇒ Object
readonly
Returns the value of attribute online.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#streamers ⇒ Object
readonly
Returns the value of attribute streamers.
Instance Method Summary collapse
- #abbrev(str, length = 77) ⇒ Object
- #banner ⇒ Object
- #check_status ⇒ Object
- #clearln ⇒ Object
- #fetch_subs(user) ⇒ Object
- #format_time(seconds) ⇒ Object
- #get_archives ⇒ Object
- #get_choice ⇒ Object
- #get_stream_choice ⇒ Object
- #get_video_choice ⇒ Object
-
#initialize(args) ⇒ Twitchy
constructor
A new instance of Twitchy.
- #launch_stream(streamer, quality) ⇒ Object
- #launch_video(url) ⇒ Object
- #parse_options(args) ⇒ Object
- #puts_stream_info(index, streamer, title, game, qualities: nil, time: nil) ⇒ Object
- #puts_streams(offset: 0) ⇒ Object
Constructor Details
#initialize(args) ⇒ Twitchy
Returns a new instance of Twitchy.
10 11 12 13 14 15 16 17 18 |
# File 'lib/twitchy/twitchy.rb', line 10 def initialize(args) (args) @streamers = args.map{|s| s.dup} @streamers += fetch_subs(@options.user) if @options.user @streamers += TwitchAPI.get_streamers_for_game( @options.game, limit: @options.limit ) if @options.game end |
Instance Attribute Details
#online ⇒ Object (readonly)
Returns the value of attribute online.
8 9 10 |
# File 'lib/twitchy/twitchy.rb', line 8 def online @online end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/twitchy/twitchy.rb', line 8 def @options end |
#streamers ⇒ Object (readonly)
Returns the value of attribute streamers.
8 9 10 |
# File 'lib/twitchy/twitchy.rb', line 8 def streamers @streamers end |
Instance Method Details
#abbrev(str, length = 77) ⇒ Object
142 143 144 145 146 |
# File 'lib/twitchy/twitchy.rb', line 142 def abbrev(str, length=77) short = str.chomp[0...length] short += "..." if str.length > 77 short end |
#banner ⇒ Object
109 110 111 |
# File 'lib/twitchy/twitchy.rb', line 109 def @optparser.help() end |
#check_status ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/twitchy/twitchy.rb', line 81 def check_status puts "Checking streamer status...".bold @online = TwitchAPI.get_stream_status(@streamers) @online.each do |streamer, data| print "Getting quality options for #{streamer} " data.streams = Livestreamer.get_available_streams(streamer) clearln end @online_streamers = @online.keys.sort_by!{|s| @online[s].viewers}.reverse! end |
#clearln ⇒ Object
113 114 115 |
# File 'lib/twitchy/twitchy.rb', line 113 def clearln print "\r\e[K" end |
#fetch_subs(user) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/twitchy/twitchy.rb', line 74 def fetch_subs(user) puts "Fetching subscriptions".bold TwitchAPI.get_follow_data(user).map do |follow| follow.channel.name end.reverse! end |
#format_time(seconds) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/twitchy/twitchy.rb', line 266 def format_time(seconds) s = seconds % 60 minutes = seconds / 60 m = minutes % 60 h = minutes / 60 if h > 0 "%d:%02d:%02d" % [h,m,s] else "%d:%02d" % [m,s] end end |
#get_archives ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/twitchy/twitchy.rb', line 92 def get_archives puts "Requesting archives".bold @archives = [] @archive_count = Hash.new(0) streamers.each do |streamer| print streamer vods = TwitchAPI.get_videos( streamer, limit: @options.limit, highlights: @options.highlights ) @archives += vods @archive_count[streamer] += vods.to_a.size clearln puts streamer.green end end |
#get_choice ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/twitchy/twitchy.rb', line 157 def get_choice if @options.videos get_video_choice else get_stream_choice end end |
#get_stream_choice ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/twitchy/twitchy.rb', line 165 def get_stream_choice prompt = "Select stream number [and quality] to watch: ".bold print prompt loop do response = ($stdin.gets || "").chomp.split response, quality = case response.length when 0 ["", nil] when 1 [response[0], @options.quality] else response[0,2] end break if response.downcase == "q" choice = Integer(response) rescue nil if choice && choice > 0 && choice <= @online_streamers.length streamer = @online_streamers[choice - 1] if @online[streamer].streams.include? quality launch_stream(streamer, quality) else print "Requested quality not available, options are " puts "[#{@online[streamer].streams.join(", ")}]" end else print "Invalid choice, try again" end #print "\r\e[A\e[K\r" + prompt print prompt end end |
#get_video_choice ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/twitchy/twitchy.rb', line 201 def get_video_choice prompt = "Select stream number to watch: ".bold print prompt offset = 0 streamer_offset = Hash.new{0} loop do response = $stdin.gets.chomp break if response.downcase == "q" if response == ":n" or response == ":next" puts "Getting next page".bold streamer_count = @archives.drop(offset).take(@options.limit).group_by{|v| v.channel.name}.map{|k,v| [k, v.size]} offset += @options.limit streamer_count.each do |s, o| streamer_offset[s] += o if streamer_offset[s] + @options.limit > @archive_count[s] @archives += TwitchAPI.get_videos( s, limit: o, offset: streamer_offset[s] ) @archive_count[s] += o end end puts_streams(offset: offset) print prompt next end if response == ":b" or response == ":back" puts "Getting previous page".bold streamer_count = @archives.drop(offset).take(@options.limit).group_by{|v| v.channel.name}.map{|k,v| [k, v.size]} streamer_count.each do |s, o| streamer_offset[s] -= o streamer_offset[s] = 0 if streamer_offset[s] < 0 end offset -= @options.limit offset = 0 if offset < 0 puts_streams(offset: offset) print prompt next end choice = Integer(response) rescue nil if choice && 0 < choice && choice <= 20 stream = @archives[offset + choice - 1] launch_video(stream.url) else print "Invalid choice, try again" end print "\r\e[A\e[K\r" + prompt end end |
#launch_stream(streamer, quality) ⇒ Object
254 255 256 257 258 259 260 |
# File 'lib/twitchy/twitchy.rb', line 254 def launch_stream(streamer, quality) opts = @options.dup opts.quality = quality Livestreamer.start_stream( streamer, @options.player, opts.quality, @options.chat ) end |
#launch_video(url) ⇒ Object
262 263 264 |
# File 'lib/twitchy/twitchy.rb', line 262 def launch_video(url) Livestreamer.start_video(url, @options.player) end |
#parse_options(args) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/twitchy/twitchy.rb', line 20 def (args) @options = OpenStruct.new( player: "vlc --no-video-title-show", quality: "best", chat: false, videos: false, limit: 20, highlights: false, game: nil ) @optparser = OptionParser.new do |opts| opts. = "Usage: twitchy [options] [channel ..]" opts.on("-p", "--player PLAYER", "Set video player") do |p| @options.player = p end opts.on("-c", "--chat", "Open popout chat with the stream") do @options.chat = true end opts.on("-u", "--user USER", "Show subs for a user") do |u| @options.user = u end opts.on("-q", "--quality QUALITY", "Set desired quality") do |q| @options.quality = q end opts.on("-v", "--videos", "List archives instead of streams") do @options.videos = true end opts.on("-l", "--limit LIMIT", Integer, "Number to fetch at once") do |l| @options.limit = l end opts.on("-g", "--game GAME", "Add top streamers for GAME") do |g| @options.game = g end opts.on("--highlights", "Only show highlights when fetching videos") do @options.highlights = true end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end @optparser.parse!(args) end |
#puts_stream_info(index, streamer, title, game, qualities: nil, time: nil) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/twitchy/twitchy.rb', line 141 def puts_stream_info(index, streamer, title, game, qualities: nil, time: nil) def abbrev(str, length=77) short = str.chomp[0...length] short += "..." if str.length > 77 short end title = abbrev(title) puts "#{index.to_s.bold}. #{streamer} - "\ "#{"(#{format_time(time)}) " if time}"\ "'#{title.sub(game, game.underline)}' "\ "#{"playing #{game.underline}" unless title.include? game if game}" puts " [ #{qualities.map{ |q| if q == @options.quality then q.bold else q end }.join(' | ')} ]" if qualities end |
#puts_streams(offset: 0) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/twitchy/twitchy.rb', line 117 def puts_streams(offset: 0) puts puts "Streams available:".bold index = 0 if @options.videos @archives.sort_by!{|v| v.recorded_at}.reverse! @archives.drop(offset).take(@options.limit).each do |video| streamer = video.channel.display_name index += 1 puts_stream_info(index, streamer, video.title.to_s, video.game.to_s, time: video.length) end else @online_streamers.each do |streamer| info = @online[streamer].channel streamer_name = info.display_name index += 1 qualities = @online[streamer].streams qualities = nil if qualities.include? @options.quality puts_stream_info(index, streamer_name, info.status.to_s, info.game.to_s, qualities: qualities) end end end |