-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyric.php
232 lines (185 loc) · 6.09 KB
/
lyric.php
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
223
224
225
226
227
228
229
230
231
<?php
if (!class_exists('FujirouCommon')) {
if (file_exists(__DIR__.'/fujirou_common.php')) {
include_once(__DIR__.'/fujirou_common.php');
} else if (file_exists(__DIR__.'/../../include/fujirou_common.php')) {
include_once(__DIR__.'/../../include/fujirou_common.php');
}
}
if (!class_exists('Scrobbler')) {
if (file_exists(__DIR__.'/scrobbler.php')) {
include_once(__DIR__.'/scrobbler.php');
}
}
class FujirouMusixMatch{
private $_site = 'https://www.musixmatch.com';
public function __construct() {
}
public function getLyricsList($artist, $title, $info) {
Scrobbler::scrobbleTrack($artist, $title);
return $this->search($info, $artist, $title);
}
public function getLyrics($id, $info) {
return $this->get($info, $id);
}
public function search($handle, $artist, $title) {
$count = 0;
// <del> https://www.musixmatch.com/ws/1.1/macro.search?format=json&q=linkin%20park&page_size=4 </del>
// https://www.musixmatch.com/search/december%20%20taylor%20swift/tracks
$searchUrl = sprintf(
"%s/search/%s/tracks",
$this->_site, rawurlencode(sprintf('%s %s', $title, $artist))
);
$content = FujirouCommon::getContent($searchUrl);
if (!$content) {
return $count;
}
$list = $this->parseSearchResult($content);
for ($idx = 0; $idx < count($list); $idx++) {
$obj = $list[$idx];
$handle->addTrackInfoToList(
$obj['artist'],
$obj['title'],
$obj['id'],
$obj['partial']
);
}
return count($list);
}
public function get($handle, $id) {
$lyric = '';
$url = sprintf("%s%s", $this->_site, $id);
$content = FujirouCommon::getContent($url);
if (!$content) {
return false;
}
$prefix = 'var __mxmState = ';
$suffix = ';</script>';
$json_string = FujirouCommon::getSubString($content, $prefix, $suffix);
if (!$json_string || $json_string === $content) {
return false;
}
$json_string = str_replace($prefix, '', $json_string);
$json_string = str_replace($suffix, '', $json_string);
$json = json_decode($json_string, true);
if (!$json) {
return false;
}
if (!isset($json['page']['lyrics']['lyrics']['body'])) {
return false;
}
$body = $json['page']['lyrics']['lyrics']['body'];
$title = $json['page']['track']['name'];
$artist = $json['page']['track']['artistName'];
$lyric = sprintf(
"%s\n\n%s\n\n\n%s",
'lyric from musixmatch',
"$artist - $title",
$body
);
$handle->addLyrics($lyric, $id);
return true;
}
private function parseSearchResult($content) {
$result = array();
$prefix = '<a class="title" ';
$suffix = '</div></li>';
$block = FujirouCommon::getSubString($content, $prefix, $suffix);
$pattern = '/<a class="title" .*?><span.*?>(.*?)<\/span><\/a>/';
$value = FujirouCommon::getFirstMatch($block, $pattern);
if (!$value) {
return $result;
}
$title = FujirouCommon::decodeHTML($value);
$pattern = '/<a class="artist".*?>(.*?)<\/a>/';
$value = FujirouCommon::getFirstMatch($block, $pattern);
if (!$value) {
return $result;
}
$artist = FujirouCommon::decodeHTML($value);
$pattern = '/<a class="title" href="(.+?)".*?><span.*?>.*?<\/span><\/a>/';
$value = FujirouCommon::getFirstMatch($block, $pattern);
if (!$value) {
return $result;
}
$id = $value;
$item = array(
'artist' => $artist,
'title' => $title,
'id' => $id,
'partial'=> ''
);
array_push($result, $item);
return $result;
}
}
if (!debug_backtrace()) {
class TestObj {
private $items;
function __construct() {
$this->items = array();
}
public function addLyrics($lyric, $id) {
printf("\n");
printf("song id: %s\n", $id);
printf("\n");
printf("== lyric ==\n");
printf("%s\n", $lyric);
printf("** END of lyric **\n\n");
}
public function addTrackInfoToList($artist, $title, $id, $prefix) {
printf("\n");
printf("song id: %s\n", $id);
printf("%s - %s\n", $artist, $title);
printf("\n");
printf("== prefix ==\n");
printf("%s\n", $prefix);
printf("** END of prefix **\n\n");
array_push($this->items, array(
'artist' => $artist,
'title' => $title,
'id' => $id
));
}
function getItems() {
return $this->items;
}
function getFirstItem() {
if (count($this->items) > 0) {
return $this->items[0];
} else {
return false;
}
}
}
$module = 'FujirouMusixMatch';
// $artist = 'pitbull';
// $title = 'We Are One (Ole Ola)';
// $title = 'red (original demo version)';
// $title = 'album red';
// $artist = 'Usher';
// $title = 'Lovers & Friends';
// $artist = 'taylor swift';
// $title = 'style';
// $artist = 'rihanna';
// $title = 'work';
// $artist = 'CHiCo with HoneyWorks';
// $title = 'プライド革命';
$artist = 'Cheat Codes & Dante Klein';
$title = 'Let Me Hold You (Turn Me On)';
$refClass = new ReflectionClass($module);
$obj = $refClass->newInstance();
printf("Search title [$title], artist [$artist]\n");
$testObj = new TestObj();
$count = $obj->search($testObj, $artist, $title);
if ($count > 0) {
$item = $testObj->getFirstItem();
if (array_key_exists('id', $item)) {
$obj->get($testObj, $item['id']);
} else {
echo "\nno id to query lyric\n";
}
}
}
// vim: expandtab ts=4
?>