forked from surperone/BiliGet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
188 lines (156 loc) · 4.97 KB
/
server.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/env node
var http = require('http'),
xml2js = require('xml2js'),
zlib = require('zlib'),
fs = require('fs'),
url = require('url'),
mime = require('./mime').types;
function regexId(string) {
var result = /flashvars="([^"]+)"/.exec(string);
if(!result) {
result = /secure,([^"]+)"/.exec(string);
}
return result ? result[1] : null;
}
function getId(pageUrl, cb) {
var options = url.parse(pageUrl);
try {
http.get(options, function(res) {
var headers = res.headers;
if(headers.location) {
pageUrl = headers.location;
options = url.parse(pageUrl);
http.get(options, function(res) {
var gunzip = zlib.createGunzip();
res.pipe(gunzip);
var html = '';
gunzip.on('data', function(data) {
html += data;
});
gunzip.on('end', function() {
var id = regexId(html);
cb(id);
});
gunzip.on('error', function(err) {
cb();
});
});
} else {
var gunzip = zlib.createGunzip();
res.pipe(gunzip);
var html = '';
gunzip.on('data', function(data) {
html += data;
});
gunzip.on('end', function() {
var id = regexId(html);
cb(id);
});
gunzip.on('error', function(err) {
cb();
});
}
});
} catch(err) {
cb();
}
}
function getURL(id, cb) {
var pageUrl = 'http://interface.bilibili.tv/playurl?' + id;
var options = url.parse(pageUrl);
try {
http.get(options, function(res) {
res.setEncoding('utf8');
var xml_raw = '';
res.on('data', function(data) {
xml_raw += data;
});
res.on('end', function() {
var parser = new xml2js.Parser();
parser.parseString(xml_raw, function(err, result) {
if(err) {
cb(null);
} else {
var list_url = [];
for(var i in result.video.durl) {
list_url.push(result.video.durl[i].url[0]);
}
cb(list_url);
}
});
});
res.on('error', function(err) {
cb();
});
});
} catch(err) {
cb();
}
}
var template = fs.readFileSync('template.html');
var server = new http.Server();
server.on('request', function(req, res) {
var get = url.parse(req.url, true);
if(get.pathname == '/') {
if('url' in get.query) {
get.query.url = get.query.url.trim();
if(get.query.url) {
getId(get.query.url, function(id) {
getURL(id, function(list) {
if(list) {
res.writeHead(200, {
'Content-Type': mime['json']
});
res.end(JSON.stringify(list));
} else {
res.writeHead(404, {
'Content-Type': mime['json']
});
res.end();
}
});
});
} else {
res.writeHead(404, {
'Content-Type': mime['json']
});
res.end();
}
} else {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write(template);
res.end();
}
} else {
var pathStatic = /\/static\/([\w]+).([\w]+)/.exec(get.pathname);
if(pathStatic) {
var file_name = pathStatic[1],
file_type = pathStatic[2];
res.writeHead(200, {
'Content-Type': mime[file_type],
'Cache-Control': 'max-age=604800'
});
fs.readFile('static/' + file_name + '.' + file_type, 'binary', function(err, data) {
if(!err) {
res.write(data, 'binary');
}
res.end();
});
} else {
res.writeHead(404, {
'Content-Type': 'text/html'
});
res.end('Σ( ° △ °|||');
}
}
});
server.listen(80);
process.on('uncaughtException', function(err) {
fs.open('exception.log', 'a', 766, function(e, id) {
fs.write(id, err + '\n', null, 'utf8', function() {
fs.close(id);
});
});
});