-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
96 lines (81 loc) · 2.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var request = require("request");
var DOMParser = require('xmldom').DOMParser;
//listen to port 3000
http.listen(3000);
/**
* On Connecction user switches into channelID room
* If room isnt already created -> call sendToRoom to start pushing channel data into room
*/
io.on('connection', function(socket){
console.log('User connected');
socket.on('room', function(room) {
console.log('User switched to ' + room);
socket.leave(socket.room);
if(!(io.sockets.adapter.rooms.hasOwnProperty(room))) {
sendToRoom(room);
}
socket.join(room);
socket.room = room;
});
socket.on('disconnect', function(){
console.log('User disconnected');
});
});
/**
* Push Channel Statistics into Channel Room with a specified interval
* @param room -> roomID (Channel ID) user switched to
*/
function sendToRoom(room) {
setInterval(function () {
getChannelStats(room, function(channelStats) {
io.to(room).emit(room, channelStats);
});
}, 800);
}
/**
* Get Channel Statistics
* Downloads raw html content of channel page and extracts views and subcounts
* cleans raw content and puts it back in an object
* @param channelID -> roomID which is Channel ID or Youtube Channel
* @return callback -> object with channel statistics
*/
function getChannelStats(channelID, callback) {
var channelStatistics = {subcount: '', viewcount: '', cid: channelID};
request({uri: "http://youtube.com/channel/" + channelID}, function(error, response, body) {
if(body) {
var subArr = body.match(/<span class=\"yt-subscription-button-subscriber-count-branded-horizontal(.*?)\">(.*?)<\/span>/s) || [null];
if(!(subArr[2] === undefined)) {
channelStatistics.subcount = cleaner(subArr[2]);
}
request({uri: "http://youtube.com/channel/" + channelID + "/about"}, function(error, response, body) {
if(body) {
var viewArr = body.match(/<b>(.*?)<\/b> views/) || [null];
if(!(viewArr[0] === undefined)) {
var views = viewArr[0].replace(' views','');
channelStatistics.viewcount = cleaner(extractContent(views));
}
if(!((subArr[2] === undefined) || (viewArr[0] === undefined))) {
callback(channelStatistics);
}
}
});
}
});
};
/**
* Extract Value of p/span Element
* @param html -> HTML p/span Elemnt
*/
function extractContent(html) {
return (new DOMParser()).parseFromString(html, 'text/xml') . documentElement . firstChild . data;
}
/**
* String to Raw Number
* @param val -> value like 999,999,999 as string
*/
function cleaner(val) {
return val.replace(/\D/g,'');
}