Skip to content

Instantly share code, notes, and snippets.

@rainux
Created April 23, 2010 19:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rainux/377056 to your computer and use it in GitHub Desktop.
Save rainux/377056 to your computer and use it in GitHub Desktop.
Grab latest Angel Beats from bbs.sumisora.com automatically

说明

此脚本用于从澄空学园(bbs.sumisora.com)自动下载当天发布的 Angel Beats。运行后将会每隔 10 分钟检查是否有当天发布的种子,直到找到后获取种子并调用外部 BT 工具完成下载。在 Windows 里运行会使用系统默认的 BT 下载工具(关联 .torrent 文件的程序);Linux / Mac OS X 里则是使用 transmission,可以修改脚本最后一行改成你使用的其它工具。

由于澄空学园需要登录才能下载种子附件,因此运行此脚本需要一个澄空学园的账户。如果担心账户安全,你可以使用任何新创建的马甲账户。

为什么这点小事都要写脚本来完成?因为 Angel Beats 是周五夜间播放,澄空周六早晨就会发布中文字幕版本。而我想要周六中午醒来立即就能观看,如此而已。

安装运行环境以及依赖库

只有 Linux / Mac OS X 用户需要用类似这样的命令安装 Ruby 解释器以及依赖库:

sudo aptitude install ruby-full rubygems
sudo gem install httpclient hpricot

使用方法

这是一个命令行脚本程序,Linux / Mac OS X 用户需要在终端里输入以下命令执行:

hunt_ab.rb your_sumisora_username your_sumisora_password

Windows 用户需要在 cmd 窗口里输入以下命令执行:

hunt_ab.exe your_sumisora_username your_sumisora_password
#!/usr/bin/env ruby
require 'iconv'
require 'tmpdir'
require 'rubygems'
require 'httpclient'
require 'hpricot'
if ARGV.size < 2
puts <<-TEXT
Usage: #{$0} your_sumisora_username your_sumisora_password
TEXT
exit
end
client = HTTPClient.new(:agent_name => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.9 Safari/533.2')
puts "Logging in with user #{ARGV.first}..."
client.post('http://bbs.sumisora.com/login.php', {:step => 2, :pwuser => ARGV.first, :pwpwd => ARGV.last})
target_link = nil
loop do
puts 'Looking for Angel Beats page posted today ...'
list_page = Hpricot(Iconv.conv('UTF-8', 'GBK', client.get_content('http://bbs.sumisora.com/thread.php?fid=50')))
(list_page/"a[@href^='read']").each do |link|
if link.to_s =~ /Angel Beats.*?第.*?话/
date = Date.parse(link.parent.parent.next_sibling.search('div').text)
if date == Date.today
target_link = link[:href]
end
end
end
break if target_link
puts 'Not found, will retry after 10 mins.'
sleep 600
end
target_page = Iconv.conv('UTF-8', 'GBK', client.get_content("http://bbs.sumisora.com/#{target_link}"))
torrents = target_page.scan(/<a[^>]+href\s*=\s*"([^"]+)"[^>]+>.*?torrent/).flatten
if torrents.empty?
puts 'No torrent link found.'
exit
end
puts 'Downloading torrent file to AngelBeats.torrent...'
torrent_file = File.join(Dir.tmpdir, 'AngelBeats.torrent')
File.open(torrent_file, 'wb') do |f|
client.get_content("http://bbs.sumisora.com/#{torrents.first}") do |chunk|
f.write(chunk)
end
end
puts 'Invoking external torrent downloader...'
if RUBY_PLATFORM.include?('mswin32')
require 'win32/api'
SW_SHOW = 5
ShellExecute = Win32::API.new('ShellExecute', 'LPPPPI', 'L', 'shell32')
ShellExecute.call(0, 'open', torrent_file, nil, nil, SW_SHOW)
else
`transmission "#{torrent_file}"`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment