/** * thirdDelimiterBrowse function. * * @access public * @param mixed $w * @param mixed $query * @param mixed $settings * @param mixed $db * @param mixed $update_in_progress * @return void */ function thirdDelimiterBrowse($w, $query, $settings, $db, $update_in_progress) { $words = explode('▹', $query); $kind = $words[0]; $all_playlists = $settings->all_playlists; $is_alfred_playlist_active = $settings->is_alfred_playlist_active; $radio_number_tracks = $settings->radio_number_tracks; $now_playing_notifications = $settings->now_playing_notifications; $max_results = $settings->max_results; $alfred_playlist_uri = $settings->alfred_playlist_uri; $alfred_playlist_name = $settings->alfred_playlist_name; $country_code = $settings->country_code; $last_check_update_time = $settings->last_check_update_time; $oauth_client_id = $settings->oauth_client_id; $oauth_client_secret = $settings->oauth_client_secret; $oauth_redirect_uri = $settings->oauth_redirect_uri; $oauth_access_token = $settings->oauth_access_token; $oauth_expires = $settings->oauth_expires; $oauth_refresh_token = $settings->oauth_refresh_token; $display_name = $settings->display_name; $userid = $settings->userid; $echonest_api_key = $settings->echonest_api_key; $country = $words[1]; $category = $words[2]; try { $offsetCategoryPlaylists = 0; $limitCategoryPlaylists = 50; $api = getSpotifyWebAPI($w); do { // refresh api $api = getSpotifyWebAPI($w, $api); $listPlaylists = $api->getCategoryPlaylists($category, array('country' => $country, 'limit' => $limitCategoryPlaylists, 'offset' => $offsetCategoryPlaylists)); $subtitle = "Launch Playlist"; $playlists = $listPlaylists->playlists; $items = $playlists->items; foreach ($items as $playlist) { $w->result(null, '', "🎵" . escapeQuery($playlist->name), "by " . $playlist->owner->id . " ● " . $playlist->tracks->total . " tracks", getPlaylistArtwork($w, $playlist->uri, false), 'no', null, "Online Playlist▹" . $playlist->uri . '∙' . escapeQuery($playlist->name) . "▹"); } $offsetCategoryPlaylists += $limitCategoryPlaylists; } while ($offsetCategoryPlaylists < $listPlaylists->playlists->total); } catch (SpotifyWebAPI\SpotifyWebAPIException $e) { $w->result(null, 'help', "Exception occurred", "" . $e->getMessage(), './images/warning.png', 'no', null, ''); echo $w->toxml(); return; } }
/** * refreshLibrary function. * * @access public * @param mixed $w * @return void */ function refreshLibrary($w) { if (!file_exists($w->data() . '/library.db')) { displayNotificationWithArtwork("Refresh library called while library does not exist", './images/warning.png'); return; } touch($w->data() . "/update_library_in_progress"); $w->write('InitRefreshLibrary▹' . 0 . '▹' . 0 . '▹' . time() . '▹' . 'starting', 'update_library_in_progress'); $in_progress_data = $w->read('update_library_in_progress'); // // Read settings from JSON // $settings = getSettings($w); $country_code = $settings->country_code; $userid = $settings->userid; $words = explode('▹', $in_progress_data); putenv('LANG=fr_FR.UTF-8'); ini_set('memory_limit', '512M'); $nb_playlist = 0; // db for fetch artworks $fetch_artworks_existed = true; $dbfile = $w->data() . '/fetch_artworks.db'; if (!file_exists($dbfile)) { touch($dbfile); $fetch_artworks_existed = false; } // kill previous process if running $pid = exec("ps -efx | grep \"php\" | egrep \"DOWNLOAD_ARTWORKS\" | grep -v grep | awk '{print \$2}'"); if ($pid != "") { logMsg("KILL Download daemon <{$pid}>"); $ret = exec("kill -9 \"{$pid}\""); } if (file_exists($w->data() . '/download_artworks_in_progress')) { deleteTheFile($w->data() . '/download_artworks_in_progress'); } try { $dbartworks = new PDO("sqlite:{$dbfile}", "", "", array(PDO::ATTR_PERSISTENT => true)); $dbartworks->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($dbartworks, $w); $dbartworks = null; $db = null; return false; } // DB artowrks if ($fetch_artworks_existed == false) { try { $dbartworks->exec("create table artists (artist_name text PRIMARY KEY NOT NULL, already_fetched boolean)"); $dbartworks->exec("create table tracks (track_uri text PRIMARY KEY NOT NULL, already_fetched boolean)"); $dbartworks->exec("create table albums (album_uri text PRIMARY KEY NOT NULL, already_fetched boolean)"); } catch (PDOException $e) { logMsg("Error(updateLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($dbartworks, $w); $dbartworks = null; $db = null; return false; } } try { // artworks $insertArtistArtwork = "insert or ignore into artists values (:artist_name,:already_fetched)"; $stmtArtistArtwork = $dbartworks->prepare($insertArtistArtwork); $insertTrackArtwork = "insert or ignore into tracks values (:track_uri,:already_fetched)"; $stmtTrackArtwork = $dbartworks->prepare($insertTrackArtwork); $insertAlbumArtwork = "insert or ignore into albums values (:album_uri,:already_fetched)"; $stmtAlbumArtwork = $dbartworks->prepare($insertAlbumArtwork); } catch (PDOException $e) { logMsg("Error(updateLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($dbartworks, $w); $dbartworks = null; $db = null; return false; } $artworksToDownload = false; rename($w->data() . '/library.db', $w->data() . '/library_old.db'); copy($w->data() . '/library_old.db', $w->data() . '/library_new.db'); $dbfile = $w->data() . '/library_new.db'; $nb_added_playlists = 0; $nb_removed_playlists = 0; $nb_updated_playlists = 0; try { $db = new PDO("sqlite:{$dbfile}", "", "", array(PDO::ATTR_PERSISTENT => true)); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->exec("drop table counters"); $db->exec("create table counters (all_tracks int, yourmusic_tracks int, all_artists int, yourmusic_artists int, all_albums int, yourmusic_albums int, playlists int)"); $getPlaylists = "select * from playlists where uri=:uri"; $stmtGetPlaylists = $db->prepare($getPlaylists); $insertPlaylist = "insert into playlists values (:uri,:name,:nb_tracks,:owner,:username,:playlist_artwork_path,:ownedbyuser,:nb_playable_tracks,:duration_playlist,:nb_times_played,:collaborative,:public)"; $stmtPlaylist = $db->prepare($insertPlaylist); $insertTrack = "insert into tracks values (:yourmusic,:popularity,:uri,:album_uri,:artist_uri,:track_name,:album_name,:artist_name,:album_type,:track_artwork_path,:artist_artwork_path,:album_artwork_path,:playlist_name,:playlist_uri,:playable,:added_at,:duration,:nb_times_played,:local_track)"; $stmtTrack = $db->prepare($insertTrack); $deleteFromTracks = "delete from tracks where playlist_uri=:playlist_uri"; $stmtDeleteFromTracks = $db->prepare($deleteFromTracks); $updatePlaylistsNbTracks = "update playlists set nb_tracks=:nb_tracks,nb_playable_tracks=:nb_playable_tracks,duration_playlist=:duration_playlist,public=:public where uri=:uri"; $stmtUpdatePlaylistsNbTracks = $db->prepare($updatePlaylistsNbTracks); $deleteFromTracksYourMusic = "delete from tracks where yourmusic=:yourmusic"; $stmtDeleteFromTracksYourMusic = $db->prepare($deleteFromTracksYourMusic); } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $dbartworks = null; $db = null; return; } $savedListPlaylist = array(); $offsetGetUserPlaylists = 0; $limitGetUserPlaylists = 50; do { $retry = true; $nb_retry = 0; $api = getSpotifyWebAPI($w); while ($retry) { try { // refresh api $api = getSpotifyWebAPI($w, $api); $userPlaylists = $api->getUserPlaylists(urlencode($userid), array('limit' => $limitGetUserPlaylists, 'offset' => $offsetGetUserPlaylists)); $retry = false; } catch (SpotifyWebAPI\SpotifyWebAPIException $e) { logMsg("Error(getUserPlaylists): retry " . $nb_retry . " (exception " . print_r($e) . ")"); if ($e->getCode() == 429 || $e->getCode() == 404 || $e->getCode() == 500 || $e->getCode() == 502 || $e->getCode() == 503 || $e->getCode() == 0) { // retry if ($nb_retry > 10) { handleSpotifyWebAPIException($w, $e); $retry = false; return false; } $nb_retry++; sleep(5); } else { handleSpotifyWebAPIException($w, $e); $retry = false; return false; } } } $nb_playlist_total = $userPlaylists->total; foreach ($userPlaylists->items as $playlist) { if ($playlist->name != "") { $savedListPlaylist[] = $playlist; } } $offsetGetUserPlaylists += $limitGetUserPlaylists; } while ($offsetGetUserPlaylists < $userPlaylists->total); // consider Your Music as a playlist for progress bar $nb_playlist_total++; foreach ($savedListPlaylist as $playlist) { $tracks = $playlist->tracks; $owner = $playlist->owner; $nb_playlist++; $w->write('Refresh Library▹' . $nb_playlist . '▹' . $nb_playlist_total . '▹' . $words[3] . '▹' . escapeQuery($playlist->name), 'update_library_in_progress'); try { // Loop on existing playlists in library $stmtGetPlaylists->bindValue(':uri', $playlist->uri); $stmtGetPlaylists->execute(); $noresult = true; while ($playlists = $stmtGetPlaylists->fetch()) { $noresult = false; break; } } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $dbartworks = null; $db = null; return; } // Playlist does not exist, add it if ($noresult == true) { $nb_added_playlists++; $playlist_artwork_path = getPlaylistArtwork($w, $playlist->uri, true, true); if ("-" . $owner->id . "-" == "-" . $userid . "-") { $ownedbyuser = 1; } else { $ownedbyuser = 0; } $nb_track_playlist = 0; $duration_playlist = 0; $offsetGetUserPlaylistTracks = 0; $limitGetUserPlaylistTracks = 100; do { $retry = true; $nb_retry = 0; while ($retry) { try { // refresh api $api = getSpotifyWebAPI($w, $api); $userPlaylistTracks = $api->getUserPlaylistTracks(urlencode($owner->id), $playlist->id, array('fields' => array('total', 'items(added_at)', 'items(is_local)', 'items.track(is_playable,duration_ms,uri,popularity,name)', 'items.track.album(album_type,images,uri,name)', 'items.track.artists(name,uri)'), 'limit' => $limitGetUserPlaylistTracks, 'offset' => $offsetGetUserPlaylistTracks, 'market' => $country_code)); $retry = false; } catch (SpotifyWebAPI\SpotifyWebAPIException $e) { logMsg("Error(getUserPlaylistTracks): retry " . $nb_retry . " (exception " . print_r($e) . ")"); if ($e->getCode() == 429 || $e->getCode() == 404 || $e->getCode() == 500 || $e->getCode() == 502 || $e->getCode() == 503 || $e->getCode() == 0) { // retry if ($nb_retry > 10) { handleSpotifyWebAPIException($w, $e); $retry = false; return false; } $nb_retry++; sleep(5); } else { handleSpotifyWebAPIException($w, $e); $retry = false; return false; } } } foreach ($userPlaylistTracks->items as $item) { $track = $item->track; $artists = $track->artists; $artist = $artists[0]; $album = $track->album; $playable = 0; $local_track = 0; if (isset($track->is_playable) && $track->is_playable) { $playable = 1; } if (isset($item->is_local) && $item->is_local) { $playable = 1; $local_track = 1; } try { // // Download artworks in Fetch later mode $thetrackuri = 'spotify:track:faketrackuri'; if ($local_track == 0 && isset($track->uri)) { $thetrackuri = $track->uri; } list($already_present, $track_artwork_path) = getTrackOrAlbumArtwork($w, $thetrackuri, true, true); if ($already_present == false) { $artworksToDownload = true; $stmtTrackArtwork->bindValue(':track_uri', $thetrackuri); $stmtTrackArtwork->bindValue(':already_fetched', 0); $stmtTrackArtwork->execute(); } $theartistname = 'fakeartist'; if (isset($artist->name)) { $theartistname = $artist->name; } list($already_present, $artist_artwork_path) = getArtistArtwork($w, $theartistname, true, true); if ($already_present == false) { $artworksToDownload = true; $stmtArtistArtwork->bindValue(':artist_name', $theartistname); $stmtArtistArtwork->bindValue(':already_fetched', 0); $stmtArtistArtwork->execute(); } $thealbumuri = 'spotify:album:fakealbumuri'; if (isset($album->uri)) { $thealbumuri = $album->uri; } list($already_present, $album_artwork_path) = getTrackOrAlbumArtwork($w, $thealbumuri, true, true); if ($already_present == false) { $artworksToDownload = true; $stmtAlbumArtwork->bindValue(':album_uri', $thealbumuri); $stmtAlbumArtwork->bindValue(':already_fetched', 0); $stmtAlbumArtwork->execute(); } } catch (PDOException $e) { logMsg("Error(updateLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($dbartworks, $w); $dbartworks = null; $db = null; return false; } $duration_playlist += $track->duration_ms; try { $stmtTrack->bindValue(':yourmusic', 0); $stmtTrack->bindValue(':popularity', $track->popularity); $stmtTrack->bindValue(':uri', $track->uri); $stmtTrack->bindValue(':album_uri', $album->uri); $stmtTrack->bindValue(':artist_uri', $artist->uri); $stmtTrack->bindValue(':track_name', escapeQuery($track->name)); $stmtTrack->bindValue(':album_name', escapeQuery($album->name)); $stmtTrack->bindValue(':artist_name', escapeQuery($artist->name)); $stmtTrack->bindValue(':album_type', $album->album_type); $stmtTrack->bindValue(':track_artwork_path', $track_artwork_path); $stmtTrack->bindValue(':artist_artwork_path', $artist_artwork_path); $stmtTrack->bindValue(':album_artwork_path', $album_artwork_path); $stmtTrack->bindValue(':playlist_name', escapeQuery($playlist->name)); $stmtTrack->bindValue(':playlist_uri', $playlist->uri); $stmtTrack->bindValue(':playable', $playable); $stmtTrack->bindValue(':added_at', $item->added_at); $stmtTrack->bindValue(':duration', beautifyTime($track->duration_ms / 1000)); $stmtTrack->bindValue(':nb_times_played', 0); $stmtTrack->bindValue(':local_track', $local_track); $stmtTrack->execute(); } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $dbartworks = null; $db = null; return; } $nb_track_playlist++; } $offsetGetUserPlaylistTracks += $limitGetUserPlaylistTracks; } while ($offsetGetUserPlaylistTracks < $userPlaylistTracks->total); try { $stmtPlaylist->bindValue(':uri', $playlist->uri); $stmtPlaylist->bindValue(':name', escapeQuery($playlist->name)); $stmtPlaylist->bindValue(':nb_tracks', $tracks->total); $stmtPlaylist->bindValue(':owner', $owner->id); $stmtPlaylist->bindValue(':username', $owner->id); $stmtPlaylist->bindValue(':playlist_artwork_path', $playlist_artwork_path); $stmtPlaylist->bindValue(':ownedbyuser', $ownedbyuser); $stmtPlaylist->bindValue(':nb_playable_tracks', $nb_track_playlist); $stmtPlaylist->bindValue(':duration_playlist', beautifyTime($duration_playlist / 1000, true)); $stmtPlaylist->bindValue(':nb_times_played', 0); $stmtPlaylist->bindValue(':collaborative', $playlist->collaborative); $stmtPlaylist->bindValue(':public', $playlist->public); $stmtPlaylist->execute(); } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $dbartworks = null; $db = null; return; } displayNotificationWithArtwork('Added playlist ' . escapeQuery($playlist->name), $playlist_artwork_path, 'Refresh Library'); } else { // number of tracks has changed or playlist name has changed or the privacy has changed // update the playlist if ($playlists[2] != $tracks->total || $playlists[1] != escapeQuery($playlist->name) || ($playlists[11] == '' && $playlist->public == true || $playlists[11] == true && $playlist->public == '')) { $nb_updated_playlists++; // force refresh of playlist artwork getPlaylistArtwork($w, $playlist->uri, true, true); try { if ($playlists[1] != escapeQuery($playlist->name)) { $updatePlaylistsName = "update playlists set name=:name where uri=:uri"; $stmtUpdatePlaylistsName = $db->prepare($updatePlaylistsName); $stmtUpdatePlaylistsName->bindValue(':name', escapeQuery($playlist->name)); $stmtUpdatePlaylistsName->bindValue(':uri', $playlist->uri); $stmtUpdatePlaylistsName->execute(); } $stmtDeleteFromTracks->bindValue(':playlist_uri', $playlist->uri); $stmtDeleteFromTracks->execute(); } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $dbartworks = null; $db = null; return; } $tmp = explode(':', $playlist->uri); $duration_playlist = 0; $nb_track_playlist = 0; $offsetGetUserPlaylistTracks = 0; $limitGetUserPlaylistTracks = 100; do { $retry = true; $nb_retry = 0; while ($retry) { try { // refresh api $api = getSpotifyWebAPI($w, $api); $userPlaylistTracks = $api->getUserPlaylistTracks(urlencode($tmp[2]), $tmp[4], array('fields' => array('total', 'items(added_at)', 'items(is_local)', 'items.track(is_playable,duration_ms,uri,popularity,name)', 'items.track.album(album_type,images,uri,name)', 'items.track.artists(name,uri)'), 'limit' => $limitGetUserPlaylistTracks, 'offset' => $offsetGetUserPlaylistTracks, 'market' => $country_code)); $retry = false; } catch (SpotifyWebAPI\SpotifyWebAPIException $e) { logMsg("Error(getUserPlaylistTracks): retry " . $nb_retry . " (exception " . print_r($e) . ")"); if ($e->getCode() == 429 || $e->getCode() == 404 || $e->getCode() == 500 || $e->getCode() == 502 || $e->getCode() == 503 || $e->getCode() == 0) { // retry if ($nb_retry > 10) { handleSpotifyWebAPIException($w, $e); $retry = false; return false; } $nb_retry++; sleep(5); } else { handleSpotifyWebAPIException($w, $e); $retry = false; return false; } } } foreach ($userPlaylistTracks->items as $item) { $track = $item->track; $artists = $track->artists; $artist = $artists[0]; $album = $track->album; $playable = 0; $local_track = 0; if (isset($track->is_playable) && $track->is_playable) { $playable = 1; } if (isset($item->is_local) && $item->is_local) { $playable = 1; $local_track = 1; } try { // // Download artworks in Fetch later mode $thetrackuri = 'spotify:track:faketrackuri'; if ($local_track == 0 && isset($track->uri)) { $thetrackuri = $track->uri; } list($already_present, $track_artwork_path) = getTrackOrAlbumArtwork($w, $thetrackuri, true, true); if ($already_present == false) { $artworksToDownload = true; $stmtTrackArtwork->bindValue(':track_uri', $thetrackuri); $stmtTrackArtwork->bindValue(':already_fetched', 0); $stmtTrackArtwork->execute(); } $theartistname = 'fakeartist'; if (isset($artist->name)) { $theartistname = $artist->name; } list($already_present, $artist_artwork_path) = getArtistArtwork($w, $theartistname, true, true); if ($already_present == false) { $artworksToDownload = true; $stmtArtistArtwork->bindValue(':artist_name', $theartistname); $stmtArtistArtwork->bindValue(':already_fetched', 0); $stmtArtistArtwork->execute(); } $thealbumuri = 'spotify:album:fakealbumuri'; if (isset($album->uri)) { $thealbumuri = $album->uri; } list($already_present, $album_artwork_path) = getTrackOrAlbumArtwork($w, $thealbumuri, true, true); if ($already_present == false) { $artworksToDownload = true; $stmtAlbumArtwork->bindValue(':album_uri', $thealbumuri); $stmtAlbumArtwork->bindValue(':already_fetched', 0); $stmtAlbumArtwork->execute(); } } catch (PDOException $e) { logMsg("Error(updateLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($dbartworks, $w); $dbartworks = null; $db = null; return false; } $duration_playlist += $track->duration_ms; try { $stmtTrack->bindValue(':yourmusic', 0); $stmtTrack->bindValue(':popularity', $track->popularity); $stmtTrack->bindValue(':uri', $track->uri); $stmtTrack->bindValue(':album_uri', $album->uri); $stmtTrack->bindValue(':artist_uri', $artist->uri); $stmtTrack->bindValue(':track_name', escapeQuery($track->name)); $stmtTrack->bindValue(':album_name', escapeQuery($album->name)); $stmtTrack->bindValue(':artist_name', escapeQuery($artist->name)); $stmtTrack->bindValue(':album_type', $album->album_type); $stmtTrack->bindValue(':track_artwork_path', $track_artwork_path); $stmtTrack->bindValue(':artist_artwork_path', $artist_artwork_path); $stmtTrack->bindValue(':album_artwork_path', $album_artwork_path); $stmtTrack->bindValue(':playlist_name', escapeQuery($playlist->name)); $stmtTrack->bindValue(':playlist_uri', $playlist->uri); $stmtTrack->bindValue(':playable', $playable); $stmtTrack->bindValue(':added_at', $item->added_at); $stmtTrack->bindValue(':duration', beautifyTime($track->duration_ms / 1000)); $stmtTrack->bindValue(':nb_times_played', 0); $stmtTrack->bindValue(':local_track', $local_track); $stmtTrack->execute(); } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $dbartworks = null; $db = null; return; } $nb_track_playlist++; } $offsetGetUserPlaylistTracks += $limitGetUserPlaylistTracks; } while ($offsetGetUserPlaylistTracks < $userPlaylistTracks->total); try { $stmtUpdatePlaylistsNbTracks->bindValue(':nb_tracks', $userPlaylistTracks->total); $stmtUpdatePlaylistsNbTracks->bindValue(':nb_playable_tracks', $nb_track_playlist); $stmtUpdatePlaylistsNbTracks->bindValue(':duration_playlist', beautifyTime($duration_playlist / 1000, true)); $stmtUpdatePlaylistsNbTracks->bindValue(':uri', $playlist->uri); $stmtUpdatePlaylistsNbTracks->bindValue(':public', $playlist->public); $stmtUpdatePlaylistsNbTracks->execute(); } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $dbartworks = null; $db = null; return; } displayNotificationWithArtwork('Updated playlist ' . escapeQuery($playlist->name), getPlaylistArtwork($w, $playlist->uri, true), 'Refresh Library'); } else { continue; } } } try { // check for deleted playlists $getPlaylists = "select * from playlists"; $stmt = $db->prepare($getPlaylists); $stmt->execute(); while ($playlist_in_db = $stmt->fetch()) { $found = false; foreach ($savedListPlaylist as $playlist) { if ($playlist->uri == $playlist_in_db[0]) { $found = true; break; } } if ($found == false) { $nb_removed_playlists++; $deleteFromPlaylist = "delete from playlists where uri=:uri"; $stmtDelete = $db->prepare($deleteFromPlaylist); $stmtDelete->bindValue(':uri', $playlist_in_db[0]); $stmtDelete->execute(); $deleteFromTracks = "delete from tracks where playlist_uri=:uri"; $stmtDelete = $db->prepare($deleteFromTracks); $stmtDelete->bindValue(':uri', $playlist_in_db[0]); $stmtDelete->execute(); displayNotificationWithArtwork('Removed playlist ' . $playlist_in_db[1], getPlaylistArtwork($w, $playlist_in_db[0], false), 'Refresh Library'); } } } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $dbartworks = null; $db = null; return; } // check for update to Your Music $retry = true; $nb_retry = 0; while ($retry) { try { // refresh api $api = getSpotifyWebAPI($w, $api); // get only one, we just want to check total for now $userMySavedTracks = $api->getMySavedTracks(array('limit' => 1, 'offset' => 0)); $retry = false; } catch (SpotifyWebAPI\SpotifyWebAPIException $e) { logMsg("Error(getMySavedTracks): retry " . $nb_retry . " (exception " . print_r($e) . ")"); if ($e->getCode() == 429 || $e->getCode() == 404 || $e->getCode() == 500 || $e->getCode() == 502 || $e->getCode() == 503 || $e->getCode() == 0) { // retry if ($nb_retry > 10) { handleSpotifyWebAPIException($w, $e); $retry = false; return false; } $nb_retry++; sleep(5); } else { handleSpotifyWebAPIException($w, $e); $retry = false; return false; } } } try { // get current number of track in Your Music $getCount = 'select count(distinct uri) from tracks where yourmusic=1'; $stmt = $db->prepare($getCount); $stmt->execute(); $yourmusic_tracks = $stmt->fetch(); } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $db = null; return; } $your_music_updated = false; if ($yourmusic_tracks[0] != $userMySavedTracks->total) { $your_music_updated = true; // Your Music has changed, update it $nb_playlist++; $w->write('Refresh Library▹' . $nb_playlist . '▹' . $nb_playlist_total . '▹' . $words[3] . '▹' . 'Your Music', 'update_library_in_progress'); // delete tracks try { $stmtDeleteFromTracksYourMusic->bindValue(':yourmusic', 1); $stmtDeleteFromTracksYourMusic->execute(); } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $db = null; return; } $offsetGetMySavedTracks = 0; $limitGetMySavedTracks = 50; do { $retry = true; $nb_retry = 0; while ($retry) { try { // refresh api $api = getSpotifyWebAPI($w, $api); $userMySavedTracks = $api->getMySavedTracks(array('limit' => $limitGetMySavedTracks, 'offset' => $offsetGetMySavedTracks, 'market' => $country_code)); $retry = false; } catch (SpotifyWebAPI\SpotifyWebAPIException $e) { logMsg("Error(getMySavedTracks): retry " . $nb_retry . " (exception " . print_r($e) . ")"); if ($e->getCode() == 429 || $e->getCode() == 404 || $e->getCode() == 500 || $e->getCode() == 502 || $e->getCode() == 503 || $e->getCode() == 0) { // retry if ($nb_retry > 10) { handleSpotifyWebAPIException($w, $e); $retry = false; return false; } $nb_retry++; sleep(5); } else { handleSpotifyWebAPIException($w, $e); $retry = false; return false; } } } foreach ($userMySavedTracks->items as $item) { $track = $item->track; $artists = $track->artists; $artist = $artists[0]; $album = $track->album; $playable = 0; $local_track = 0; if (isset($track->is_playable) && $track->is_playable) { $playable = 1; } if (isset($item->is_local) && $item->is_local) { $playable = 1; $local_track = 1; } try { // // Download artworks in Fetch later mode $thetrackuri = 'spotify:track:faketrackuri'; if ($local_track == 0 && isset($track->uri)) { $thetrackuri = $track->uri; } list($already_present, $track_artwork_path) = getTrackOrAlbumArtwork($w, $thetrackuri, true, true); if ($already_present == false) { $artworksToDownload = true; $stmtTrackArtwork->bindValue(':track_uri', $thetrackuri); $stmtTrackArtwork->bindValue(':already_fetched', 0); $stmtTrackArtwork->execute(); } $theartistname = 'fakeartist'; if (isset($artist->name)) { $theartistname = $artist->name; } list($already_present, $artist_artwork_path) = getArtistArtwork($w, $theartistname, true, true); if ($already_present == false) { $artworksToDownload = true; $stmtArtistArtwork->bindValue(':artist_name', $theartistname); $stmtArtistArtwork->bindValue(':already_fetched', 0); $stmtArtistArtwork->execute(); } $thealbumuri = 'spotify:album:fakealbumuri'; if (isset($album->uri)) { $thealbumuri = $album->uri; } list($already_present, $album_artwork_path) = getTrackOrAlbumArtwork($w, $thealbumuri, true, true); if ($already_present == false) { $artworksToDownload = true; $stmtAlbumArtwork->bindValue(':album_uri', $thealbumuri); $stmtAlbumArtwork->bindValue(':already_fetched', 0); $stmtAlbumArtwork->execute(); } } catch (PDOException $e) { logMsg("Error(updateLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($dbartworks, $w); $dbartworks = null; $db = null; return false; } try { $stmtTrack->bindValue(':yourmusic', 1); $stmtTrack->bindValue(':popularity', $track->popularity); $stmtTrack->bindValue(':uri', $track->uri); $stmtTrack->bindValue(':album_uri', $album->uri); $stmtTrack->bindValue(':artist_uri', $artist->uri); $stmtTrack->bindValue(':track_name', escapeQuery($track->name)); $stmtTrack->bindValue(':album_name', escapeQuery($album->name)); $stmtTrack->bindValue(':artist_name', escapeQuery($artist->name)); $stmtTrack->bindValue(':album_type', $album->album_type); $stmtTrack->bindValue(':track_artwork_path', $track_artwork_path); $stmtTrack->bindValue(':artist_artwork_path', $artist_artwork_path); $stmtTrack->bindValue(':album_artwork_path', $album_artwork_path); $stmtTrack->bindValue(':playlist_name', ''); $stmtTrack->bindValue(':playlist_uri', ''); $stmtTrack->bindValue(':playable', $playable); $stmtTrack->bindValue(':added_at', $item->added_at); $stmtTrack->bindValue(':duration', beautifyTime($track->duration_ms / 1000)); $stmtTrack->bindValue(':nb_times_played', 0); $stmtTrack->bindValue(':local_track', $local_track); $stmtTrack->execute(); } catch (PDOException $e) { logMsg("Error(refreshLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $db = null; return; } } $offsetGetMySavedTracks += $limitGetMySavedTracks; } while ($offsetGetMySavedTracks < $userMySavedTracks->total); } // update counters try { $getCount = 'select count(distinct uri) from tracks'; $stmt = $db->prepare($getCount); $stmt->execute(); $all_tracks = $stmt->fetch(); $getCount = 'select count(distinct uri) from tracks where yourmusic=1'; $stmt = $db->prepare($getCount); $stmt->execute(); $yourmusic_tracks = $stmt->fetch(); $getCount = 'select count(distinct artist_name) from tracks'; $stmt = $db->prepare($getCount); $stmt->execute(); $all_artists = $stmt->fetch(); $getCount = 'select count(distinct artist_name) from tracks where yourmusic=1'; $stmt = $db->prepare($getCount); $stmt->execute(); $yourmusic_artists = $stmt->fetch(); $getCount = 'select count(distinct album_name) from tracks'; $stmt = $db->prepare($getCount); $stmt->execute(); $all_albums = $stmt->fetch(); $getCount = 'select count(distinct album_name) from tracks where yourmusic=1'; $stmt = $db->prepare($getCount); $stmt->execute(); $yourmusic_albums = $stmt->fetch(); $getCount = 'select count(*) from playlists'; $stmt = $db->prepare($getCount); $stmt->execute(); $playlists_count = $stmt->fetch(); $insertCounter = "insert into counters values (:all_tracks,:yourmusic_tracks,:all_artists,:yourmusic_artists,:all_albums,:yourmusic_albums,:playlists)"; $stmt = $db->prepare($insertCounter); $stmt->bindValue(':all_tracks', $all_tracks[0]); $stmt->bindValue(':yourmusic_tracks', $yourmusic_tracks[0]); $stmt->bindValue(':all_artists', $all_artists[0]); $stmt->bindValue(':yourmusic_artists', $yourmusic_artists[0]); $stmt->bindValue(':all_albums', $all_albums[0]); $stmt->bindValue(':yourmusic_albums', $yourmusic_albums[0]); $stmt->bindValue(':playlists', $playlists_count[0]); $stmt->execute(); } catch (PDOException $e) { logMsg("Error(updateLibrary): (exception " . print_r($e) . ")"); handleDbIssuePdoEcho($db, $w); $dbartworks = null; $db = null; return false; } $elapsed_time = time() - $words[3]; $changedPlaylists = false; $changedYourMusic = false; $addedMsg = ''; $removedMsg = ''; $updatedMsg = ''; $yourMusicMsg = ''; if ($nb_added_playlists > 0) { $addedMsg = $nb_added_playlists . ' added'; $changedPlaylists = true; } if ($nb_removed_playlists > 0) { $removedMsg = $nb_removed_playlists . ' removed'; $changedPlaylists = true; } if ($nb_updated_playlists > 0) { $updatedMsg = $nb_updated_playlists . ' updated'; $changedPlaylists = true; } if ($your_music_updated) { $yourMusicMsg = ' - Your Music: updated'; $changedYourMusic = true; } if ($changedPlaylists && $changedYourMusic) { $message = 'Playlists: ' . $addedMsg . ' ' . $removedMsg . ' ' . $updatedMsg . ' ' . $yourMusicMsg; } elseif ($changedPlaylists) { $message = 'Playlists: ' . $addedMsg . ' ' . $removedMsg . ' ' . $updatedMsg; } elseif ($changedYourMusic) { $message = $yourMusicMsg; } else { $message = 'No change'; } displayNotificationWithArtwork($message . " - took " . beautifyTime($elapsed_time, true), './images/update.png', 'Library refreshed'); if (file_exists($w->data() . '/library_old.db')) { deleteTheFile($w->data() . '/library_old.db'); } rename($w->data() . '/library_new.db', $w->data() . '/library.db'); // Download artworks in background logMsg("========DOWNLOAD_ARTWORKS DURING REFRESH LIBRARY ========"); exec("php -f ./src/action.php -- \"\" \"DOWNLOAD_ARTWORKS\" \"DOWNLOAD_ARTWORKS\" >> \"" . $w->cache() . "/action.log\" 2>&1 & "); deleteTheFile($w->data() . '/update_library_in_progress'); }