-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.php
446 lines (370 loc) · 12.3 KB
/
get.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
/**
* get.php path_to_save
*
*/
date_default_timezone_set('Europe/Moscow');
ini_set('memory_limit', '256M');
include "config.php";
include "db.php";
include "php-soundcloud/Services/Soundcloud.php";
class scsync
{
public $save_path = '/store/';
public $save_local = true;
const db_links = DB_LINK;
const db_auth = DB_AUTH;
public $db;
public $already_downloaded = array();
public $sc; // API object
public $count = 50;
public static $current_download_operation = 'downloading content';
public $stat = array(0, 0, 0);
static function log($msg, $replace = false)
{
if ($replace) {
echo "\r" . $msg . "";
} else {
echo "\n" . wordwrap($msg, 80) . "";
}
}
/**
* Pre progress bar
*/
static function ppb()
{
self::log('_');
}
/**
* Auth
*/
public function __construct()
{
$auth = $this->getAuth();
// create client object and set access token
$this->sc = new Services_Soundcloud(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
$this->sc->setCurlOptions(array(
CURLOPT_PROGRESSFUNCTION => array(get_class($this), 'progressCallback'),
CURLOPT_NOPROGRESS => false,
CURLOPT_RETURNTRANSFER => true,
));
if ($auth['expires_in'] > time()) { // сессия еще валидна
self::log('Use saved session.');
$this->sc->setAccessToken($auth['access_token']);
} else {
// обновляем сессию
self::log('Session expired. Trying to refresh session.');
try {
self::$current_download_operation = 'Refreshing session';
self::ppb();
$token = $this->sc->accessTokenRefresh($auth['refresh_token']);
$token['expires_in'] += time();
$db = new db(self::db_auth);
$db->saveAuth(serialize($token));
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
self::log('Refreshing session failed.');
die();
}
}
self::log('Authorization successful.');
$this->already_downloaded = $this->getLinksArray();
}
/**
* Print statistic
*/
public function __destruct()
{
self::log(str_repeat('-', 80));
self::log($this->stat[0] . ' downloaded, ' . $this->stat[1] . ' skipped, ' . $this->stat[2] . ' failed');
self::log('');
}
/**
* Set path to download tracks
* @param $local
* @param $path
*/
public function setSavePath($local, $path)
{
$this->save_local = $local;
$this->save_path = $path;
}
/**
* Set download limit
* @param $count
*/
public function setCount($count)
{
$this->count = $count;
}
/**
* Download all tracks from own stream object
* @param $stream
*/
public function processStream($stream)
{
$i = 0;
$j = 0;
$count = min($this->count, count($stream->collection));
foreach ($stream->collection as $item) {
if ($i >= $count) continue;
if ($this->trackDownload($item->origin)) $j++;
$i++;
self::log($i . ' / ' . $count . ' tracks processed. ' . ceil(($i) * 100 / $count) . '%, ' . $j . ' downloaded, ' . ($i - $j) . ' skipped');
}
}
/**
* Download all tracks from array of track objects
* @param $tracks
*/
public function processTracksArray($tracks)
{
$i = 0;
$j = 0;
$count = min($this->count, count($tracks));
foreach ($tracks as $track) {
if ($i >= $count) continue;
if ($this->trackDownload($track)) $j++;
$i++;
self::log($i . ' / ' . $count . ' tracks processed. ' . ceil(($i) * 100 / $count) . '%, ' . $j . ' downloaded, ' . ($i - $j) . ' skipped');
}
}
/**
* Download one track from track object
* @param $track
* @return bool
*/
public function trackDownload($track)
{
if ($track->kind != 'track') return false;
self::log(str_repeat('-', 80)); // -----------------------------
self::log('Track "' . $track->title . '"');
if (in_array($track->id, $this->already_downloaded)) {
self::log('Already downloaded. Continue...');
$this->stat[1]++; // skipped
self::log(str_repeat('-', 80)); // -----------------------------
return false;
}
$downloaded_file = null;
$download = false;
$low = false;
try {
$low = false;
self::$current_download_operation = 'Trying to download full track';
self::ppb();
$downloaded_file = $this->sc->download($track->id);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
//self::log($e->getMessage());
self::log('Unable to download full track.');
}
if (is_null($downloaded_file)) {
self::log('Downloading stream version');
$track_url = $track->stream_url . '?client_id=' . CLIENT_ID;
//$downloaded_file = file_get_contents($track_url);
self::$current_download_operation = 'Downloading stream';
self::ppb();
$downloaded_file = $this->downloadFileByUrl($track_url);
$low = true;
}
if (!is_null($downloaded_file)) {
if ($this->save_local == true) {
$path = dirname(__FILE__) . $this->save_path;
if (!file_exists($path)) mkdir($path);
} else {
$path = $this->save_path;
}
$new_name = '';
if ($track->created_at != null) {
$new_name .= date('Y-m-d', strtotime($track->created_at));
}
$new_name .= '-';
$new_name .= str_replace(' ', '_', $track->user->username);
$new_name .= '-';
$new_name .= str_replace(' ', '_', $track->title);
if ($low) {
$new_name .= '-';
$new_name .= '128';
}
$new_name .= '.mp3';
$download = file_put_contents($path . $new_name, $downloaded_file);
if ($download) {
$this->addLink($track->id);
self::log('Successfully downloaded as "' . $new_name . '"!');
$this->stat[0]++; // downloaded
}
} else {
self::log('Unable to download ' . ($low ? '(low)' : '(high)') . ' "' . $track->title . '" ');
$this->stat[2]++; // failed
}
self::log(str_repeat('-', 80)); // -----------------------------
return $download ? true : false;
}
/**
* Download file by url (for downloading stream version of track)
* @param $url
* @return mixed
*/
public function downloadFileByUrl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array(get_class($this), 'progressCallback'));
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* Progress bar handler
* @param $download_size
* @param $downloaded
* @param $upload_size
* @param $uploaded
*/
public function progressCallback($download_size, $downloaded, $upload_size, $uploaded)
{
if ($download_size > 0) {
$progress = (round($downloaded / $download_size * 1000) / 10);
$progress_int = round($progress);
// [*******----------][43%]
$pr_long = 40;
$pr_complete = round($pr_long * $progress_int / 100);
$pr_string = "[" . str_repeat('*', $pr_complete) . str_repeat('-', ($pr_long - $pr_complete)) . "][" . $progress_int . "%] " . scsync::$current_download_operation;
// scsync::log( scsync::$current_download_operation . ', precent:' .$progress. " from " . $download_size, true);
scsync::log($pr_string, true);
} else {
scsync::log(scsync::$current_download_operation, true);
}
}
/**
* Add track id in history log
* @param $link
*/
public function addLink($link)
{
$db = new db(self::db_links);
$db->addLink($link);
}
/**
* Get list of all track ids from history log
* @return array
*/
public function getLinksArray()
{
$db = new db(self::db_links);
return $db->getLinksArray();
}
/**
* Get auth from db
* @return mixed
*/
public function getAuth()
{
$db = new db(self::db_auth);
return unserialize($db->getAuth());
}
/**
* Download tracks from own stream
*/
public function ownStream()
{
try {
self::log('Own stream');
self::$current_download_operation = 'Getting own stream';
self::ppb();
$stream = json_decode($this->sc->get('me/activities/tracks/affiliated'));
$this->processStream($stream);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
self::log('Unknown error with message ' . $e->getMessage());
}
}
/**
* Resolve content by url
* @param $url
* @return mixed
*/
public function resolve($url)
{
try {
self::$current_download_operation = 'Resolve content type by link';
self::ppb();
return json_decode($this->sc->get('resolve', array('url' => $url), array(CURLOPT_FOLLOWLOCATION => true)));
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
self::log('Resolve failed.');
}
}
/**
* Download user's tracks
* @param $id
*/
public function getUserTracks($id)
{
try {
self::log('User #' . $id);
self::$current_download_operation = 'Getting user\'s tracks';
self::ppb();
$stream = json_decode($this->sc->get('users/' . $id . '/tracks'));
$this->processTracksArray($stream);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
self::log('Unknown error with message ' . $e->getMessage());
}
}
/**
* Download group's tracks
* @param $id
*/
public function getGroupTracks($id)
{
try {
self::log('Group #' . $id);
self::$current_download_operation = 'Getting group\'s tracks';
self::ppb();
$stream = json_decode($this->sc->get('groups/' . $id . '/tracks'));
$this->processTracksArray($stream);
} catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
self::log('Unknown error with message ' . $e->getMessage());
}
}
}
$app = new scsync();
if (isset($argv[1])) {
$app->setSavePath(false, $argv[1]);
}
if (isset($argv[3])) {
$app->setCount($argv[3]);
}
if (isset($argv[2])) {
if ($argv[2] == 'STREAM') {
$app->ownStream();
} else {
$resolve = $app->resolve($argv[2]);
$id = $resolve->id;
switch ($resolve->kind) {
case 'track':
$app->trackDownload($resolve);
break;
case 'user':
$app->getUserTracks($id);
break;
case 'playlist':
$app->processTracksArray($resolve->tracks);
break;
case 'group':
$app->getGroupTracks($id);
break;
default:
scsync::log('Unknown kind: ' . $resolve->kind . ', id: ' . $id);
}
}
}
//print_r($argv);
//die('DIE');
/*
/me/activities gives you user's recent activities
/me/activities/all is the same as the one above (Recent activities) /me/activities/tracks/affiliated is the recent tracks from users the logged-in user follows (the stream)
/me/activities/tracks/exclusive is recent exclusively shared tracks
/me/activities/all/own is recent activities on the logged-in users tracks
*/
?>