-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (93 loc) · 3.12 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var fs = require('fs');
var path = require('path');
var XML2JS = require('xml2js').parseString;
var PythonShell = require('python-shell');
var app = express();
var tmpTextFile = 'public/body.txt';
var markovGenFile = tmpTextFile;
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: false }))
var mergeBodyText = function(res) {
console.log("Beginning video merge...");
var options = {
mode: 'text',
pythonOptions: ['-u'],
args: [tmpTextFile]
};
PythonShell.run('merge.py', options, function(err, results) {
if (err) {
return console.log(err);
}
if (results) {
console.log('results:');
for (var result of results) {
console.log(result);
}
}
var file = __dirname + 'output.mp4';
res.download('output.mp4');
});
}
var generateMakovText = function(res) {
console.log("Writing new TED talk to ", markovGenFile, "...");
var options = {
mode: 'text',
pythonOptions: ['-u'],
args: [markovGenFile]
};
PythonShell.run('generateText.py', options, function(err, results) {
console.log('Markov generation results: %j', results);
mergeBodyText(res);
});
}
app.post('/submit_text', function(req, res) {
var givenText = req.body.texter;
fs.writeFile(tmpTextFile, givenText, function(err) {
if(err) {
return console.log(err);
}
console.log("Wrote custom text to body.txt");
mergeBodyText(res);
});
});
app.post('/submit_song', function(req, res) {
var givenText = '';
var CHART_LYRIC_API = 'http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist={artistName}&song={songName}';
var artist = req.body.artist;
var song = req.body.song;
CHART_LYRIC_API = CHART_LYRIC_API.replace('\{artistName\}', artist).replace('\{songName\}', song);
var originalRes = res;
request(CHART_LYRIC_API, function (err, res, body) {
console.log("Searching up song lyrics on Chart Lyric...")
console.log(" Artist: ", artist, " Song: ", song);
if(err) {
return console.log(err);
}
XML2JS(body, function (err, result) {
if (err) {
return console.log(err);
}
console.log("Received lyrics response...");
var jsonResponse = JSON.parse(JSON.stringify(result));
var lyrics = jsonResponse.GetLyricResult.Lyric;
givenText = lyrics[0];
fs.writeFile(tmpTextFile, givenText, function(err) {
if(err) {
return console.log(err);
}
console.log("Wrote custom text to body.txt");
mergeBodyText(originalRes);
});
});
});
});
app.post('/generate_ted', function(req, res) {
generateMakovText(res);
});
var server = app.listen(8080, function() {
var addr = server.address();
console.log('Listening @ port %d', addr.port);
});