Created
May 1, 2019 14:30
-
-
Save stuartd/941ea08b21eca6d470f1da8b7255743f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using iTunesLib; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
// Must NOT be run as admin | |
static readonly List<Track> library = new List<Track>(); | |
static void Main(string[] args) { | |
var type = Type.GetTypeFromCLSID(new Guid("DC0C2640-1415-4644-875C-6F4D769839BA"), true); | |
var app = Activator.CreateInstance(type) as iTunesApp; | |
Console.WriteLine("Loading"); | |
var sw = Stopwatch.StartNew(); | |
var enumerator = app.LibraryPlaylist.Tracks.GetEnumerator(); | |
while (enumerator.MoveNext()) { | |
ProcessTrack(enumerator.Current as IITTrack); | |
} | |
sw.Stop(); | |
Console.WriteLine($"Loaded in {sw.Elapsed.TotalSeconds} seconds"); | |
GetAlbumsWithOnlyOneSong(); | |
// GetAlbumsWithMultipleGenres(); | |
Console.ReadLine(); | |
} | |
private static void GetAlbumsWithOnlyOneSong() { | |
var albums = library.Select(l => l.Album).Distinct(); | |
foreach (var album in albums) { | |
var albumTracks = library.Where(t => t.Album == album); | |
if (albumTracks.Count() == 1) { | |
Console.WriteLine($"{album} - {albumTracks.Single().Artist} - {albumTracks.Single().Name}"); | |
} | |
} | |
} | |
private static void GetAlbumsWithMultipleGenres() { | |
// So for example, get albums with multiple genres | |
var albums = library.Select(l => l.Album).Distinct(); | |
foreach (var album in albums) { | |
var albumTracks = library.Where(t => t.Album == album); | |
var albumGenres = albumTracks.Select(t => t.Genre).GroupBy(t => t); | |
if (albumGenres.Count() > 1) { | |
string genres = string.Join(",", albumGenres.Select(g => g.Key + ":" + g.Count())); | |
Console.WriteLine($"Album {album} has multiple genres: {genres}"); | |
Console.WriteLine(); | |
} | |
} | |
} | |
private static void ProcessTrack(IITTrack track) { | |
if (track.Kind == ITTrackKind.ITTrackKindSharedLibrary) { | |
return; | |
} | |
library.Add(new Track { | |
Album = track.Album, | |
Artist = track.Artist, | |
Genre = track.Genre, | |
Name = track.Name | |
}); | |
} | |
private class Track { | |
public string Album { get; set; } | |
public string Artist { get; set; } | |
public string Genre { get; set; } | |
public string Name { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment