private async Task SetAlbumArtwork(IList <AlbumArtwork> allAlbumArtwork, IList <string> albumsKeys = null) { if (this.albums != null && this.albums.Count > 0) { await Task.Run(() => { foreach (AlbumViewModel alb in this.albums) { try { if (allAlbumArtwork != null && allAlbumArtwork.Count > 0 && albumsKeys != null ? albumsKeys.Contains(alb.AlbumKey) : true) { AlbumArtwork albumArtwork = allAlbumArtwork.Where(a => a.AlbumKey.Equals(alb.AlbumKey)).FirstOrDefault(); if (albumArtwork != null) { alb.ArtworkPath = this.cacheService.GetCachedArtworkPath(albumArtwork.ArtworkID); } } } catch (Exception ex) { LogClient.Error("Error while refreshing artwork for Album {0}/{1}. Exception: {2}", alb.AlbumTitle, alb.AlbumArtist, ex.Message); } } }); } }
private void UpdateAlbumViewModel(int number, IList <AlbumData> albumDatas, ref AlbumViewModel albumViewModel) { if (albumDatas.Count >= number) { AlbumData data = albumDatas[number - 1]; if (albumViewModel == null || !albumViewModel.AlbumKey.Equals(data.AlbumKey)) { Task <AlbumArtwork> task = this.albumArtworkRepository.GetAlbumArtworkAsync(data.AlbumKey); AlbumArtwork albumArtwork = task.Result; albumViewModel = new AlbumViewModel(data, true) { ArtworkPath = this.cacheService.GetCachedArtworkPath(albumArtwork.ArtworkID) }; } } else { // Shows an empty tile albumViewModel = new AlbumViewModel(AlbumData.CreateDefault(), false) { ArtworkPath = string.Empty, Opacity = 0.8 - (number / 10.0) }; } RaisePropertyChanged("AlbumViewModel" + number.ToString()); System.Threading.Thread.Sleep(Constants.CloudLoadDelay); }
public async Task <AlbumArtwork> GetAlbumArtworkForPathAsync(string path) { AlbumArtwork albumArtwork = null; await Task.Run(() => { try { using (var conn = this.factory.GetConnection()) { try { albumArtwork = conn.Query <AlbumArtwork>("SELECT * FROM AlbumArtwork a LEFT JOIN Track t ON a.AlbumKey = t.AlbumKey WHERE t.SafePath=?;", path.ToSafePath()).FirstOrDefault(); } catch (Exception ex) { LogClient.Error($"Could not get album artwork for path '{path}'. Exception: {ex.Message}"); } } } catch (Exception ex) { LogClient.Error("Could not connect to the database. Exception: {0}", ex.Message); } }); return(albumArtwork); }
public async Task <AlbumArtwork> GetAlbumArtworkAsync(string albumKey) { AlbumArtwork albumArtwork = null; await Task.Run(() => { try { using (var conn = this.factory.GetConnection()) { try { albumArtwork = conn.Query <AlbumArtwork>("SELECT * FROM AlbumArtwork WHERE AlbumKey=?;", albumKey).FirstOrDefault(); } catch (Exception ex) { LogClient.Error("Could not get album artwork. Exception: {0}", ex.Message); } } } catch (Exception ex) { LogClient.Error("Could not connect to the database. Exception: {0}", ex.Message); } }); return(albumArtwork); }
protected void Dispose(bool disposing) { if (disposing) { _trackFetcherTimer.Stop(); _trackFetcherTimer?.Dispose(); AlbumArtwork?.Dispose(); client?.Dispose(); httpClient?.Dispose(); } }
private byte[] GetAlbumArtwork(string filename, int size) { byte[] artwork = null; Task <AlbumArtwork> task = this.albumArtworkRepository.GetAlbumArtworkForPathAsync(filename); AlbumArtwork albumArtwork = task.Result; if (albumArtwork != null) { string artworkPath = this.cacheService.GetCachedArtworkPath(albumArtwork.ArtworkID); if (!string.IsNullOrEmpty(artworkPath)) { artwork = ImageUtils.Image2ByteArray(artworkPath, size, size); } } return(artwork); }
private async Task AddArtworkInBackgroundAsync(int passNumber) { LogClient.Info("+++ STARTED ADDING ARTWORK IN THE BACKGROUND +++"); this.canIndexArtwork = true; this.isIndexingArtwork = true; DateTime startTime = DateTime.Now; await Task.Run(async() => { using (SQLiteConnection conn = this.factory.GetConnection()) { try { IList <string> albumKeysWithArtwork = new List <string>(); IList <AlbumData> albumDatasToIndex = await this.trackRepository.GetAlbumDataToIndexAsync(); foreach (AlbumData albumDataToIndex in albumDatasToIndex) { // Check if we must cancel artwork indexing if (!this.canIndexArtwork) { try { LogClient.Info("+++ ABORTED ADDING ARTWORK IN THE BACKGROUND. Time required: {0} ms +++", Convert.ToInt64(DateTime.Now.Subtract(startTime).TotalMilliseconds)); this.AlbumArtworkAdded(this, new AlbumArtworkAddedEventArgs() { AlbumKeys = albumKeysWithArtwork }); // Update UI } catch (Exception ex) { LogClient.Error("Failed to commit changes while aborting adding artwork in background. Exception: {0}", ex.Message); } this.isIndexingArtwork = false; return; } // Start indexing artwork try { // Delete existing AlbumArtwork await this.albumArtworkRepository.DeleteAlbumArtworkAsync(albumDataToIndex.AlbumKey); // Create a new AlbumArtwork var albumArtwork = new AlbumArtwork(albumDataToIndex.AlbumKey); if (passNumber.Equals(1)) { // During the 1st pass, look for artwork in file(s). // Only set NeedsAlbumArtworkIndexing = 0 if artwork was found. So when no artwork was found, // this gives the 2nd pass a chance to look for artwork on the Internet. albumArtwork.ArtworkID = await this.GetArtworkFromFile(albumDataToIndex.AlbumKey); if (!string.IsNullOrEmpty(albumArtwork.ArtworkID)) { await this.trackRepository.DisableNeedsAlbumArtworkIndexingAsync(albumDataToIndex.AlbumKey); } } else if (passNumber.Equals(2)) { // During the 2nd pass, look for artwork on the Internet and set NeedsAlbumArtworkIndexing = 0. // We don't want future passes to index for this AlbumKey anymore. albumArtwork.ArtworkID = await this.GetArtworkFromInternet( albumDataToIndex.AlbumTitle, DataUtils.SplitAndTrimColumnMultiValue(albumDataToIndex.AlbumArtists).ToList(), albumDataToIndex.TrackTitle, DataUtils.SplitAndTrimColumnMultiValue(albumDataToIndex.Artists).ToList() ); await this.trackRepository.DisableNeedsAlbumArtworkIndexingAsync(albumDataToIndex.AlbumKey); } // If artwork was found, keep track of the albumID if (!string.IsNullOrEmpty(albumArtwork.ArtworkID)) { albumKeysWithArtwork.Add(albumArtwork.AlbumKey); conn.Insert(albumArtwork); } // If artwork was found for 20 albums, trigger a refresh of the UI. if (albumKeysWithArtwork.Count >= 20) { var eventAlbumKeys = new List <string>(albumKeysWithArtwork); albumKeysWithArtwork.Clear(); this.AlbumArtworkAdded(this, new AlbumArtworkAddedEventArgs() { AlbumKeys = eventAlbumKeys }); // Update UI } } catch (Exception ex) { LogClient.Error("There was a problem while updating the cover art for Album {0}/{1}. Exception: {2}", albumDataToIndex.AlbumTitle, albumDataToIndex.AlbumArtists, ex.Message); } } try { this.AlbumArtworkAdded(this, new AlbumArtworkAddedEventArgs() { AlbumKeys = albumKeysWithArtwork }); // Update UI } catch (Exception ex) { LogClient.Error("Failed to commit changes while finishing adding artwork in background. Exception: {0}", ex.Message); } } catch (Exception ex) { LogClient.Error("Unexpected error occurred while updating artwork in the background. Exception: {0}", ex.Message); } } }); this.isIndexingArtwork = false; LogClient.Error("+++ FINISHED ADDING ARTWORK IN THE BACKGROUND. Time required: {0} ms +++", Convert.ToInt64(DateTime.Now.Subtract(startTime).TotalMilliseconds)); }
public async Task GetAsync() { try { if (client == null) { return; } var currentTrack = await client.GetPlayingTrackAsync(); if (currentTrack.IsPlaying) { var devices = (await client.GetDevicesAsync()).Devices; if (devices.Except(AvailableSpotifyDevices).Any()) { AvailableSpotifyDevices = devices; } var currentActiveDevice = devices.Find(x => x.IsActive); if (ActiveDevice.Id != currentActiveDevice.Id) { ActiveDevice = _deiceMapper.Map <SpotifyDevice, Device>(currentActiveDevice); DeviceChanged?.Invoke(ActiveDevice); } if (currentTrack.Item != null) { Duration_ms = currentTrack.Item.DurationMs; Position_ms = currentTrack.ProgressMs; Volume = currentActiveDevice.VolumePercent; var testURL = currentTrack.Item.ExternUrls.FirstOrDefault(); var url = testURL.Equals(default(KeyValuePair <string, string>)) ? string.Empty : testURL.Value; if (Url != url) { Url = url; AlbumArtwork?.Dispose(); AlbumArtwork = null; } if (AlbumArtwork == null) { var artist = await client.GetArtistAsync(currentTrack.Item.Artists.FirstOrDefault()?.Id); if (artist != null) { Genre = artist.Genres.FirstOrDefault(); Type = artist.Type; } Track = currentTrack.Item.Name; Album = currentTrack.Item.Album.Name; var mainArtist = currentTrack.Item.Artists.First().Name; if (currentTrack.Item.Artists.Count() > 1) { var artistFeat = $@"{mainArtist} feat. "; artistFeat += string.Join(", ", currentTrack.Item.Artists.Skip(1).Select(x => x.Name)); Artist = artistFeat; } else { Artist = mainArtist; } //Lyrics = await LyricsHelpers.GetLyricsAsync(mainArtist, this.Track); if (currentTrack.Item.Album.Images.Any()) { ArtworkURL = currentTrack.Item.Album.Images[0].Url; AlbumArtwork = await GetImageAsync(ArtworkURL); } else { AlbumArtwork = new Bitmap(1, 1); } TrackChanged?.Invoke(this); } ; } else { Track = "Loading..."; Artist = "Loading..."; Album = "Loading..."; Duration_ms = 0; Position_ms = 0; Volume = 0; TrackChanged?.Invoke(this); } } if (IsPlaying != currentTrack.IsPlaying) { TrackPlayStateChanged?.Invoke(currentTrack.IsPlaying ? PlayState.Play : PlayState.Pause); } IsPlaying = currentTrack.IsPlaying; TrackDurationChanged?.Invoke(this); } catch (Exception ex) { Console.WriteLine(ex); } }