-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
77 lines (64 loc) · 2.07 KB
/
app.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
'use strict';
var express = require('express');
var ejs = require('ejs');
var request = require('request');
var fs = require('fs');
var xml2js = require('xml2js');
var bodyParser = require('body-parser');
var app = express();
app.set('port', process.env.PORT || 8080);
app.set('view engine', 'ejs');
app.use(bodyParser.json({limit: '10mb'}));
app.use(bodyParser.urlencoded({limit: '10mb', extended: true}));
app.use('/static', express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('pages/index');
});
app.get('/about', function(req, res) {
res.render('pages/about');
});
app.post('/ajax', function(req, res) {
var url = req.body.url;
var id = url.slice(-11);
url = 'http://video.google.com/timedtext?lang=en&v=' + id;
console.log("Fetching transcript for-" + url);
request(url, function(error, response, html){
if (error) {
console.log(error);
return res.send({"error": "Something went wrong. My developer sucks"});
}
//console.log(response);
if (response.statusCode != 200) {
var error = new Error('Not a valid youtube page - 404 Error');
console.log(error);
return res.send({"error": "I think you entered an invalid youtube URL. Don't mess around with me :P"});
}
//console.log("Html-"+html)
if (html.length == 0) {
console.log("Transcript does not exist");
return res.send({"error":"Sorry, transcript does not exist for this video"});
}
var parser = new xml2js.Parser();
var xml = html;
parser.parseString(xml, function (err, result) {
if (error) {
console.log(error);
return res.send({"error":"Something went wrong. My developer sucks"});
}
var textArray = result.transcript.text;
//console.log(textArray);
var subs = '';
for (var i in textArray) {
var obj = textArray[i];
var subs = subs + '\n' + obj['_'].replace('\\n',' ').replace('--', ' ');
}
res.send({"subs":subs});
});
});
});
app.use(function(req, res){
res.status(404);
res.render('pages/error');
});
app.listen(app.get('port'));
console.log('App started and listening on port: '+ app.get('port') );