-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli-shuffle-all.php
100 lines (91 loc) · 3.14 KB
/
cli-shuffle-all.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
<?php
$lock_file = fopen('/run/lock/spotify.pid', 'c');
$got_lock = flock($lock_file, LOCK_EX | LOCK_NB, $wouldblock);
if ($lock_file === false || (!$got_lock && !$wouldblock)) {
throw new Exception(
"Unexpected error opening or locking lock file. Perhaps you " .
"don't have permission to write to the lock file or its " .
"containing directory?"
);
} else if (!$got_lock && $wouldblock) {
exit("Another instance is already running; terminating.\n");
}
ftruncate($lock_file, 0);
fwrite($lock_file, getmypid() . "\n");
require 'vendor/autoload.php';
require 'config.php';
$sleep=10;
// Using https://github.com/jwilsson/spotify-web-api-php
$session = new SpotifyWebAPI\Session($clientid, $clientsecrect, $returnurl);
$options = [
'auto_retry'=>true,
'scope' => [
'playlist-modify-public',
'user-read-email',
'user-library-modify',
'user-read-recently-played',
],
];
$api = new SpotifyWebAPI\SpotifyWebAPI(array('auto_retry'=>true));
$db = new PDO("mysql:host=localhost;dbname=spotify;",'spotify','spotify');
$stmt = $db->query("SELECT refreshToken FROM token;");
while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) $token=$row;
$session->refreshAccessToken($token['refreshToken']);
$accessToken = $session->getAccessToken();
$refreshToken = $session->getRefreshToken();
$api->setAccessToken($accessToken);
if ($refreshToken!=$token['refreshToken']) {
$stmt = $db->prepare("UPDATE token SET refreshToken = :refreshToken;");
$opts = array(':refreshToken'=>$refreshToken);
$stmt->execute($opts);
}
$recents = $api->getMyRecentTracks(array('limit'=>50));
$played=array();
$liststocheck = array();
foreach ($recents->items as $track) {
if (!in_array($track->track->id, $played)) $played[$track->track->id] = $track->played_at;
$liststocheck[$track->context->uri]=1;
}
if (count($played)>0) {
sleep($sleep);
$playlists = $api->getUserPlaylists($spotifyusername, ['limit' => 50]);
foreach ($playlists->items as $playlist) {
if ($playlist->tracks->total>100/$randomposition) {
sleep($sleep);
$move = array();
$offset=0;
$playlistTracks = $api->getPlaylistTracks($playlist->id, $options=['offset'=>0,'limit'=>100]);
if (isset($playlistTracks->items)) {
foreach ($playlistTracks->items as $track) {
if (isset($track->track->id)&&array_key_exists($track->track->id, $played)&&$offset<$playlist->tracks->total*0.5) $move[]=$offset;
$offset++;
}
}
if (count($move)>0) {
lg($playlist->name);
rsort($move);
foreach ($move as $i) {
sleep($sleep);
$place=rand(floor(($playlist->tracks->total)*$randomposition), $playlist->tracks->total);
lg(' '.$i.' > '.$place.' | '.$playlist->tracks->total);
$api->reorderPlaylistTracks($playlist->id, [
'range_start' => $i,
'insert_before' => $place,
]);
}
}
}
}
}
function lg($msg) {
echo $msg.PHP_EOL;
$fp=fopen('/var/log/Spotify-dedup-shuffle-'.date("Y-m-d").'.log', "a+");
$time=microtime(true);
$dFormat="Y-m-d H:i:s";
$mSecs=$time-floor($time);
$mSecs=substr(number_format($mSecs, 3), 1);
fwrite($fp, sprintf("%s%s %s\n", date($dFormat), $mSecs, $msg));
fclose($fp);
}
ftruncate($lock_file, 0);
flock($lock_file, LOCK_UN);