Class: Nicovideo
Defined Under Namespace
Classes: ForbiddenError
Instance Method Summary collapse
- #download ⇒ Object
- #dump ⇒ Object
-
#initialize(params, options) ⇒ Nicovideo
constructor
A new instance of Nicovideo.
Methods inherited from WebRadio
Constructor Details
#initialize(params, options) ⇒ Nicovideo
Returns a new instance of Nicovideo.
11 12 13 14 15 16 17 18 19 |
# File 'lib/nicovideo.rb', line 11 def initialize(params, ) account = Pit::get('nicovideo', :require => { :id => 'your nicovideo id', :pass => 'your nicovideo password' }) @nico = Niconico.new(account[:id], account[:pass]) @nico.login super end |
Instance Method Details
#download ⇒ Object
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 |
# File 'lib/nicovideo.rb', line 21 def download offset = 0 begin video = get_video(@url, offset) @cover = thumbinfo(video, 'thumbnail_url') unless @cover title = video.title || thumbinfo(video, 'title') || video.id title.tr!('0-9', '0-9') serial = title.scan(/(?:[#第]|[ ]EP|track-)(\d+)|/).flatten.compact[0].to_i if serial == 0 tmp = title.scan(/\d+/).last.to_i serial = tmp if tmp > 0 end appendix = title =~ /おまけ|アフタートーク/ ? 'a' : '' @file = "#{@label}##{'%02d' % serial}#{appendix}.#{video.type}" @mp3_file = @file.sub(/\....$/, '.mp3') mp3nize(@file, @mp3_file) do loop do print '.' _, err, status = Open3.capture3("youtube-dl -f mp4 -o #{@file} --netrc #{video.url}") break if status == 0 next if err =~ /403: Forbidden/ raise ForbiddenError.new("Could not access to #{video.url}") if err =~ /TypeError/ raise DownloadError.new(err) end end rescue ForbiddenError puts "#{$!.}, try next." offset += 1 retry rescue NotFoundError raise DownloadError.new('video not found') end end |
#dump ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/nicovideo.rb', line 55 def dump begin tag = Pathname(@url).basename.to_s.gsub(%r|[-/]|, '_') rss_url = "#{@url}/video?rss=2.0" desc = RSS::Parser.parse(rss_url).channel.dc_creator return { tag => { 'desc' => desc, 'url' => rss_url, 'label' => tag } } rescue RSS::NotWellFormedError raise end end |