/// <summary> /// Process the echo command and return a response identical to the request. /// </summary> /// <param name="command"> /// The command to process. /// </param> /// <returns> /// The response to the request. /// </returns> public override byte[] ProcessCommand(Command command) { // Check parameters if (!(command is ArtworkGet)) { throw new ArgumentException("Command is not an artwork get command"); } ArtworkGet request = command as ArtworkGet; // Create an empty response object ArtworkGetResponse response = new ArtworkGetResponse(); // Try to retrieve artist image if requested if ((!string.IsNullOrEmpty(request.Artist)) && (request.GetArtistImage)) { AudioArtwork artwork = AudioArtworkFactory.ApplicationInstance.GetForArtist(request.Artist); if (artwork != null) { response.ArtistImage = artwork.Load(); response.ArtistImageAvailable = response.ArtistImage != null; } } // Try to retrieve album image if request if ((!string.IsNullOrEmpty(request.Artist)) && (!string.IsNullOrEmpty(request.Album)) && (request.GetAlbumImage)) { AudioArtwork artwork = AudioArtworkFactory.ApplicationInstance.GetForAlbum(request.Artist, request.Album); if (artwork != null) { response.AlbumImage = artwork.Load(); response.AlbumImageAvailable = response.AlbumImage != null; } } return(ToJsonBuffer(response)); }
private void DiscoveryThread() { LastFmClient client = new LastFmClient(); while (true) { try { // Find artist/album details that we require AudioFile[] audioFiles = AudioFileFactory.ApplicationInstance.FilesRequiringArtworkScan(); if (audioFiles == null) { Logger.Debug("No files requiring artwork checks, waiting 30 seconds"); Thread.Sleep(30000); continue; } foreach (AudioFile audioFile in audioFiles) { // Determine what (if any) artwork we have for these items AudioArtwork artistArtwork = string.IsNullOrEmpty(audioFile.Artist) ? null : AudioArtworkFactory.ApplicationInstance.GetForArtist(audioFile.Artist); AudioArtwork albumArtwork = string.IsNullOrEmpty(audioFile.Artist) || string.IsNullOrEmpty(audioFile.Album) ? null : AudioArtworkFactory.ApplicationInstance.GetForAlbum(audioFile.Artist, audioFile.Album); // Get artist artwork if required if ((artistArtwork == null) && (!string.IsNullOrEmpty(audioFile.Artist))) { try { // Make a request for the artist artwork and create appropriate // artistArtwork object Logger.Debug("Searching for artist artwork for " + audioFile.Artist); using (Bitmap artistImage = client.GetArtistImage(audioFile.Artist)) { AudioArtwork artwork = new AudioArtwork { Artist = audioFile.Artist }; AudioArtworkFactory.ApplicationInstance.Create(artwork); bool hasImage = false; if (artistImage == null) { Logger.Info("No artist image for " + audioFile.Artist + " available"); } else { Logger.Info("Found artist image for " + audioFile.Artist); artwork.Save(artistImage); hasImage = true; } // Now trigger a notification indicating this artwork is available Controller.NotificationNetworkServer.SendNotification(new AudioArtworkAvailableNotification { Artist = audioFile.Artist, ImageContent = hasImage ? artwork.Load() : null }); } } catch (Exception ex) { Logger.Info("Failed to get album artwork for " + audioFile.Artist + ": " + ex.Message); } } // Get album artwork if required if ((albumArtwork == null) && (!string.IsNullOrEmpty(audioFile.Artist)) && (!string.IsNullOrEmpty(audioFile.Album))) { try { // Make a request for the artist artwork and create appropriate // artistArtwork object Logger.Debug("Searching for artist artwork for " + audioFile.Artist + " - " + audioFile.Album); using (Bitmap albumImage = client.GetAlbumImage(audioFile.Artist, audioFile.Album)) { AudioArtwork artwork = new AudioArtwork { Artist = audioFile.Artist, Album = audioFile.Album }; AudioArtworkFactory.ApplicationInstance.Create(artwork); bool hasImage = false; if (albumImage == null) { Logger.Info("No album image for " + audioFile.Artist + " - " + audioFile.Album + " available"); } else { Logger.Info("Found artist image for " + audioFile.Artist + " - " + audioFile.Album); artwork.Save(albumImage); hasImage = true; } // Now trigger a notification indicating this artwork is available Controller.NotificationNetworkServer.SendNotification(new AudioArtworkAvailableNotification { Artist = audioFile.Artist, Album = audioFile.Album, ImageContent = hasImage ? artwork.Load() : null }); } } catch (Exception ex) { Logger.Info("Failed to get album artwork for " + audioFile.Artist + " - " + audioFile.Album + ": " + ex.Message); } } // Update this audio file as checked audioFile.ArtworkSearchDate = DateTime.Now; AudioFileFactory.ApplicationInstance.Update(audioFile); Thread.Sleep(1000); } Thread.Sleep(5000); } catch (Exception ex) { Logger.Error("Error in artwork discovery loop", ex); } Thread.Sleep(500); } }