-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (105 loc) · 2.89 KB
/
index.js
File metadata and controls
114 lines (105 loc) · 2.89 KB
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
const cheerio = require('cheerio')
const axios = require('axios')
const express = require('express')
const app = express()
let topItunes = []
var allowCrossDomain = (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain)
axios.get('http://lyrics.wikia.com/wiki/LyricWiki')
.then((response)=>{
let $;
$ = cheerio.load(response.data);
$('#mpITunesFeed>ol>li>b>a').each((i,elem)=>{
let result = {
link: $(elem).attr('href')
}
topItunes.push(result)
})
getTopItunesLyrics()
})
.catch((err)=>{
})
function getTopItunesLyrics(){
topItunes.forEach((elem,index)=>{
let $;
axios.get(`http://lyrics.wikia.com${elem.link}`)
.then((response)=>{
let data = response.data
$ = cheerio.load(data)
let lyrics = $('#mw-content-text>div.lyricbox').html()
let artistsong = elem.link.match(/([^/:]*):(.*)/)
let embed = $('span.youtube').text()
topItunes[index].lyrics = lyrics
topItunes[index].artist = artistsong[1]
topItunes[index].song = artistsong[2]
if(embed){
embed = 'https://www.youtube.com/watch?v=' + embed.match(/[^|]*/)[0]
topItunes[index].embed = embed
}
}).catch((response)=>{
topItunes[index].lyrics = 'No lyrics found'
})
})
}
app.get('/itunes/',(req, res)=>{
res.send({
rescode:200,
topItunes
})
});
app.get('/search/:query',(req, res)=>{
let $;
axios.get(`http://lyrics.wikia.com/wiki/Special:Search?search=${req.params.query}`)
.then((response)=>{
let data = response.data
$ = cheerio.load(data)
let results = [];
$('.result>article>h1').each((i,elem)=>{
let result = $(elem).text().match(/([^:^\t^\n]*):(.*)/)
if(result){
results.push({
artist: result[1],
song: result[2],
})
}
})
res.send({
rescode:200,
results
})
}).catch((err)=>{
res.send({rescode:404,err});
})
})
app.get('/lyrics/:artistsong', function(req, res){
let $;
axios.get(`http://lyrics.wikia.com/wiki/${req.params.artistsong}`)
.then((response)=>{
let data = response.data
$ = cheerio.load(data)
let embed = $('span.youtube').text()
let result ={}
if(embed){
result.embed = 'https://www.youtube.com/watch?v=' + embed.match(/[^|]*/)[0]
}
result.rescode = 200
result.lyrics = $('#mw-content-text>div.lyricbox').html()
res.send(result)
}).catch((response)=>{
rescode: 404
})
})
app.listen(process.env.PORT, ()=>{
console.log('listening on port 3000')
})