3 * AudioscrobblerController
4 * Audioscrobbler Support Class
6 * Original Author : Kent Sutherland <kent.sutherland@ithinksw.com>
7 * Responsibility : Kent Sutherland <kent.sutherland@ithinksw.com>
9 * Copyright (c) 2005 iThink Software.
14 #import "AudioscrobblerController.h"
15 #import <openssl/evp.h>
16 #import <ITFoundation/ITDebug.h>
18 static AudioscrobblerController *_sharedController = nil;
20 @implementation AudioscrobblerController
24 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
25 [[AudioscrobblerController sharedController] submitTrack:@"Immigrant Song" artist:@"Led Zeppelin" album:@"How The West Was Won" length:221];
26 [[AudioscrobblerController sharedController] submitTrack:@"Comfortably Numb" artist:@"Pink Floyd" album:@"The Wall" length:384];
27 [[AudioscrobblerController sharedController] submitTracks];
31 + (AudioscrobblerController *)sharedController
33 if (!_sharedController) {
34 _sharedController = [[AudioscrobblerController alloc] init];
36 return _sharedController;
41 if ( (self = [super init]) ) {
42 /*_handshakeCompleted = NO;
46 _handshakeCompleted = YES;
47 _md5Challenge = @"rawr";
48 _postURL = [NSURL URLWithString:@"http://audioscrobbler.com/"];
52 _tracks = [[NSMutableArray alloc] init];
53 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioscrobblerNotification:) name:nil object:self];
60 [_md5Challenge release];
62 [_responseData release];
67 - (void)attemptHandshake
69 //Delay if we haven't met the interval time limit
70 NSTimeInterval interval = [_delayDate timeIntervalSinceNow];
72 ITDebugLog(@"Audioscrobbler: Delaying handshake attempt for %i seconds", interval);
73 [self performSelector:@selector(attemptHandshake) withObject:nil afterDelay:interval + 1];
77 if (!_handshakeCompleted) {
78 NSString *version = [[[NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:@"iTunes.app"]] infoDictionary] objectForKey:@"CFBundleVersion"], *user = @"Tristrex";
79 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://post.audioscrobbler.com/?hs=true&p=1.1&c=tst&v=%@&u=%@", version, user]];
80 NSURLConnection *connection;
82 _currentStatus = AudioscrobblerRequestingHandshakeStatus;
83 connection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30] delegate:self];
87 - (BOOL)handshakeCompleted
89 return _handshakeCompleted;
92 - (void)submitTrack:(NSString *)title artist:(NSString *)artist album:(NSString *)album length:(int)length
94 if (!_handshakeCompleted) {
95 [self attemptHandshake];
98 ITDebugLog(@"Audioscrobbler: Adding a new track to the submission queue.");
99 NSDictionary *newTrack = [NSDictionary dictionaryWithObjectsAndKeys:title,
105 [NSString stringWithFormat:@"%i", length],
107 [[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%d %H:%M:%S" timeZone:nil locale:nil],
110 [_tracks addObject:newTrack];
115 NSTimeInterval interval = [_delayDate timeIntervalSinceNow];
117 ITDebugLog(@"Audioscrobbler: Delaying track submission for %i seconds", interval);
118 [self performSelector:@selector(attemptHandshake) withObject:nil afterDelay:interval + 1];
123 NSMutableString *requestString;
124 NSString *authString, *responseHash;
125 char *pass = "waffles";
126 unsigned char *buffer;
129 if (!_handshakeCompleted) {
130 [self attemptHandshake];
134 ITDebugLog(@"Audioscrobbler: Submitting queued tracks");
136 if ([_tracks count] == 0) {
137 ITDebugLog(@"Audioscrobbler: No queued tracks to submit.");
141 //Build the MD5 response string we send along with the request
142 buffer = malloc(EVP_MD_size(EVP_md5()));
143 EVP_DigestInit(&ctx, EVP_md5());
144 EVP_DigestUpdate(&ctx, pass, strlen(pass));
145 EVP_DigestFinal(&ctx, buffer, NULL);
147 for (i = 0; i < 16; i++) {
148 responseHash = [responseHash stringByAppendingFormat:@"%0.2x", buffer[i]];
152 buffer = malloc(EVP_MD_size(EVP_md5()));
153 char *cat = (char *)[[responseHash stringByAppendingString:_md5Challenge] UTF8String];
154 EVP_DigestInit(&ctx, EVP_md5());
155 EVP_DigestUpdate(&ctx, cat, strlen(cat));
156 EVP_DigestFinal(&ctx, buffer, NULL);
159 for (i = 0; i < 16; i++) {
160 responseHash = [responseHash stringByAppendingFormat:@"%0.2x", buffer[i]];
164 authString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)[NSString stringWithFormat:@"u=%@&s=%@", @"Tristrex", responseHash], NULL, NULL, kCFStringEncodingUTF8);
165 requestString = [[NSMutableString alloc] initWithString:authString];
166 [authString release];
168 //We can only submit ten tracks at a time
169 for (i = 0; ([_tracks count] > 0) && (i < 10); i++) {
170 NSDictionary *nextTrack = [_tracks objectAtIndex:i];
171 NSString *trackString;
173 trackString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)[NSString stringWithFormat:@"&a[%i]=%@&t[%i]=%@&b[%i]=%@&m[%i]=&l[%i]=%@&i[%i]=%@", i, [nextTrack objectForKey:@"artist"], i, [nextTrack objectForKey:@"title"], i, [nextTrack objectForKey:@"album"], i, i, [nextTrack objectForKey:@"length"], i, [nextTrack objectForKey:@"time"]], NULL, NULL, kCFStringEncodingUTF8);
174 [requestString appendString:trackString];
175 [trackString release];
178 //Create and send the request
179 NSMutableURLRequest *request = [[NSURLRequest requestWithURL:_postURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30] mutableCopy];
180 [request setHTTPMethod:@"POST"];
181 [request setHTTPBody:[requestString dataUsingEncoding:NSUTF8StringEncoding]];
182 _currentStatus = AudioscrobblerSubmittingTracksStatus;
183 //[NSURLConnection connectionWithRequest:request delegate:self];
184 [requestString release];
187 //If we have tracks left, submit again after the interval seconds
190 - (void)handleAudioscrobblerNotification:(NSNotification *)note
192 if ([[note name] isEqualToString:@"AudioscrobblerHandshakeComplete"]) {
193 if ([_tracks count] > 0) {
196 [self submitTrack:@"Immigrant Song" artist:@"Led Zeppelin" album:@"How The West Was Won" length:221];
197 [self submitTrack:@"Comfortably Numb" artist:@"Pink Floyd" album:@"The Wall" length:384];
204 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
206 ITDebugLog(@"Audioscrobbler: Connection error \"%@\"", error);
209 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
211 [_responseData appendData:data];
214 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
216 NSString *string = [[NSString alloc] initWithData:_responseData encoding:NSASCIIStringEncoding];
217 NSArray *lines = [string componentsSeparatedByString:@"\n"];
218 NSString *responseAction = nil;
220 if ([lines count] > 0) {
221 responseAction = [lines objectAtIndex:0];
224 if (_currentStatus == AudioscrobblerRequestingHandshakeStatus) {
225 if ([lines count] < 2) {
226 //We have a protocol error
228 if ([responseAction isEqualToString:@"UPTODATE"]) {
229 if ([lines count] >= 4) {
230 _md5Challenge = [[lines objectAtIndex:1] retain];
231 _postURL = [[NSURL alloc] initWithString:[lines objectAtIndex:2]];
232 _handshakeCompleted = YES;
233 [[NSNotificationCenter defaultCenter] postNotificationName:@"AudioscrobblerHandshakeComplete" object:self];
235 //We have a protocol error
238 } else if (([responseAction length] > 5) && [[responseAction substringToIndex:5] isEqualToString:@"UPDATE"]) {
239 //Something plus update action
240 } else if (([responseAction length] > 5) && [[responseAction substringToIndex:5] isEqualToString:@"FAILED"]) {
242 } else if ([responseAction isEqualToString:@"BADUSER"]) {
247 } else if (_currentStatus == AudioscrobblerSubmittingTracksStatus) {
248 if ([responseAction isEqualToString:@"OK"]) {
249 } else if ([responseAction isEqualToString:@"BADAUTH"]) {
251 } else if (([responseAction length] > 5) && [[responseAction substringToIndex:5] isEqualToString:@"FAILED"]) {
257 //Handle the final INTERVAL response
258 if (([responseAction length] > 9) && [[responseAction substringToIndex:7] isEqualToString:@"INTERVAL"]) {
259 int seconds = [[[lines objectAtIndex:[lines count] - 1] substringFromIndex:9] intValue];
260 ITDebugLog(@"Audioscrobbler: INTERVAL %i", seconds);
261 _delayDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
263 //We have a protocol error
267 [_responseData release];
270 -(NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
272 ITDebugLog(@"Audioscrobbler: Sending URL request.");
273 NSLog(@"Sending request.");
274 _responseData = [[NSMutableData alloc] init];