5月 31 2009
三国志大戦:セレクション動画ダウンロードスクリプト
長期旅行の時も拾っておきたいので作った。
注意点。
- 使いかたについての質問は一切受けつけません。そのようなコメントは削除します。
- 使った結果について何が起ころうと責任は一切負いません。
そのうち GitHub か coderepos あたりに投げます。
#!/usr/bin/ruby
# -*- mode:ruby; coding:utf-8 -*-
$KCODE = 'u'
require 'rubygems'
require 'mechanize'
require 'logger'
require 'tempfile'
require 'fileutils'
require 'nkf'
#
# SEGA の三国志大戦公式サイトへアクセスするライブラリ.
#
class Enbujyo
attr_accessor :mail, :password
attr_reader :agent
class LoginError < RuntimeError; end
def initialize(agent = nil)
@agent = agent
unless @agent
@agent = WWW::Mechanize.new
@agent.user_agent_alias = 'Mac Safari'
end
end
def login
page = @agent.get('http://enbujyo.3594t.com/')
@agent.page.form_with(:action => '/action.cgi') {|f|
f.field_with(:name => 'mail').value = @mail
f.field_with(:name => 'password').value = @password
f.click_button
}
raise LoginError if /error/i =~ @agent.page.uri.to_s
end
def download_selection(movie_type = 's')
movie_date = (Time.now - 60*60*17).strftime("%Y%m%d") # 17時で切り替え/JST前提
@agent.get('http://enbujyo.3594t.com/members/selection/index.html')
login if /エラー/ =~ @agent.page.title
tmpname = "_enbujyo_download.#{random_string}.wmv"
open(tmpname, 'w'){|tmp|
tmp.print @agent.get_file("http://download.enbujyo.3594t.com/selection_download.cgi?date=#{movie_date}&type=#{movie_type}")
}
filename = "selection_#{movie_date}.wmv"
if @agent.page.response['content-disposition']
/filename="(.*\.wmv)"/ =~ @agent.page.response['content-disposition']
filename = windows? ? $1 : NKF.nkf('-Sw', $1)
end
FileUtils.mv(tmpname, filename)
puts "Downloading #{filename} has finished."
end
private
def random_string(length = 10, strings = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
Array.new(length).map{ strings[rand(strings.size),1] }.join
end
def windows?
RUBY_PLATFORM.downcase =~ /mswin(?!ce)|mingw|cygwin|bccwin/
end
end
if $0 == __FILE__
enbu = Enbujyo.new
enbu.mail = 'LOGINMAILADDRESS'
enbu.password = 'YOURPASSWORD'
#enbu.agent.log = Logger.new($stdout) # DEBUG
enbu.download_selection
end
