Changeset 28504

Show
Ignore:
Timestamp:
01/16/09 17:11:59 (4 years ago)
Author:
valda
Message:

youtube のテストを追加

Location:
lang/ruby/video_scraper
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/video_scraper/lib/www/video_scraper/you_tube.rb

    r28495 r28504  
    55module WWW 
    66  module VideoScraper 
    7     class YouTube 
    8       @@options ||= {} 
     7    class YouTube < Base 
     8      url_regex %r!\Ahttp://(?:www|jp)\.youtube\.com/watch.*[?&]v=([[:alnum:]]+)! 
    99 
    10       def self.options 
    11         @@options 
    12       end 
    13  
    14       def self.options=(opts) 
    15         @@options = opts 
    16       end 
    17  
    18       def self.configure(&proc) 
    19         raise ArgumentError, 'Block is required.' unless block_given? 
    20         yield @@options 
    21       end 
    22  
    23       attr_reader :request_url, :response_body, :page_url, :title, :video_url, :thumb_url, :embed_tag 
    24  
    25       def initialize(opt) 
    26         @opt = opt.is_a?(String) ? { :url => opt } : opt 
    27         @agent = WWW::Mechanize.new 
    28         @agent.user_agent_alias = 'Windows IE 6' 
    29         @agent.keep_alive = false 
     10      def initialize(url, opt = nil) 
     11        super 
    3012        do_query 
    31       end 
    32  
    33       def self.valid_url?(url) 
    34         get_mediaid(url) 
    35       end 
    36  
    37       def self.get_mediaid(url) 
    38         begin 
    39           uri = URI.parse(url) 
    40         rescue 
    41           return nil 
    42         end 
    43         return nil unless uri.host.match(%r!(?:www|jp)\.youtube\.com!) 
    44         url.match(%r![?&]v=([[:alnum:]]+)!)[1] rescue nil 
    4513      end 
    4614 
    4715      private 
    4816      def login 
    49         page = @agent.get("#{@page_uri.scheme}://#{@page_uri.host}/login") 
     17        uri = URI.parse(@page_url) 
     18        page = agent.get("#{uri.scheme}://#{uri.host}/login") 
    5019        login_form = page.forms.with.name('loginForm').first 
    51         login_form.username = YouTube.options[:mail] 
    52         login_form.password = YouTube.options[:password] 
    53         @agent.submit(login_form) 
     20        login_form.username = @opt[:you_tube_username] 
     21        login_form.password = @opt[:you_tube_password] 
     22        agent.submit(login_form) 
    5423      end 
    5524 
    5625      def pass_verify_age 
    57         page = @agent.get(@page_url) 
     26        uri = URI.parse(@page_url) 
     27        page = agent.get(uri) 
    5828        if page.uri.path =~ /verify_age/ 
    59           # ログインする 
    6029          login 
    61           # 確認フォームを送信 
    62           page = @agent.post(page.uri, 
    63                              'next_url' => "#{@page_uri.path}?#{@page_uri.query}", 
    64                              'action_confirm' => 'Confirm Birth Date') 
     30          page = agent.post(page.uri, 
     31                            'next_url' => "#{uri.path}?#{uri.query}", 
     32                            'action_confirm' => 'Confirm Birth Date') 
    6533        end 
    6634        page 
     
    6836 
    6937      def do_query 
    70         url = @opt[:url] 
    71         raise StandardError, 'url param is requred' unless url 
    72         raise StandardError, "url is not YouTube link: #{url}" unless YouTube.valid_url? url 
    73         @page_url = url 
    74         uri = URI.parse(@page_url) 
    75         @page_uri = uri 
    76  
    7738        page = pass_verify_age 
    78         @response_body = page.body 
    79         @title = page.root.at('//head/title').inner_html rescue '' 
     39        @title = page.root.at('//head/title').inner_html.sub(/^YouTube[\s-]*/, '') rescue '' 
    8040        @embed_tag = page.root.at('//input[@id="embed_code"]').attributes['value'] rescue nil 
    81         uri.path = '/get_video' 
    8241        page.root.search('//script').each do |script| 
    83           if script.inner_html.match(/var\s+swfArgs\s*=\s*([^;]+);/) 
    84             swf_args = JSON::parse($1) 
     42          if m = script.inner_html.match(/var\s+swfArgs\s*=\s*([^;]+);/) 
     43            swf_args = JSON::parse(m[1]) 
     44            uri = URI.parse(@page_url) 
     45            uri.path = '/get_video' 
    8546            uri.query = "video_id=#{swf_args['video_id']}&t=#{swf_args['t']}" 
    8647            @video_url = uri.to_s 
     
    8849          end 
    8950        end 
    90         raise FileNotFound, 'file not found' if @embed_tag.nil? and @video_url.nil? and @thumb_url.nil? 
     51        raise FileNotFound, 'file not found' if @video_url.nil? 
    9152      end 
    9253    end 
    9354  end 
    9455end 
    95  
    96 if $0 == __FILE__ 
    97   require 'yaml' 
    98   y = YAML.load_file(File.join(ENV['HOME'], '.videoscraperrc')) 
    99   VideoScraper::YouTube.configure do |conf| 
    100     conf[:mail] = y['youtube']['mail'] 
    101     conf[:password] = y['youtube']['password'] 
    102   end 
    103  
    104   w = VideoScraper::YouTube.new('http://www.youtube.com/watch?v=OFPnvARUOHI&feature=dir') 
    105   puts w.title 
    106   puts w.video_url 
    107   puts w.thumb_url 
    108   puts w.embed_tag 
    109   puts '---------------' 
    110   w = VideoScraper::YouTube.new('http://www.youtube.com/watch?v=ysdSl5kmzFY&feature=bz303') 
    111   puts w.title 
    112   puts w.video_url 
    113   puts w.thumb_url 
    114   puts w.embed_tag 
    115 end