@@ -81,9 +81,9 @@ public function crawlAll(string $username): void
81
81
$ session = $ spotifySession ->getUnderlyingObject ();
82
82
$ spotifyWebApi = $ this ->factory ->getSpotifyWebAPI ($ session );
83
83
84
- logs ('crawler ' )->debug ("starting spotify crawler for username $ username " );
84
+ logs ('crawler ' )->info ("starting spotify crawler for username $ username " );
85
85
$ this ->crawlTrackHistoryAndAudioFeatures ($ username , $ spotifyWebApi );
86
- logs ('crawler ' )->debug ("finished spotify crawler for username $ username " );
86
+ logs ('crawler ' )->info ("finished spotify crawler for username $ username " );
87
87
88
88
$ this ->sessionHandler ->saveSession ($ spotifySession , $ username );
89
89
}
@@ -94,10 +94,10 @@ public function crawlAll(string $username): void
94
94
protected function crawlTrackHistoryAndAudioFeatures (string $ username , SpotifyWebAPI $ spotifyWebApi ): void
95
95
{
96
96
// TODO add "after" instead of limit if last crawl was last hour
97
- $ recentTracks = $ spotifyWebApi ->getMyRecentTracks ([
97
+ $ recentTracks = $ spotifyWebApi ->getMyRecentTracks ([
98
98
'limit ' => config ('services.spotify.crawl_bulk_limit ' ),
99
99
])->items ;
100
- logs ('crawler ' )->debug ("retrieved recent logs for user $ username " );
100
+ logs ('crawler ' )->info ("retrieved recent logs for user $ username " );
101
101
102
102
$ recentTracksIds = [];
103
103
foreach ($ recentTracks as $ recentTrack ) {
@@ -112,12 +112,13 @@ protected function crawlTrackHistoryAndAudioFeatures(string $username, SpotifyWe
112
112
$ artistIds [$ artist ->id ] = $ artist ->id ;
113
113
}
114
114
}
115
-
115
+ logs ( ' crawler ' )-> debug ( " getting artists for user $ username " );
116
116
$ artistsById = $ this ->getArtistsById ($ spotifyWebApi , $ artistIds );
117
+ logs ('crawler ' )->debug ("getting audio features for user $ username " );
117
118
$ audioFeatures = $ this ->getAudioFeatures ($ spotifyWebApi , $ recentTracksIds );
118
-
119
+ logs ( ' crawler ' )-> debug ( " writing history for user $ username " );
119
120
foreach ($ recentTracks as $ recentTrack ) {
120
- $ this ->writeTrackHistoryPoint ($ audioFeatures [$ recentTrack ->track ->id ], $ recentTrack , $ username );
121
+ $ this ->writeTrackHistoryPoint ($ audioFeatures [$ recentTrack ->track ->id ] ?? null , $ recentTrack , $ username );
121
122
$ this ->writeGenreHistoryPoints ($ recentTrack , $ artistsById , $ username );
122
123
}
123
124
@@ -132,13 +133,17 @@ protected function crawlTrackHistoryAndAudioFeatures(string $username, SpotifyWe
132
133
*/
133
134
protected function getArtistsById (SpotifyWebAPI $ spotifyWebApi , array $ artistIds ): array
134
135
{
136
+ logs ('crawler ' )->debug ("getting artists for user $ username " );
135
137
$ artistsFromAPI = $ this ->getCachedArtistsAndCleanupIds ($ artistIds );
138
+
136
139
$ cache = Cache::tags ([
137
140
ServiceEnum::Spotify->value ,
138
141
ServiceEnum::Spotify->value . CacheKeyEnum::CACHE_KEY_SEPARATOR . CacheKeyEnum::Artist->value
139
142
]);
140
143
141
- if (count ($ artistIds ) > 0 ) {
144
+ $ newArtistIdCount = count ($ artistIds );
145
+ if (newArtistIdCount > 0 ) {
146
+ logs ('crawler ' )->debug ("retrieving uncached data for $ newArtistIdCount artists " );
142
147
// artistIds count could be more than crawl_bulk_limit
143
148
$ artistIdsChunks = array_chunk ($ artistIds , config ('services.spotify.crawl_bulk_limit ' ));
144
149
@@ -211,13 +216,16 @@ protected function getAudioFeatures(SpotifyWebAPI $spotifyWebApi, array $trackId
211
216
logs ('crawler ' )->debug ("found audio feature for $ trackId in cache " );
212
217
}
213
218
}
214
-
215
219
if (count ($ trackIds ) > 0 ) {
216
- $ response = $ spotifyWebApi ->getMultipleAudioFeatures (array_values ($ trackIds ));
217
- foreach ($ response ->audio_features as $ audioFeature ) {
218
- $ cache ->put ($ audioFeature ->id , $ audioFeature , config ('services.spotify.cache_ttl ' ));
219
- $ audioFeatures [$ audioFeature ->id ] = $ audioFeature ;
220
- logs ('crawler ' )->debug ("set audio feature for $ audioFeature ->id to cache " );
220
+ try {
221
+ $ response = $ spotifyWebApi ->getMultipleAudioFeatures (array_values ($ trackIds ));
222
+ foreach ($ response ->audio_features as $ audioFeature ) {
223
+ $ cache ->put ($ audioFeature ->id , $ audioFeature , config ('services.spotify.cache_ttl ' ));
224
+ $ audioFeatures [$ audioFeature ->id ] = $ audioFeature ;
225
+ logs ('crawler ' )->debug ("set audio feature for $ audioFeature ->id to cache " );
226
+ }
227
+ } catch (Exception $ e ) {
228
+ logs ('crawler ' )->warning ('error while fetching audio feature, skipping. Exception: ' . $ e ->getMessage ());
221
229
}
222
230
}
223
231
@@ -227,7 +235,7 @@ protected function getAudioFeatures(SpotifyWebAPI $spotifyWebApi, array $trackId
227
235
/**
228
236
* @throws Exception
229
237
*/
230
- protected function writeTrackHistoryPoint (stdClass $ audioFeature , stdClass $ track , string $ username ): void
238
+ protected function writeTrackHistoryPoint (? stdClass $ audioFeature , stdClass $ track , string $ username ): void
231
239
{
232
240
$ point = $ this ->factory ->getTrackHistoryPoint (
233
241
$ username , ServiceEnum::Spotify->value , $ audioFeature , $ track
0 commit comments