Last active
August 29, 2015 14:01
-
-
Save hakatashi/9cf2a20aecfb43202844 to your computer and use it in GitHub Desktop.
This file contains 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
var WebSocket = require('websocket').client; | |
var request = require('request'); | |
var cheerio = require('cheerio'); | |
var stripJsonComments = require('strip-json-comments'); | |
var moment = require('moment-timezone'); | |
var fs = require('fs'); | |
var hostname = 'ws://typhoon.yumetaro.info/websocket'; | |
var ws = new WebSocket(); | |
ws.on('connectFailed', function(error) { | |
console.log(error); | |
}); | |
ws.on('connect', function(connection) { | |
connection.on('message', function(message) { | |
var data = JSON.parse(message.utf8Data); | |
var songs = data.queue; | |
console.log('[' + moment().tz('Asia/Tokyo').format('LLL') + '] Length: ' + songs.length); | |
if (songs.length < 5) { | |
fs.readFile('playlist.json', function(error, data) { | |
var playlist = JSON.parse(stripJsonComments(data.toString())); | |
var id = playlist[Math.floor(Math.random() * playlist.length)]; | |
console.log('[' + moment().tz('Asia/Tokyo').format('LLL') + '] Requesting ' + id); | |
request.post({ | |
url: 'http://typhoon.yumetaro.info/', | |
form: { | |
url: 'http://www.youtube.com/watch?v=' + id | |
} | |
}, function(error, response, body) { | |
if (error) { | |
console.log('[' + moment().tz('Asia/Tokyo').format('LLL') + '] Error: ' + error); | |
return; | |
} | |
$ = cheerio.load(body); | |
var alerts = $('div.main > div.alert').text().replace(/(\r\n|\n|\r)/gm, ""); | |
console.log('[' + moment().tz('Asia/Tokyo').format('LLL') + '] Success(statusCode=' + response.statusCode + '): ' + (alerts ? alerts : 'no alerts')); | |
}); | |
}); | |
}; | |
}); | |
connection.on('close', function() { | |
ws.connect(hostname); | |
}); | |
}); | |
ws.connect(hostname); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment