エロ動画

ゆーすけべー日記

いいものを作ってくれました.いい世の中になったものです.でも,いちいちweb経由でダウンロードするのは大変なので,補助的なスクリプトを書きました.もっといい世の中になるといいです.

#!/usr/bin/ruby

require 'optparse'
require 'open-uri'
require 'rubygems'
require 'mechanize'

Signal.trap(:INT, "exit")

class GetYourFile < WWW::Mechanize
  def self.parse(args)
    @@options = { :search_site => 'http://getyourfile.dyndns.tv' }

    opts = OptionParser.new do |opts| 
      opts.banner = "Usage: get_your_file [options]"

      opts.on("-u", "--url str", "source") do |url|
        @@options[:url] = url
      end

      opts.on("-t", "--title str", "title") do |title|
        @@options[:title] = title
      end

      opts.on("-d", "--download-directory dir", "destination") do |dir|
        @@options[:dir] = ::File.expand_path(dir)
      end

      opts.on("-h", "--help", "Show help message") do |v|
        puts opts
        exit
      end
    end

    opts.parse!(args)

    unless @@options[:url] 
      puts "Usage: get_your_file [options]"
      exit
    end

    new
  end

  def search
    get @@options[:search_site]
    search_form = page.forms.first
    search_form.url = @@options[:url]
    submit search_form

    error =  page.at(:p)
    if error
      puts error.inner_text
      exit 
    end

    if page.body =~ /<img\s*src="(.*?)"\s*alt/
      href, title = $1, $1.split('/').last 
      get_file(href, title)
    end

    href = page.links.last.href
    if href
      title = href.split('/').last.split('?').first
      get_file(href, title)
    end
  end

  def get_file(href, title)
    puts "#{href} via HTTP... this might take a while."
    open(href) do |f|
      if @@options[:title]
        title = @@options[:title] + title.match(/(\..*)$/).to_a[1]
      end

      dest = @@options[:dir] ? ::File.join(@@options[:dir], title) : title
      ::File.open(dest, 'w') do |out|
        out.write f.read
      end
    end
  end
end

GetYourFile.parse(ARGV).search

書き終えてから,Mechanize::Fileクラスにsave_asってメソッドがあることに気づきました.