Created
October 18, 2015 10:36
-
-
Save hakatashi/b213e8533dca5a40eca6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| request = require 'C:\\Users\\hakatashi\\node_modules\\request' | |
| cheerio = require 'C:\\Users\\hakatashi\\node_modules\\cheerio' | |
| async = require 'C:\\Users\\hakatashi\\node_modules\\async' | |
| file = require 'C:\\Users\\hakatashi\\node_modules\\file' | |
| sanitize = require 'C:\\Users\\hakatashi\\node_modules\\sanitize-filename' | |
| mkdirp = require 'C:\\Users\\hakatashi\\node_modules\\mkdirp' | |
| colors = require 'C:\\Users\\hakatashi\\node_modules\\colors' | |
| fs = require 'fs' | |
| path = require 'path' | |
| channels = {} | |
| logme = (text) -> | |
| console.log "[log #{new Date().toLocaleString()}] #{text}" | |
| errorme = (text) -> | |
| console.error "[error #{new Date().toLocaleString()}] #{text}".red | |
| queue = async.queue (data, done) -> | |
| channelName = channels[data.id]?.name | |
| channelType = channels[data.id]?.type | |
| logme "Processing file #{data.file}" | |
| async.series [ | |
| (done) -> | |
| if channelName | |
| done() | |
| else | |
| logme "Fetching #{data.url}" | |
| async.waterfall [ | |
| (done) -> request data.url, done | |
| (response, body, done) -> | |
| logme "Waiting a second..." | |
| setTimeout -> | |
| done null, response, body | |
| , 3000 | |
| (response, body, done) -> | |
| if response.statusCode isnt 200 | |
| return done new Error "status code = #{response.statusCode}" | |
| logme "Successfully fetched #{data.url}" | |
| $ = cheerio.load body | |
| if ($channel = $ 'div.chan > div > div.shosai > a').length > 0 | |
| channelType = 'channel' | |
| channelName = sanitize $channel.eq(0).text() | |
| else if ($channel = $ 'div.com > div > div.shosai > a > span').length > 0 | |
| channelType = 'community' | |
| channelName = sanitize $channel.eq(0).text() | |
| else | |
| channelType = 'channel' | |
| channelName = '公式生放送' | |
| if channelName.length is 0 | |
| return done new Error "channel name is empty" | |
| channels[data.id] = | |
| name: channelName | |
| type: channelType | |
| return done null, ['video', channelType, channelName].join path.sep | |
| (newdir, done) -> mkdirp newdir, (error) -> done error | |
| ], done | |
| (done) -> | |
| newname = ['video', channelType, channelName, data.fileinfo.base].join path.sep | |
| fs.rename data.file, newname, done | |
| # logme "Will rename #{data.file} into #{newname}" | |
| # done() | |
| ], done | |
| file.walk 'Rec', (error, dir, dirs, files) -> | |
| throw error if error | |
| files.forEach (file) -> | |
| fileinfo = path.parse file | |
| if fileinfo.dir.split(path.sep)[-1..][0][0] is '.' | |
| # logme "Skipping file #{file}" | |
| return | |
| if match = fileinfo.name.match /lv\d{8,9}/ | |
| id = match[0] | |
| url = "http://live.nicovideo.jp/watch/#{id}" | |
| queue.push | |
| fileinfo: fileinfo | |
| file: file | |
| url: url | |
| id: id | |
| , (error) -> | |
| if error | |
| errorme "#{id}: #{error.message}" | |
| else | |
| logme "Successfully processed #{id}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment