This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
BGTrackCollector.m
123 lines (95 loc) · 4.75 KB
/
BGTrackCollector.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
//
// BGTrackCollector.m
// ScrobblePod
//
// Created by Ben on 13/01/08.
// Copyright 2008 Ben Gummer. All rights reserved.
//
#import "BGTrackCollector.h"
#import "BGLastFmSong.h"
#import "Defines.h"
#import "NSDictionary+ExclusionTest.h"
@implementation BGTrackCollector
-(NSMutableArray *)collectTracksFromXMLFile:(NSString *)xmlPath withCutoffDate:(NSDate *)cutoffDate includingPodcasts:(BOOL)includePodcasts includingVideo:(BOOL)includeVideo ignoringComment:(NSString *)ignoreString ignoringGenre:(NSString *)genreString withMinimumDuration:(int)minimumDuration {
double oldPriority = [NSThread threadPriority];
[NSThread setThreadPriority:0.0];
if (!xmlPath || ![[NSFileManager defaultManager] fileExistsAtPath:xmlPath]) {
NSLog(@"Supplied XML path does not exist - Using default XML path");
xmlPath = [@"~/Music/iTunes/iTunes Music Library.xml" stringByExpandingTildeInPath];
}
NSLog(@"Starting XML contents read");
NSDictionary *itunesLibrary = [NSDictionary dictionaryWithContentsOfFile:xmlPath];
NSLog(@"Completed XML contents read");
//check to see if library load was successful
NSMutableArray *resultSongArray = [NSMutableArray new];
BOOL useAlbumArtist = [[NSUserDefaults standardUserDefaults] boolForKey:BGPrefShouldUseAlbumArtist];
BOOL useComposerInsteadOfArtist = [[NSUserDefaults standardUserDefaults] boolForKey:BGPrefShouldUseComposerInsteadOfArtist];
BOOL useGroupingInTitle = [[NSUserDefaults standardUserDefaults] boolForKey:BGPrefShouldUseGroupingInTitle];
if (itunesLibrary) {
NSLog(@"Parsing XML contents");
NSDictionary *itunesTracks = [itunesLibrary objectForKey:@"Tracks"];
if (itunesTracks) {
NSMutableArray *wantedTracks = [NSMutableArray new];
NSString *key;
for (key in itunesTracks) {
NSDictionary *trackDetails = [itunesTracks objectForKey:key];
BOOL trackPassed = [trackDetails passesExclusionTestWithCutoffDate:cutoffDate includingPodcasts:includePodcasts includingVideo:includeVideo ignoringComment:ignoreString ignoringGenre:genreString withMinimumDuration:minimumDuration];
if (trackPassed==YES) [wantedTracks addObject:trackDetails];
}
NSLog(@"Tracks Passed: %d",wantedTracks.count);
NSSortDescriptor *d = [[[NSSortDescriptor alloc] initWithKey: @"Play Date UTC" ascending: YES] autorelease];
NSArray *wantedTracksSorted = [wantedTracks sortedArrayUsingDescriptors: [NSArray arrayWithObject: d]];
[wantedTracks release];
NSDictionary *trackStuff;
for (trackStuff in wantedTracksSorted) {
// track name
NSString *trackTitle = [trackStuff objectForKey:@"Name"];
NSString *nameString = nil;
if (trackTitle) {
if (useGroupingInTitle) {
NSString *trackGrouping = [trackStuff objectForKey:@"Grouping"];
if (trackGrouping) nameString = [NSString stringWithFormat:@"%@: %@",trackGrouping,trackTitle];
}
if (!nameString) nameString = trackTitle;
}
if (!nameString) nameString = @"";
// artist, using album artist where possible
NSString *artistString = nil;
if (useAlbumArtist) {
artistString = [trackStuff objectForKey:@"Album Artist"];
} else if (useComposerInsteadOfArtist) {
artistString = [trackStuff objectForKey:@"Composer"];
}
if (!artistString) artistString = [trackStuff objectForKey:@"Artist"];
if (!artistString) artistString = @"";
// album name
NSString *albumString = [trackStuff objectForKey:@"Album"];
if (!albumString) albumString = @"";
// track duration
NSString *theduration = [NSString stringWithFormat:@"%i",(int)([[trackStuff objectForKey:@"Total Time"] intValue]/1000)];
// played date
NSString *playedString = [[trackStuff objectForKey:@"Play Date UTC"] descriptionWithCalendarFormat:@"%Y-%m-%d %H:%M:%S" timeZone:nil locale:nil];
NSCalendarDate *playedDate = [NSCalendarDate dateWithString:playedString calendarFormat:@"%Y-%m-%d %H:%M:%S"];
// play count
int playCount = [[trackStuff objectForKey:@"Play Count"] intValue];
// unique identifier
NSString *uniqueIdentifier = [[trackStuff objectForKey:@"Track ID"] stringValue];
BGLastFmSong *newSong;
newSong = [[BGLastFmSong alloc] initWithTitle:nameString
artist:artistString
album:albumString];
[newSong setLength:[theduration intValue]];
[newSong setLastPlayed:playedDate];
[newSong setExtraPlays:0];
[newSong setPlayCount:playCount];
[newSong setUniqueIdentifier:uniqueIdentifier];
[resultSongArray addObject:newSong];
[newSong release];
}
} else NSLog(@"Could not load track item from dictionary, but XML was loaded OK");
} else NSLog(@"Could not load dictionary from XML");
//itunesLibrary
[NSThread setThreadPriority:oldPriority];
return resultSongArray;
}
@end