forked from triplef/LastHistory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LHiTunesLibrary.m
158 lines (128 loc) · 3.62 KB
/
LHiTunesLibrary.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// LHiTunesLibrary.m
// LastHistory
//
// Created by Frederik Seiffert on 08.11.09.
// Copyright 2009 Frederik Seiffert. All rights reserved.
//
#import "LHiTunesLibrary.h"
#import "SBiTunes.h"
@implementation LHiTunesLibrary
@synthesize libraryURL=_libraryURL;
+ (LHiTunesLibrary *)defaultLibrary
{
static id defaultLibrary = nil;
if (!defaultLibrary) {
[[NSUserDefaults standardUserDefaults] synchronize];
NSArray *dbs = [[NSUserDefaults standardUserDefaults] persistentDomainForName:@"com.apple.iApps"][@"iTunesRecentDatabases"];
if (dbs.count > 0) {
NSURL *url = [NSURL URLWithString:dbs[0]];
if (url.fileURL)
defaultLibrary = [[self alloc] initWithURL:url];
}
}
return defaultLibrary;
}
- (instancetype)initWithURL:(NSURL *)libraryURL
{
self = [super init];
if (self != nil) {
_libraryURL = [libraryURL retain];
}
return self;
}
- (NSDictionary *)tracks
{
if (_tracks)
return _tracks;
NSURL *libraryURL = self.libraryURL;
if (!libraryURL)
return nil;
NSLog(@"Reading iTunes library: %@", libraryURL);
NSDictionary *library = [NSDictionary dictionaryWithContentsOfURL:libraryURL];
if (!library) {
NSLog(@"Error: unable to read iTunes library from '%@'.", libraryURL);
return nil;
}
NSDictionary *tracks = library[@"Tracks"];
if (!tracks) {
NSLog(@"Error: No tracks found in iTunes library.");
return nil;
}
NSLog(@"Read %lu tracks from iTunes.", (unsigned long)tracks.count);
NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:tracks.count];
for (NSDictionary *track in tracks.allValues)
{
NSString *name = [track valueForKey:@"Name"];
NSString *artist = [track valueForKey:@"Artist"];
NSString *trackID = [NSString stringWithFormat:@"%@ - %@", artist, name].lowercaseString;
result[trackID] = track;
}
_tracks = [result copy];
return _tracks;
}
- (NSDictionary *)trackForTrack:(NSString *)name artist:(NSString *)artist
{
NSString *trackID = [NSString stringWithFormat:@"%@ - %@", artist, name].lowercaseString;
return (self.tracks)[trackID];
}
#pragma mark -
#pragma mark iTunes Scripting
- (SBiTunesApplication *)app
{
if (!_app)
_app = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
return _app;
}
- (SBiTunesSource *)source
{
for (SBiTunesSource *source in [self app].sources) {
if (source.kind == SBiTunesESrcLibrary)
return source;
}
return nil;
}
// master music library playlist
- (SBiTunesPlaylist *)masterPlaylist
{
if (!_masterPlaylist)
{
for (SBiTunesPlaylist *playlist in [self source].playlists) {
if (playlist.specialKind == SBiTunesESpKMusic) {
_masterPlaylist = playlist;
break;
}
}
}
return _masterPlaylist;
}
- (SBiTunesTrack *)trackForTrack:(NSDictionary *)track
{
SBiTunesPlaylist *masterPlaylist = [self masterPlaylist];
NSString *persistentID = track[@"Persistent ID"];
return [masterPlaylist.tracks filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"persistentID == %@", persistentID]].lastObject;
}
- (void)revealTrack:(NSDictionary *)track
{
if (track)
{
SBiTunesTrack *iTunesTrack = [self trackForTrack:track];
[iTunesTrack reveal];
}
}
- (void)createPlaylist:(NSString *)name withTracks:(NSArray *)tracks
{
if (tracks.count > 0)
{
NSDictionary *playlistProperties = @{@"name": name};
SBiTunesPlaylist *playlist = [[[[self app] classForScriptingClass:@"playlist"] alloc] initWithProperties:playlistProperties];
[[self source].userPlaylists insertObject:playlist atIndex:0];
for (NSDictionary *track in tracks)
{
SBiTunesTrack *iTunesTrack = [self trackForTrack:track];
if (iTunesTrack)
[iTunesTrack duplicateTo:playlist];
}
}
}
@end