Skip to content

Instantly share code, notes, and snippets.

@smallfield
Last active December 16, 2015 04:59
Show Gist options
  • Save smallfield/7c6e1fe51220de26a743 to your computer and use it in GitHub Desktop.
Save smallfield/7c6e1fe51220de26a743 to your computer and use it in GitHub Desktop.
しょぼいカレンダーの番組IDと話数から、サブタイトルをひっぱってくる奴
#!/usr/local/bin/ruby
# -*- encoding: utf-8 -*-
require "csv"
require "net/http"
require 'sqlite3'
db=SQLite3::Database.new("/tmp/subtitle.db")
sql = <<SQL
create table if not exists subtitle (
tid integer,
number integer,
subtitle varchar(200)
);
SQL
db.execute(sql)
if ARGV.length != 2
puts "usage: animesub <PID> <SUBTITLE_NO>"
exit 1
end
tid = ARGV[0].to_i
number = ARGV[1].to_i
sql = "select subtitle from subtitle where tid = ? and number = ?"
ret = db.execute sql, tid, number
if ret.length > 0
print ret[0][0]
$stdout.flush
exit
end
response = Net::HTTP.get('cal.syoboi.jp', '/db.php?Command=TitleLookup&TID=' + tid.to_s + '&fmt=csv')
response.force_encoding('utf-8')
sql = "insert into subtitle values (?, ?, ?)"
CSV.parse(response, { :headers=>true }) {|row|
row["SubTitles"].each_line {|line|
if line =~ /^\*([0-9]+)\*(.*)$/
db.execute sql, tid, $1.to_i, $2.rstrip.encode
end
}
}
sql = "select subtitle from subtitle where tid = ? and number = ?"
ret = db.execute sql, tid, number
if ret.length > 0
print ret[0][0]
$stdout.flush
end
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment