-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeniusProvider.ts
137 lines (124 loc) · 3.69 KB
/
GeniusProvider.ts
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
import Provider from '../Provider';
import phin from 'phin';
import { PartialSong, Song } from '../types';
import cheerio from 'cheerio';
const host = 'https://genius.com';
const searchRegex = /(.+(?= – )) – (.+(?=\|\/))\|(.+(?=\|))\|(\d+)/i;
const searchHeaders = {
Referrer: host,
Host: 'genius.com',
Accept:
'application/x-javascript, text/javascript, text/html, application/xml, text/xml, */*',
};
export interface GeniusAPIResults {
response: {
hits: {
result: {
title: string;
url: string;
primary_artist: {
name: string;
};
};
}[];
};
}
/**
* Provides song data from Genius.com.
*
* @export
* @class GeniusProvider
* @extends {Provider}
*/
export default class GeniusProvider extends Provider {
private apiKey?: string;
/**
* Creates an Genius provider.
*
* The API key is 100% optional and it's only used for searching.
*/
constructor(apiKey?: string) {
super();
this.apiKey = apiKey;
}
public async search(query: string) {
if (this.apiKey) {
return this.apiSearch(query);
} else {
return this.peasantSearch(query);
}
}
private async apiSearch(query: string): Promise<PartialSong[]> {
const body = await phin({
url: `https://api.genius.com/search?q=${encodeURIComponent(query)}`,
parse: 'json',
headers: {
Authorization: `Bearer ${this.apiKey}`,
},
}).then(res => res.body as GeniusAPIResults);
return body.response.hits.map<PartialSong>(obj => ({
artist: obj.result.primary_artist.name,
title: obj.result.title,
url: obj.result.url,
}));
}
private async peasantSearch(query: string): Promise<PartialSong[]> {
const body = await phin({
url: `${host}/search/quick?&q=${encodeURIComponent(query)}`,
headers: searchHeaders,
}).then(res => res.body.toString());
const songs = ((body
.split('\n')
.map(line => searchRegex.exec(line))
.filter(val => val) as unknown) as [
string,
string,
string,
string,
string
]).map(([_, artist, title, url]) => {
return {
artist,
title,
url: host + url,
};
});
return songs;
}
async lyrics(song: PartialSong, firstTime = true): Promise<Song> {
const body = await phin(song.url).then(res => res.body.toString());
const $ = cheerio.load(body);
const artist = $('h2')
.first()
.text()
.trim(),
lyrics = $('.lyrics')
.first()
.text()
.trim(),
thumbnail = $('.cover_art-image')
.first()
.attr('src');
if (!(artist && lyrics && thumbnail)) {
return firstTime
? this.lyrics(song, false)
: {
artist: (null as unknown) as string,
lyrics: (null as unknown) as string,
thumbnail: (null as unknown) as string,
title: (null as unknown) as string,
url: (null as unknown) as string,
};
}
return {
artist,
lyrics,
thumbnail,
title: $('h1')
.first()
.text()
.trim(),
url: song.url,
};
}
}