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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
pub mod album;
pub mod annotation;
pub mod auth;
pub mod search;
pub mod song;
pub mod user;
use album::Album;
use regex::Regex;
use reqwest::Client;
use scraper::{Html, Selector};
use search::Hit;
use serde::Deserialize;
use song::Song;
#[cfg(test)]
mod tests {
use super::*;
use dotenv;
#[tokio::test]
async fn search_test() {
dotenv::dotenv().expect("Can't load dot env file");
let genius = Genius::new(dotenv::var("TOKEN").unwrap());
let result = genius.search("Ariana Grande").await;
assert!(result.is_ok());
}
#[tokio::test]
async fn get_lyrics_test() {
dotenv::dotenv().expect("Can't load dot env file");
let genius = Genius::new(dotenv::var("TOKEN").unwrap());
let lyrics = genius
.get_lyrics("https://genius.com/Lsd-thunderclouds-lyrics")
.await
.unwrap();
for verse in lyrics {
println!("{}", verse);
}
}
#[tokio::test]
async fn get_song_test() {
dotenv::dotenv().expect("Can't load dot env file");
let genius = Genius::new(dotenv::var("TOKEN").unwrap());
genius.get_song(378195, "plain").await.unwrap();
}
#[tokio::test]
async fn get_album_test() {
dotenv::dotenv().expect("Can't load dot env file");
let genius = Genius::new(dotenv::var("TOKEN").unwrap());
genius.get_album(27501, "plain").await.unwrap();
}
}
const URL: &str = "https://api.genius.com";
pub struct Genius {
reqwest: Client,
token: String,
}
impl Genius {
pub fn new(token: String) -> Self {
Self {
reqwest: Client::new(),
token,
}
}
pub async fn search(&self, q: &str) -> Result<Vec<Hit>, reqwest::Error> {
let request = self
.reqwest
.get(format!("{}/search?q={}", URL, q))
.bearer_auth(&self.token)
.send()
.await?;
let res = request.json::<Response>().await?;
Ok(res.response.hits.unwrap())
}
pub async fn get_lyrics(&self, url: &str) -> Result<Vec<String>, reqwest::Error> {
let res = &self
.reqwest
.get(url)
.header("Cookie", "_genius_ab_test_cohort=33")
.send()
.await?
.text()
.await?;
let regex_italic = Regex::new("</*i>").unwrap();
let html = String::from(regex_italic.replace_all(res, ""));
let document = Html::parse_document(&html);
let lyrics_selector = Selector::parse("div.Lyrics__Container-sc-1ynbvzw-8").unwrap();
let mut lyrics = vec![];
document.select(&lyrics_selector).for_each(|elem| {
elem.text().for_each(|text| {
lyrics.push(text.to_string());
});
});
Ok(lyrics)
}
pub async fn get_song(&self, id: u32, text_format: &str) -> Result<Song, reqwest::Error> {
let request = self
.reqwest
.get(format!("{}/songs/{}?text_format={}", URL, id, text_format))
.bearer_auth(&self.token)
.send()
.await?;
let res = request.json::<Response>().await?;
Ok(res.response.song.unwrap())
}
pub async fn get_album(&self, id: u32, text_format: &str) -> Result<Album, reqwest::Error> {
let request = self
.reqwest
.get(format!("{}/albums/{}?text_format={}", URL, id, text_format))
.bearer_auth(&self.token)
.send()
.await?;
let res = request.json::<Response>().await?;
Ok(res.response.album.unwrap())
}
}
#[derive(Deserialize, Debug)]
pub struct Body {
pub plain: Option<String>,
pub html: Option<String>,
}
#[derive(Deserialize, Debug)]
struct Meta {
status: u32,
message: Option<String>,
}
#[derive(Deserialize, Debug)]
struct Response {
meta: Meta,
response: BlobResponse,
}
#[derive(Deserialize, Debug)]
struct BlobResponse {
song: Option<Song>,
hits: Option<Vec<Hit>>,
album: Option<Album>,
}