Advertisement
Guest User

「ニュースで英会話」ダウンロード

a guest
Apr 20th, 2013
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.46 KB | None | 0 0
  1. #!/usr/bin/env ruby -Ku
  2. # -*- encoding: UTF-8 -*-
  3. $KCODE = 'UTF8'
  4.  
  5. require 'kconv'
  6. require 'open-uri'
  7. require 'rexml/document'
  8. require 'date'
  9.  
  10. $is_windows = RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|cygwin|bccwin/
  11. $script_dir = File.dirname( File.expand_path( __FILE__ ) )
  12. if $is_windows
  13.     $script_dir = $script_dir.kconv( Kconv::UTF8, Kconv::SJIS )
  14.     $null = "nul"
  15. else
  16.     $null = "/dev/null"
  17. end
  18.  
  19. $cgi_uri = 'http://www50.atpages.jp/streamer/enews/enews.cgi'
  20. $flv_prefix = 'rtmp://flv9.nhk.or.jp/flv9/_definst_/e-news/data/'
  21.  
  22. def to_native( string )
  23.     return $is_windows ? string.kconv( Kconv::SJIS, Kconv::UTF8 ) : string
  24. end
  25.  
  26. def main
  27.     open( $cgi_uri ) { |f|
  28.         doc = REXML::Document.new( f )
  29.         prefix = doc.elements["enews/prefix/@video"].to_s
  30.         doc.elements.each( 'enews/flv' ) { |element|
  31.             flv = element.attributes['video']
  32.             out_file = to_native( "ニュースで英会話_#{flv[0, 4]}_#{flv[4, 2]}_#{flv[7, 2]}.flv" )
  33.             out_dir = to_native( File.join( $script_dir, "ニュースで英会話" ) )
  34.             Dir.mkdir( out_dir ) unless File.exist?( out_dir )
  35.             out_dir += File::Separator
  36.             command = "flvstreamer -m 0 -r \"#{prefix}#{flv}\" -o \"#{out_dir}#{out_file}\" > #{$null} 2>&1"
  37.             if File.exist?( out_dir + out_file )
  38.                 puts( 'skip    : ' + out_file )
  39.             else
  40.                 puts( 'download: ' + out_file )
  41.                 system( command )
  42.                 if $?.to_i == 0x7f00
  43.                     puts( 'Failed to execute flvstreamer' )
  44.                     exit
  45.                 end
  46.             end
  47.         }
  48.     }
  49. end
  50.  
  51. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement