<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>HWPS! &#187; Mechanize</title>
	<atom:link href="http://holy.enyou.org/tag/mechanize/feed/" rel="self" type="application/rss+xml" />
	<link>http://holy.enyou.org</link>
	<description>HoLY's Web Press Site</description>
	<lastBuildDate>Fri, 28 Jan 2011 04:19:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>三国志大戦:セレクション動画ダウンロードスクリプト</title>
		<link>http://holy.enyou.org/2009/05/31/sangokushi-taisen-enbujyo-selection-downloader/</link>
		<comments>http://holy.enyou.org/2009/05/31/sangokushi-taisen-enbujyo-selection-downloader/#comments</comments>
		<pubDate>Sat, 30 May 2009 22:02:22 +0000</pubDate>
		<dc:creator>HoLY</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[三国志大戦]]></category>
		<category><![CDATA[Mechanize]]></category>

		<guid isPermaLink="false">http://holy.enyou.org/2009/05/31/sangokushi-taisen-enbujyo-selection-downloader/</guid>
		<description><![CDATA[長期旅行の時も拾っておきたいので作った。

注意点。


使いかたについての質問は一切受けつけません。そのようなコメントは削除します。
使った結果について何が起ころうと責任は一切負いません。


そのうち 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 &#60; RuntimeError; end

  def initialize(agent = nil)
    @agent = agent 
    unless @agent
      [...]]]></description>
			<content:encoded><![CDATA[<p>長期旅行の時も拾っておきたいので作った。</p>

<p>注意点。</p>

<ul>
<li>使いかたについての質問は一切受けつけません。そのようなコメントは削除します。</li>
<li>使った結果について何が起ころうと責任は一切負いません。</li>
</ul>

<p>そのうち GitHub か coderepos あたりに投げます。</p>

<p><span id="more-304"></span></p>

<pre><code>#!/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 &lt; 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 =&gt; '/action.cgi') {|f|
      f.field_with(:name =&gt; 'mail').value = @mail
      f.field_with(:name =&gt; '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}&amp;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
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://holy.enyou.org/2009/05/31/sangokushi-taisen-enbujyo-selection-downloader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

