Skip to content

Instantly share code, notes, and snippets.

@geta6
Created November 25, 2013 11:59
Show Gist options
  • Save geta6/7640263 to your computer and use it in GitHub Desktop.
Save geta6/7640263 to your computer and use it in GitHub Desktop.
#!/usr/bin/env coffee
fs = require 'fs'
AC = require 'async-cache'
path = require 'path'
async = require 'async'
request = require 'request'
cheerio = require 'cheerio'
cached_request = new AC
maxAge: 1000 * 60 * 5
load: (key, callback) ->
request key, (err, head, body) ->
callback err, body
exports.videodetail = videodetail = (target, callback) ->
fields =
tid: null
track: null
disc: null
year: null
title: null
title_ruby: null
series: null
series_ruby: null
artist: null
artist_ruby: null
director: null
director_ruby: null
casts: []
fs.exists target, (exists) ->
return callback new Error "unexists #{target}" unless exists
return callback new Error "not mp4 #{target}" unless '.mp4' is target.slice '-4'
basename = path.basename target
dirname = path.basename path.dirname target
fields.tid = parseInt dirname.replace /^([0-9]+) .*$/, '$1'
return callback new Error "isNaN tid #{target}" if isNaN fields.tid
fields.track = parseInt basename.replace /^([0-9]+) .*$/, '$1'
return callback new Error "isNaN track #{target}" if isNaN fields.track
async.series [
(next) ->
cached_request.get "http://cal.syoboi.jp/tid/#{fields.tid}", (err, body) ->
console.error err if err
$ = cheerio.load body
title = ($ '#main > h1')
disc = ($ '#main > h1 .seriesList')
if 0 < disc.length
disc.find('*').remove()
fields.disc = parseInt disc.text().replace /[^0-9]/g, ''
else
fields.disc = 1
title.find('*').remove()
fields.series = title.text().trim()
fields.series_ruby = title.attr 'title'
basic = ($ '#tidLeft .basic .data tr').map (i, tr) ->
year = no
($ tr).children().each (i, tag) ->
text = ($ tag).text().trim()
switch tag.name
when 'th'
year = /放送期間/.test text
when 'td'
if year
fields.year = parseInt text.replace /.*?([0-9]{4,4}?).*$/, '$1'
staff = ($ '#tidLeft .staff .data tr').map (i, tr) ->
artist = no
director = no
($ tr).children().each (i, tag) ->
text = ($ tag).text().trim()
if tag.name is 'th'
director = fields.director is null and (/^監督$/.test(text) or /^(総*監督)/.test(text) or /([・総]*監督)$/.test(text))
artist = fields.artist is null and (/アニメーション(製|制)作$/.test(text) or /^(企画)*(製|制)作$/.test(text))
if tag.name is 'td'
if director
fields.director = text
if artist
fields.artist = text
staff = ($ '#tidLeft .staff .data tr').map (i, tr) ->
artist = no
director = no
($ tr).children().each (i, tag) ->
text = ($ tag).text().trim()
if tag.name is 'th'
director = fields.director is null and (/脚本$/.test(text) or /^脚本/.test(text))
artist = fields.artist is null and (/^原[作|案]$/.test(text) or /^(原作・|・原作|原案・|・原案)$/.test(text))
if tag.name is 'td'
if director
fields.director = text
if artist
fields.artist = text
casts = ($ '#tidLeft .cast .data tr').map (i, tr) ->
actor = null
($ tr).children().each (i, tag) ->
text = ($ tag).text().trim()
if tag.name is 'th'
actor = text
if tag.name is 'td'
if actor and 0 < actor.length
if -1 is fields.casts.indexOf text
fields.casts.push text
return next err
(next) ->
cached_request.get "http://cal.syoboi.jp/tid/#{fields.tid}/subtitle", (err, body) ->
console.error err if err
$ = cheerio.load body
talk = 0
title = ($ '#tid_subtitle .progs tr').map (i, tr) ->
($ tr).children().each (i, tag) ->
text = ($ tag).text().trim()
switch i
when 0 then talk = parseInt text.trim()
when 1 then fields.title = text if talk is fields.track
return next err
], (err) ->
error = ''
error = "#{error}, tid" if typeof fields.tid isnt 'number'
error = "#{error}, track" if typeof fields.track isnt 'number'
error = "#{error}, disc" if typeof fields.disc isnt 'number'
error = "#{error}, year" if typeof fields.year isnt 'number'
#error = "#{error}, title" if fields.title is null
error = "#{error}, series" if fields.series is null
error = "#{error}, artist" if fields.artist is null
error = "#{error}, director" if fields.director is null
err = new Error error if 0 < error.length
return callback err, fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment