Changeset 28502
- Timestamp:
- 01/16/09 17:11:54 (4 years ago)
- Location:
- lang/ruby/video_scraper
- Files:
-
- 1 added
- 3 modified
- 1 moved
-
lib/www/video_scraper.rb (modified) (3 diffs)
-
lib/www/video_scraper/base.rb (modified) (2 diffs)
-
lib/www/video_scraper/you_porn.rb (modified) (2 diffs)
-
test/www/test_video_scraper.rb (moved) (moved from lang/ruby/video_scraper/test/www/video_scraper/test_video_scraper.rb)
-
test/www/video_scraper/test_you_porn.rb (added)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/video_scraper/lib/www/video_scraper.rb
r28495 r28502 18 18 class FileNotFound < RuntimeError; end 19 19 VERSION = '1.0.0' 20 20 21 21 MODULES_NAME = %w(age_sage ameba_vision dailymotion moro_tube 22 22 nico_video pornhub pornotube red_tube veoh 23 23 you_porn you_tube your_file_host) 24 24 25 25 @@modules = MODULES_NAME.map do |name| 26 26 require "www/video_scraper/#{name}" 27 27 const_get( name.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } ) 28 28 end 29 29 30 30 @@options = { 31 31 :logger => nil, … … 33 33 :debug => false, 34 34 } 35 35 36 36 class << self 37 37 def options 38 38 @@options 39 39 end 40 40 41 41 def options=(opts) 42 42 @@options = opts … … 84 84 end 85 85 end 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 33 33 end 34 34 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 || {} ) 37 39 if @opt[:cache] 38 40 unless @opt[:cache].respond_to?(:get) and @opt[:cache].respond_to?(:set) … … 40 42 end 41 43 @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) 43 46 content = open(url, open_opt) {|fh| fh.read } 44 @opt[:cache].set( url, content)47 @opt[:cache].set(cache_key, content) 45 48 end 46 49 else -
lang/ruby/video_scraper/lib/www/video_scraper/you_porn.rb
r28495 r28502 5 5 module WWW 6 6 module VideoScraper 7 class YouPorn 8 attr_reader :request_url, :response_body, :page_url, :title, :video_url, :thumb_url7 class YouPorn < Base 8 url_regex %r!\Ahttp://youporn\.com/watch/(\d+)! 9 9 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 14 12 do_query 15 end16 17 def self.valid_url?(url)18 get_mediaid(url)19 end20 21 def self.get_mediaid(url)22 url.match(%r!\Ahttp://youporn\.com/watch/(\d+)!)[1] rescue nil23 13 end 24 14 25 15 private 26 16 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] 32 18 33 @request_url =url.sub(/(\?.*)?$/, '?user_choice=Enter')34 page = @agent.get(@request_url)35 @response_body = page.body36 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| 37 23 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! 39 25 end 40 h1 = page.root.at('//div[@id="videoArea"]/h1')26 h1 = doc.at('//div[@id="videoArea"]/h1') 41 27 @title = h1.inner_html.gsub(/<[^>]*>/, '').strip 42 28 @thumb_url = h1.at('/img').attributes['src'].sub(/(\d+)_small\.jpg$/, '\1_large.jpg') … … 45 31 end 46 32 end 47 48 if $0 == __FILE__49 w = VideoScraper::YouPorn.new('http://youporn.com/watch/93495?user_choice=Enter')50 puts w.title51 puts w.video_url52 puts w.thumb_url53 puts '--------------------'54 w = VideoScraper::YouPorn.new('http://youporn.com/watch/93495')55 puts w.title56 puts w.video_url57 puts w.thumb_url58 end
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)