Skip to content

Instantly share code, notes, and snippets.

@stuartd
Created May 1, 2019 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartd/941ea08b21eca6d470f1da8b7255743f to your computer and use it in GitHub Desktop.
Save stuartd/941ea08b21eca6d470f1da8b7255743f to your computer and use it in GitHub Desktop.
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