2008-10-19

YourFileHostの動画をなんとかする

YourFileHostのCAPTCHA画像をなんとかするの続きの続き。

まぁ、なんというか、一応できたので張ってみる。微妙な出来栄えだけど。

decaptcha.rbと同じディレクトリに置いて適当に動かしてみてください。

どっかにいいRSSがないかなぁ。。。

しかし、CAPTCHA画像を相手にしてた時の方が楽しかったなぁ。。。

あと、添削とかいろいろ歓迎です。

追記

なぜか&が&に置き換えられてるみたいなので、適当に&に読み替えてください。

download.rb

#!/usr/local/bin/ruby
$LOAD_PATH << File::dirname(File::expand_path($0))
require 'rubygems'
require 'rss'
require 'mechanize'
require 'decaptcha'
# require 'ruby-debug'
# require 'pp'

DESTDIR = 'files'
TMPDIR = '/tmp'

class Downloader
  COOKIE_URI = 'http://www.yourfilehost.com/media.php?cat=video&amp;file=%s'
  DOWNLOAD_URI =
    'http://www.yourfilehost.com/downloadlink.php?cat=video&amp;file=%s&amp;adult=1'
  VERBOSE = false
  DEBUG = false
  
  def self.fetch(file, destdir)
    puts "Fetching file: #{file}"

    destfile = destdir + '/' + file
    if test(?e, destfile) then
      puts '  - Already exists. skip'
      puts
      return nil
    end
  
    #
    # Initialize Mech
    #
    mech = WWW::Mechanize.new
    mech.user_agent_alias = 'Windows IE 6'
    mech.max_history = 3
    mech.open_timeout = 15
    mech.read_timeout = 3
  
    #
    # get cookie
    #
    uri = COOKIE_URI % file
    page = mech.get(uri)
  
    #
    # Challenge against CAPTCHA
    #
    uri = DOWNLOAD_URI % file
    page = mech.get(uri)
  
    for i in 0...3
      captcha_path = (page/'img[@height="38"]').attr(:src)
      captcha_uri = 'http://www.yourfilehost.com/%s' % captcha_path
      gif = mech.get_file(captcha_uri)
      open("#{TMPDIR}/captcha.gif", 'w') {|fd| fd.write(gif) }
      mech.back
  
      code = DeCAPTCHA.decode("#{TMPDIR}/captcha.gif")
      File.unlink("#{TMPDIR}/captcha.gif")
      if code.nil? then
        puts '  - CAPTCHA decode failed. retry' if VERBOSE
        next
      end
  
      form = mech.page.forms.first
      form.verify = code
      page = mech.submit(form)
  
      break unless page.links.empty?
    end
  
    if page.links.empty? then
      puts '  - Failed 3 times.  Try another one.'
      puts
      return nil
    end
  
    #
    # download
    #
    puts "  - Downloading: #{page.links.first.href}" if DEBUG
    retry_count = 0
    video = nil
    begin
      video = page.links.first.click
    rescue Timeout::Error => evar
      retry_count += 1
      if retry_count < 5 then
        puts "  * Timedout, retry" if VERBOSE
        retry
      end
      raise evar
    end

    unless video.instance_of?(WWW::Mechanize::File) then
      if DEBUG then
        puts "  - Something wrong while downloading. skip."
        puts
      end
      return nil
    end
    video.save(destfile)

    return destfile
  end
end



#
# main
#
[DESTDIR, TMPDIR].each do |dir|
  if !test(?d, dir) or !test(?w, dir) then
    puts "#{dir}: Directory not exists or cannot write."
    exit
  end
end

files = []


# collect URIs from RSS
RSS_URI = [
  ['http://www.yourfilehost.com/make-rss.php?range=day&amp;af=off',
    lambda {|rss| rss.items.map {|x| x.link }},
  ],
]
RSS_URI.each do |uri, preprocessor|
  rss = RSS::Parser.parse(uri)
  uris = preprocessor.call(rss) #=> Array of URI of YourFileHost

  uris.map {|x| /file=([^&amp;]*)/.match(x).to_a[1] }.each do |file|
    # next if file !~ /\.wmv/  # uncomment it if you need only wmv
    files << file
  end
end
files.uniq!


# download
files.each do |file|
  begin
    Downloader.fetch(file, DESTDIR)
  rescue SystemCallError, Timeout::Error => evar
    puts "  - error (#{evar.to_s}). skip."
    puts
  end
end
__END__
トラックバック - http://anond.hatelabo.jp/20081019011444
  • YourFileHostのCAPTCHA画像をなんとかする

    破ろうぜ!CAPTCHA画像♪(うっうーん) そんなわけで、みんな大好きなYourFileHostだけども、最近みてみたら、なんかCAPTCHA認証がついているわけじゃないですか。 でもこれってさーCAPTCHAとい...

    • http://anond.hatelabo.jp/20081006220009

      YourFileHostのCAPTCHA画像をなんとかするやつの続き。 その後、適当にいじったら、手元環境で1枚あたり25秒くらい→だいたい2.5秒くらいで判別できるようになった。このくらいなら使えるか...

      • YourFileHostの動画をなんとかする

        YourFileHostのCAPTCHA画像をなんとかするの続きの続き。 まぁ、なんというか、一応できたので張ってみる。微妙な出来栄えだけど。 decaptcha.rbと同じディレクトリに置いて適当に動かしてみて...

      • http://anond.hatelabo.jp/20081017212852

        DIGITS_ASSOCみたいなのをみると激しく圧縮したくなる ぬおおお

    • http://anond.hatelabo.jp/20081006220009

      ruby のソースコードってなんか気持ち悪いな。 なんでだろ。 C系になれてるとかっこがないのが気持ち悪く感じるのかな。

      • http://anond.hatelabo.jp/20081006220301

        perlもこんな感じじゃね?

      • http://anond.hatelabo.jp/20081006220301

        ごめんw perl みたいで気持ち悪い・・・って書くべきだった。w perl も使うことはあるけど気持ち悪くてしょうがないんだよね。 初めて見たときは頭がクラクラした。w

      • http://anond.hatelabo.jp/20081006220301

        シンタックスハイライトがあると、だいぶましなんだけどね。 っていうか貼り付けるときにスーパーpre記法でシンタックスハイライト指定してみたんだけど、日本語部分が文字化けして...