Changeset 28502

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

YouPorn? のテストを追加

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

Legend:

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

    r28495 r28502  
    1818    class FileNotFound < RuntimeError; end 
    1919    VERSION = '1.0.0' 
    20      
     20 
    2121    MODULES_NAME = %w(age_sage ameba_vision dailymotion moro_tube 
    2222                      nico_video pornhub pornotube red_tube veoh 
    2323                      you_porn you_tube your_file_host) 
    24      
     24 
    2525    @@modules = MODULES_NAME.map do |name| 
    2626      require "www/video_scraper/#{name}" 
    2727      const_get( name.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } ) 
    2828    end 
    29        
     29 
    3030    @@options = { 
    3131      :logger => nil, 
     
    3333      :debug => false, 
    3434    } 
    35      
     35 
    3636    class << self 
    3737      def options 
    3838        @@options 
    3939      end 
    40        
     40 
    4141      def options=(opts) 
    4242        @@options = opts 
     
    8484  end 
    8585end 
    86  
    87  
    88 if $0 == __FILE__ 
    89    #   'http://www.yourfilehost.com/media.php?cat=video&file=XV436__03.wmv', 
    90    #   'http://www.morotube.com/watch.php?clip=46430e1d', 
    91    #   'http://www.pornhub.com/view_video.php?viewkey=35f8c5b464a15c9d3567', 
    92    #   'http://www.redtube.com/8415', 
    93 end 
  • lang/ruby/video_scraper/lib/www/video_scraper/base.rb

    r28496 r28502  
    3333      end 
    3434 
    35       def http_get(url) 
    36         open_opt = { "User-Agent" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)" } 
     35      def http_get(url, opt = nil) 
     36        open_opt = { 
     37          "User-Agent" => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)", 
     38        }.merge( opt || {} ) 
    3739        if @opt[:cache] 
    3840          unless @opt[:cache].respond_to?(:get) and @opt[:cache].respond_to?(:set) 
     
    4042          end 
    4143          @opt[:logger].debug 'use cache.' 
    42           unless content = @opt[:cache].get(url) 
     44          cache_key = "#{url}|#{open_opt}" 
     45          unless content = @opt[:cache].get(cache_key) 
    4346            content = open(url, open_opt) {|fh| fh.read } 
    44             @opt[:cache].set(url, content) 
     47            @opt[:cache].set(cache_key, content) 
    4548          end 
    4649        else 
  • lang/ruby/video_scraper/lib/www/video_scraper/you_porn.rb

    r28495 r28502  
    55module WWW 
    66  module VideoScraper 
    7     class YouPorn 
    8       attr_reader :request_url, :response_body, :page_url, :title, :video_url, :thumb_url 
     7    class YouPorn < Base 
     8      url_regex %r!\Ahttp://youporn\.com/watch/(\d+)! 
    99 
    10       def initialize(opt) 
    11         @opt = opt.is_a?(String) ? { :url => opt } : opt 
    12         @agent = WWW::Mechanize.new 
    13         @agent.user_agent_alias = 'Windows IE 6' 
     10      def initialize(url, opt = nil) 
     11        super 
    1412        do_query 
    15       end 
    16  
    17       def self.valid_url?(url) 
    18         get_mediaid(url) 
    19       end 
    20  
    21       def self.get_mediaid(url) 
    22         url.match(%r!\Ahttp://youporn\.com/watch/(\d+)!)[1] rescue nil 
    2313      end 
    2414 
    2515      private 
    2616      def do_query 
    27         url = @opt[:url] 
    28         raise StandardError, 'url param is requred' unless url 
    29         raise StandardError, "url is not YouPorn link: #{url}" unless YouPorn.valid_url? url 
    30         @page_url = url 
    31         id = YouPorn.get_mediaid(url) 
     17        id = url_regex_match[1] 
    3218 
    33         @request_url = url.sub(/(\?.*)?$/, '?user_choice=Enter') 
    34         page = @agent.get(@request_url) 
    35         @response_body = page.body 
    36         page.root.search('//div[@id="download"]//a').each do |elem| 
     19        request_url = @page_url.sub(/(\?.*)?$/, '?user_choice=Enter') 
     20        html = http_get(request_url, 'Cookie' => 'age_check=1') 
     21        doc = Hpricot(html) 
     22        doc.search('//div[@id="download"]//a').each do |elem| 
    3723          href = elem.attributes['href'] 
    38           @video_url = href if href =~ /\.flv$/ 
     24          (@video_url = href; break) if href =~ %r!^http://download\.youporn\.com/download/.*\.flv! 
    3925        end 
    40         h1 = page.root.at('//div[@id="videoArea"]/h1') 
     26        h1 = doc.at('//div[@id="videoArea"]/h1') 
    4127        @title = h1.inner_html.gsub(/<[^>]*>/, '').strip 
    4228        @thumb_url = h1.at('/img').attributes['src'].sub(/(\d+)_small\.jpg$/, '\1_large.jpg') 
     
    4531  end 
    4632end 
    47  
    48 if $0 == __FILE__ 
    49   w = VideoScraper::YouPorn.new('http://youporn.com/watch/93495?user_choice=Enter') 
    50   puts w.title 
    51   puts w.video_url 
    52   puts w.thumb_url 
    53   puts '--------------------' 
    54   w = VideoScraper::YouPorn.new('http://youporn.com/watch/93495') 
    55   puts w.title 
    56   puts w.video_url 
    57   puts w.thumb_url 
    58 end