-
Notifications
You must be signed in to change notification settings - Fork 3
/
lyric.php
192 lines (155 loc) · 4.3 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
<?php
if (!class_exists('FujirouCommon')) {
if (file_exists(__DIR__.'/fujirou_common.php')) {
require(__DIR__.'/fujirou_common.php');
} else if (file_exists(__DIR__.'/../../include/fujirou_common.php')) {
require(__DIR__.'/../../include/fujirou_common.php');
}
}
if (!class_exists('md_scrobbler')) {
if (file_exists(__DIR__.'/Scrobbler.php')) {
require(__DIR__.'/Scrobbler.php');
} else if (file_exists(__DIR__.'/../../include/fujirou_common.php')) {
require(__DIR__.'/../../include/fujirou_common.php');
}
}
// http://api.wikia.com/wiki/LyricWiki_API
class FujirouLyricWiki {
private $apiUrl = 'http://lyrics.wikia.com/api.php';
private $sitePrefix = 'http://lyrics.wikia.com';
public function __construct() {
}
public function getLyricsList($artist, $title, $info) {
$scrobbler = new md_Scrobbler('USER', 'PASSWORD');
$scrobbler->add($artist, $title, '', 60);
$scrobbler->submit();
return $this->search($info, $artist, $title);
}
public function getLyrics($id, $info) {
return $this->get($info, $id);
}
public function search($handle, $artist, $title) {
$count = 0;
$searchUrl = sprintf(
"%s?artist=%s&song=%s&fmt=realjson",
$this->apiUrl, urlencode($artist), urlencode($title)
);
$content = FujirouCommon::getContent($searchUrl);
$obj = json_decode($content, TRUE);
if ($obj['lyrics'] !== 'Not found') {
$obj = $this->decodeUTF8($obj);
$id = $obj['url'];
$handle->addTrackInfoToList($obj['artist'], $obj['song'], $id, $obj['lyrics']);
$count = 1;
}
return $count;
}
public function get($handle, $id) {
$result = array();
$lyric = '';
// id should be url of lyric
if (strchr($id, $this->sitePrefix) === FALSE) {
return FALSE;
}
$content = FujirouCommon::getContent($id);
if (!$content) {
return FALSE;
}
$prefix = "<div class='lyricbox'>";
$suffix = "<!--";
$lyricLine = FujirouCommon::getSubString($content, $prefix, $suffix);
$pattern = '/<\/script>(.*)<!--/';
$matchedString = FujirouCommon::getFirstMatch($lyricLine, $pattern);
if (!$matchedString) {
return FALSE;
}
$lyric = trim(str_replace('<br />', "\n", $matchedString));
$lyric = FujirouCommon::decodeHTML($lyric);
$lyric = trim(strip_tags($lyric));
$handle->addLyrics($lyric, $id);
return TRUE;
}
private function decodeUTF8($obj) {
$tryDecodeFunc = function($str) {
if ($str) {
$length = strlen($str);
$temp = '';
// if decode failed, remove character from end and decode again
while (empty($temp) && $length > 0) {
$temp = utf8_decode(substr($str, 0, $length));
$jsonStr = json_encode($temp);
if (empty($jsonStr) || 'null' === $jsonStr) {
// will lead to json_encode() fail
$temp = '';
}
$length -= 1;
}
$str = $temp;
}
return $str;
};
return array_map($tryDecodeFunc, $obj);
}
}
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 = 'FujirouLyricWiki';
// $artist = 'Taylor Swift';
// $title = 'back to december';
// $artist = 'Eminem';
// $title = 'Business';
$artist = 'Taylor Swift';
$title = 'Shake it off';
$refClass = new ReflectionClass($module);
$obj = $refClass->newInstance();
$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";
}
} else {
echo "\nempty result\n";
}
}
?>