-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlyricsGrabber.cpp
222 lines (169 loc) · 4.51 KB
/
lyricsGrabber.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
#include "lyricsGrabber.hpp"
#include "config.hpp"
#include <sys/types.h>
#include <regex.h>
#include <ctime>
#include <cstring>
#include <unistd.h>
using namespace std;
namespace lyricsGrabber {
//Static Function Prototypes
static int lyricsFly(string title, string artist, string album,
string& lyrics);
extern list<string>* getLyrics(string title, string artist, string album) {
list<string>* lyrics = new list<string>;
string s;
if ( lyricsFly(title, artist, album, s) == 0) {
lyrics->push_front(s);
}
return lyrics;
}
static int lyricsFly(string title, string artist, string album,
string& lyrics) {
lyrics.clear();
//First we must sanitize the paramters. Any non-alpha
//numerics (or spaces) are replaced with '%'
char* san_title = new char[title.size()+1];
char* san_artist = new char[artist.size()+1];
strcpy(san_title, title.c_str());
strcpy(san_artist, artist.c_str());
for (int i = 0; san_title[i] != '\0'; i++) {
if ( !isalnum(san_title[i]) && san_title[i] != ' ' )
san_title[i] = '%';
}
for (int i = 0; san_artist[i] != '\0'; i++) {
if ( !isalnum(san_artist[i]) && san_artist[i] != ' ')
san_artist[i] = '%';
}
//Now we must url encode the paramters
stringstream url_artist, url_title;
for(int i = 0; san_title[i] != '\0'; i++) {
switch(san_title[i]) {
case ' ':
url_title << "%20";
break;
case '%':
url_title << "%25";
break;
default:
url_title << san_title[i];
}
}
for (int i = 0; san_artist[i] != '\0'; i++) {
switch(san_artist[i]) {
case ' ':
url_artist << "%20";
break;
case '%':
url_artist << "%25";
break;
default:
url_artist << san_artist[i];
}
}
//Clean Up
delete[] san_title;
delete[] san_artist;
//Get Key From Keyfile
//Weekly keys are available from http://lyricsfly.com/api/
//Review terms and request permanent keys at
//http://lyricsfly.com/contact/
string skey;
skey = lyric_con::return_config("lyricskey");
// keyFile.open(LYRICSFLY_KEYFILE);
// getline(keyFile,skey);
// keyFile.close();
//Creation of the command to run from the shell.
stringstream curl;
curl << "curl \"http://api.lyricsfly.com/api/api.php?";
curl << "i=" << skey << "&a=";
curl << url_artist.str().c_str() << "&t=" << url_title.str().c_str()
<< "\"";
curl << " > tmp 2> /dev/null";
char* buf;
int length;
bool good_to_go = false;
do {
//System Call
system(curl.str().c_str());
ifstream in;
in.open("tmp");
if (in == NULL)
return -1;
in.seekg(0,ios::end);
length = in.tellg();
in.seekg(0, ios::beg);
buf = new char[length+1];
buf[length] = '\0';
in.read(buf, length);
in.close();
regex_t reg;
if (regcomp(®, "<status>...</status>", REG_ICASE|REG_EXTENDED) != 0) {
cout << "Regex error 1.\n";
exit(-1);
}
regmatch_t matches;
int status = regexec(®, buf, 1, &matches, 0);
if (status != 0) {
char tmp[50];
regerror(status, NULL, tmp, 50);
cout << tmp << "\n\n";
if (status == REG_NOMATCH) {
good_to_go = false;
sleep(1);
delete[] buf;
} else {
exit(-1);
}
}
if (status == 0) {
char* stat_code;
stat_code = new char[matches.rm_eo-matches.rm_so+1]; //+1 for NULL terminator!
strncpy(stat_code, buf+matches.rm_so, matches.rm_eo-matches.rm_so);
stat_code[matches.rm_eo-matches.rm_so] = '\0';
//cout << stat_code <<"\n\n";
if (strcmp(stat_code, "<status>200</status>") != 0) {
if (strcmp(stat_code, "<status>402</status>") == 0) {
good_to_go=false;
delete[] buf;
sleep(1);
} else {
good_to_go = true;
}
} else {
good_to_go = true;
}
}
regfree(®);
} while (good_to_go == false);
//Take out only the actual lyrics from the stuff.
stringstream return_buf;
regex_t reg;
if (regcomp(®, "<tx>[^><]*</tx>", REG_ICASE|REG_EXTENDED) != 0) {
cout << "Regex Error\n\n";
exit(-1);
}
regmatch_t match;
bool anyLyrics = false;
char* buf_point = buf;
int status = regexec(®, buf_point, 1, &match, 0);
while (status == 0) {
anyLyrics = true;
int len = match.rm_eo - match.rm_so;
char* lyrics_buf = new char[len];
strncpy(lyrics_buf, buf_point+match.rm_so, len);
lyrics_buf[len-5] = '\0';
return_buf << lyrics_buf + 4;
return_buf << "\n\n\n";
delete[] lyrics_buf;
buf_point = buf_point + match.rm_eo;
status = regexec(®, buf_point, 1, &match, 0);
}
delete[] buf;
//Return the results.
lyrics = return_buf.str();
if (anyLyrics == false)
return -1;
return 0;
}
}