-
Notifications
You must be signed in to change notification settings - Fork 0
/
genius.js
119 lines (100 loc) · 2.66 KB
/
genius.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
import fetch from 'node-fetch';
import http from 'http'
import cheerio from 'cheerio';
import fs, {
existsSync
} from 'fs';
const PORT = 3000;
const token = "oevAmuh71xZFg1T9WhQfdPPXl4WcbIg1zW52b-NSMF68dO1rliZx8zRdXZUK1yxQ";
const exportFile = "exported.json"
const readSongFile = (filePath) => {
fs.stat(filePath, (err, stat) => {
if (err && err.code === 'ENOENT') {
throw ('Could not find songs .txt file.')
}
})
const storedtext = fs.readFileSync(filePath).toString();
if (storedtext.length == 0) {
throw ('Songs .txt is empty')
}
return storedtext
}
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
});
server.listen(PORT, () => {
console.log('Server running at PORT:' + PORT);
})
const get_data = async (url) => {
const response = await fetch(url);
const data = response.text()
return data;
}
const get_results = async (query) => {
let rj = null
const uri = encodeURI(query.trim())
await fetch("https://api.genius.com/search?q=" + uri + "&access_token=" + token)
.then((res) => res.json().then((rjson) => {
rj = rjson
}));
return rj
}
const get_hit_paths = (rj) => {
return rj.response.hits.map((el) => {
return el.result.path
})
}
const scrapper = async (url) => {
const data = await get_data(url);
const $ = cheerio.load(data);
const lyrics = $("p").text()
return new Object({
'url': url,
'lyrics': lyrics
});
}
const exportToJSON = (jsonFile) => {
try {
if (!existsSync(exportFile))
throw new Error('Did not find export File. Creating one now.')
} catch (err) {
console.log(err)
}
try {
fs.writeFileSync(exportFile, "", "utf-8")
} catch (err) {
console.log(err)
}
fs.appendFileSync(exportFile, JSON.stringify(jsonFile), "utf-8")
}
const initiateTask = async () => {
try {
let tracks = readSongFile('songs.txt').split("\n");
let scrappedLyrics = {
'songs': []
}
let tracks_json = tracks.map((track) => {
let [song, artist] = track.split(",")
return new Object({
'song': song,
'artist': artist
})
})
for (let t in tracks_json) {
let hits = await get_results(tracks_json[t].song + " " + tracks_json[t].artist);
let paths = get_hit_paths(hits);
for (let p in paths) {
let song_url = "http://genius.com" + paths[p];
const lyricObj = await scrapper(song_url)
scrappedLyrics.songs.push(lyricObj)
}
}
exportToJSON(scrappedLyrics);
console.log('Task Finished');
} catch (err) {
console.log('Could not complete task. Reason: ' + err)
}
}
initiateTask()