-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdioLastConvert.php
201 lines (179 loc) · 6.72 KB
/
rdioLastConvert.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
<?php
/** Converts Rdio JSON blob to last.fm export format
*
* artist MBID lookups are made to the MusicBrainz API and cached
*
* MusicBrainz Licence: http://musicbrainz.org/doc/Live_Data_Feed
* API: http://musicbrainz.org/doc/Development/XML_Web_Service/Version_2
*
* PHP version 5.5
*
* @category Rdio
* @package RdioLastConverter
* @author Rowcliffe Browne <[email protected]>
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link https://github.com/cybacolt
*/
namespace Rdio;
$config = array();
$config['uscrobble'] = false; // default to libreImport.py format
$options = getopt('i:o:uh');
if(empty($options)) {
$options['h'] = 'h';
}
foreach ($options as $option => $optionValue) {
switch ($option) {
case "h":
print "php rdioLastConvert.php -i <file> -o <file> -h\n";
print "\n";
print "\t -i\tinput file\n";
print "\t -o\toutput file\n";
print "\t -u\toutput in universalscrobble CSV (defaults to libreImport.py CSV)\n";
print "\t -h\tdisplay help\n";
print "\n";
die();
break; // gotta love PSR2 standards...
case "i":
$config['input'] = $optionValue;
break;
case "o":
$config['output'] = $optionValue;
break;
case "u":
$config['uscrobble'] = true;
break;
default:
break;
}
}
try {
if (array_key_exists('input', $config) && array_key_exists('output', $config)) {
$rdioConvert = new RdioLastConverter($config);
$output = $rdioConvert->lastConvert();
file_put_contents('./'.$config['output'], $output);
} else {
throw new \Exception("missing arguments...\n");
}
} catch (\Exception $e) {
print $e->getMessage();
}
/** RdioLastConverter
*
* Converts Rdio JSON blob to last.fm export format
*
* @category Rdio
* @package RdioLastConverter
* @author Rowcliffe Browne <[email protected]>
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link https://github.com/cybacolt
*/
class RdioLastConverter
{
private $artistLookup = array();
private $rdioHistory = '';
private $fauxLastFMHistory = array();
private $config = array();
private $options = array(
'http' =>
array(
'method' => "GET",
'header' => "User-Agent: Rdio History Lookup/1.0 ( [email protected] )\r\n"));
private $throttled = array();
public function __construct($config)
{
$this->rdioHistory = json_decode(file_get_contents('./'.$config['input']));
if (file_exists('./lookup.cache')) {
// reduce MB lookups as much as possible if restarted
$this->artistLookup = json_decode(file_get_contents('./lookup.cache'), true);
}
$this->config = $config;
}
private function getArtistMBID($artist)
{
if (array_key_exists($artist, $this->artistLookup)) {
return $this->artistLookup[$artist];
}
return false;
}
private function lookupArtistMBID($artist)
{
$context = stream_context_create($this->options);
$url = "http://musicbrainz.org/ws/2/artist/?query=".urlencode(preg_quote($artist, '/'))."&fmt=json&limit=1";
for ($i=1; $i<=3; $i++) {
print "Looking Up ".$artist."... ";
sleep(1); // avoid MB throttling
$lookup = json_decode(file_get_contents($url, false, $context));
if ($lookup) {
break;
}
}
if (!$lookup) {
$this->throttled[] = $artist;
if (count($this->throttled) > 9) {
// remove failures from cache and die
foreach ($this->throttled as $item) {
unset($this->artistLookup[$item]);
}
file_put_contents('./lookup.cache', json_encode($this->artistLookup));
throw new \Exception("possibly throttled -- stopped after 10 failed attempts.\n");
}
$this->artistLookup[$artist] = '';
} elseif ($lookup->count < 1 || levenshtein(strtolower($artist), strtolower($lookup->artists[0]->name)) > 2) {
// if not found or almost exactly the same, do not match with MBID
$this->artistLookup[$artist] = '';
print "not found.\n";
return false;
} else {
$this->artistLookup[$artist] = $lookup->artists[0]->id;
print $this->artistLookup[$artist]."\n";
}
file_put_contents('./lookup.cache', json_encode($this->artistLookup));
return $this->artistLookup[$artist];
}
public function lastConvert ()
{
$i = 0;
foreach ($this->rdioHistory->result->sources as $sources) {
foreach ($sources->tracks->items as $track) {
$scrobbleTime = $track->time;
$track = $track->track;
if ($this->config['uscrobble']) {
$this->fauxLastFMHistory[] = $this->universalScrobbleMap($track);
} else {
$this->fauxLastFMHistory[] = $this->libreImportMap($scrobbleTime, $track);
}
$i++;
if ($i % 10 === 0) {
print $i." tracks\n";
}
}
}
print count($this->fauxLastFMHistory)." complete!\n";
return $this->fauxLastFMHistory;
}
private function libreImportMap($scrobbleTime, $track) {
$buffer = array();
// unixtimestamp trackname artist album trackMBID (optional) artistMBID (optional) albumMBID (optional)
$buffer['time'] = strtotime($scrobbleTime);
$buffer['name'] = $track->name;
$buffer['artist'] = $track->artist;
$buffer['album'] = $track->album;
$buffer['trackMBID'] = ''; // too ambigious to find easily, so skipping
$buffer['artistMBID'] = $this->getArtistMBID($track->artist);
$buffer['albumMBID'] = ''; // too ambigious to find easily, so skipping
if ($buffer['artistMBID'] === false) {
$buffer['artistMBID'] = $this->lookupArtistMBID($track->artist);
}
return implode("\t", $buffer)."\n";
}
private function universalScrobbleMap($track) {
$buffer = array();
// "{artist}", "{track}", "{album}", "{album artist}", "{duration}"
$buffer['artist'] = $track->artist;
$buffer['name'] = $track->name;
$buffer['album'] = $track->album;
$buffer['album_artist'] = $track->artist;
$buffer['duration'] = $track->duration;
return '"'.implode('","', $buffer).'"'."\n";
}
}