forked from isakjanov/pusha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpusha_music.php
62 lines (61 loc) · 1.95 KB
/
pusha_music.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
<?
require_once "lib/PlaylistManager.php";
require_once "lib/PlaylistBuilder.php";
require_once "lib/Uploader.php";
$config = yaml_parse(file_get_contents("config.yml"));
$d = dir($config["music_path"]);
while (false !== ($entry = $d->read())) {
if ($entry == ".." || $entry == ".") continue;
$builder = new PlaylistBuilder();
$manager = new PlaylistManager();
try {
$dir = $config["music_path"]."/".$entry;
echo "Working with {$dir}\n";
$playlist = $builder->build($dir);
$uploader = new Uploader();
$mp3s = array();
echo "Is playlist exists?\n";
if ($manager->isNew($playlist->title)->status) {
echo "We have this playlist already. Skipping...\n";
continue;
} else {
echo "Playlist is new\n";
echo "Creating playlist {$playlist->title}\n";
$result = $manager->createPlaylist($playlist);
if ($result == NULL || $result->status == "ERR") {
echo "Cannot create playlist\n";
continue;
}
if ($result->status == "ERR") {
echo "Cannot create playlist\n";
continue;
}
$playlist_id = $result->playlist_id;
echo "Playlist id is $playlist_id\n";
}
echo "Uploading tracks for {$playlist->title}\n";
foreach ($playlist->tracks as $track) {
echo "Uploading $track\n";
try {
$file_id = $uploader->upload($track)->file_id;
} catch(Exception $e) {
echo "Error uploading file ".$e->getMessage()."\n";
continue;
}
echo "File id is $file_id\n";
if (empty($file_id)) {
echo "Ups... Failed to upload file... Skip it\n";
continue;
}
echo "Add this track to playlist\n";
try {
echo $manager->addMp3ToPlaylist($playlist_id, $file_id)->status."\n";
} catch(Exception $e) {
echo "Error adding track to playlist ".$e->getMessage()."\n";
}
sleep(0.5);
}
} catch (IncompletePlaylistException $e) {
echo $e->getMessage()."\n";
}
}