예제 #1
0
        public async Task <int> ProcessRecentTracks()
        {
            int processed = 0;
            var lib       = new LastFMLibrary();

            try
            {
                using (var app = new iTunesLibrary())
                {
                    var itunes = app.GetLibraryAsDictionary();

                    // Process 10 pages
                    for (int i = 1; i <= 10; i++)
                    {
                        // Start with the list of recent tracks, which includes last played date
                        // Make a call to track.getInfo to get the total user play count
                        var tracks = await lib.GetRecentTracks(Settings.Default.LastFMUser, i, 200);

                        if (tracks == null)
                        {
                            break;
                        }

                        foreach (var t2 in tracks.OrderByDescending(x => x.PlayDate))
                        {
                            IITTrack  track;
                            IRevTrack playCount;
                            DateTime? playDate = t2.PlayDate;

                            try
                            {
                                track =
                                    itunes[iTunesLibrary.SelectKey(t2.Artist, t2.Album ?? string.Empty, t2.Song)];
                            }
                            catch (KeyNotFoundException)
                            {
                                var orig = Console.ForegroundColor;
                                Console.ForegroundColor = ConsoleColor.Cyan;
                                Console.WriteLine(
                                    $"Need to buy? {t2.Album} {t2.Song} by {t2.Artist}");
                                Console.ForegroundColor = orig;
                                continue;
                            }

                            if (playDate != null && TrackRecord.Instance.IsCurrent(
                                    track.Name, track.Album, playDate.Value.ToUniversalTime()))
                            {
                                Console.WriteLine("I listen to this song a lot...");
                                continue;
                            }

                            try
                            {
                                if (t2.Song.Length > 36 && t2.MBId.Length == 36)
                                {
                                    playCount = await lib.GetTrackAsync(t2.MBId);
                                }
                                else
                                {
                                    playCount = await lib.GetTrackAsync(t2.Artist, t2.Song);
                                }
                            }
                            catch (RuntimeBinderException error)
                            {
                                Console.WriteLine(error.ToString());
                                continue;
                            }

                            UpdateTrackCountAndDate(playCount, track, playDate);
                            processed++;
                        }
                    }
                }
            }
            catch (COMException exception)
            {
                if (exception.Message.Contains("CO_E_SERVER_EXEC_FAILURE"))
                {
                    Console.WriteLine("Server busy error from iTunes.");
                }
            }

            return(processed);
        }
예제 #2
0
        public async Task <int> ProcessLibrary()
        {
            int processed = 0;
            var lib       = new LastFMLibrary();

            try
            {
                using (var app = new iTunesLibrary())
                {
                    var itunes = app.GetLibraryWithoutDuplicates().OrderBy(x => x.PlayedDate);
                    foreach (IITTrack track in itunes)
                    {
                        Console.WriteLine("iTunes: " + track.Name + " by " + track.Artist);

                        IRevTrack playCount;
                        IRevTrack playDate;

                        // Call user.getArtistTracks to get last played time
                        try
                        {
                            var artistScrobbles = await lib.GetArtistScrobbles(track.Artist, Settings.Default.LastFMUser);

                            playDate = artistScrobbles.OrderByDescending(p => p.PlayDate)
                                       .FirstOrDefault(t => string.Equals(t.Song, track.Name,
                                                                          StringComparison.CurrentCultureIgnoreCase));
                        }
                        catch (RuntimeBinderException error)
                        {
                            Console.WriteLine(error.ToString());
                            continue;
                        }

                        // Check if this is a duplicate.
                        if (playDate != null && TrackRecord.Instance.IsCurrent(
                                track.Name, track.Album, playDate.PlayDate.GetValueOrDefault().ToUniversalTime()))
                        {
                            Console.WriteLine("I listen to this song a lot...");
                            continue;
                        }

                        // Make a call to track.getInfo to get the total user play count
                        try
                        {
                            playCount = await lib.GetTrackAsync(track.Artist, track.Name);
                        }
                        catch (RuntimeBinderException error)
                        {
                            Console.WriteLine(error.ToString());
                            continue;
                        }

                        if (playCount == null)
                        {
                            continue;
                        }

                        UpdateTrackCountAndDate(playCount, track, playDate?.PlayDate);

                        processed++;
                    }
                }
            }
            catch (COMException exception)
            {
                if (exception.Message.Contains("CO_E_SERVER_EXEC_FAILURE"))
                {
                    Console.WriteLine("Server busy error from iTunes.");
                }
            }

            return(processed);
        }