X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/97eb97cd6af1e3019980ed380a6cbaf1feae0abe..ba95bdd1193aefaac80e1f1f6d3956733ce0f3bd:/AudioscrobblerController.m diff --git a/AudioscrobblerController.m b/AudioscrobblerController.m index 695c684..8d65de3 100644 --- a/AudioscrobblerController.m +++ b/AudioscrobblerController.m @@ -12,6 +12,7 @@ */ #import "AudioscrobblerController.h" +#import static AudioscrobblerController *_sharedController = nil; @@ -19,7 +20,9 @@ static AudioscrobblerController *_sharedController = nil; + (void)load { - //[[AudioscrobblerController sharedController] attemptHandshake]; + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + [[AudioscrobblerController sharedController] submitTrack:@"Stairway To Heaven" artist:@"Led Zeppelin" album:@"Led Zeppelin IV" length:483]; + [pool release]; } + (AudioscrobblerController *)sharedController @@ -35,12 +38,17 @@ static AudioscrobblerController *_sharedController = nil; if ( (self = [super init]) ) { _handshakeCompleted = NO; _responseData = nil; + _md5Challenge = nil; + _postURL = nil; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAudioscrobblerNotification:) name:nil object:self]; } return self; } - (void)dealloc { + [_md5Challenge release]; + [_postURL release]; [_responseData release]; [super dealloc]; } @@ -48,13 +56,12 @@ static AudioscrobblerController *_sharedController = nil; - (void)attemptHandshake { NSString *version = [[[NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:@"iTunes.app"]] infoDictionary] objectForKey:@"CFBundleVersion"], *user = @"Tristrex"; - NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://post.audioscrobbler.com/?hs=true&p=1.1&c=est&v=\"%@\"&u=\"%@\"", version, user]]; + NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://post.audioscrobbler.com/?hs=true&p=1.1&c=tst&v=%@&u=%@", version, user]]; NSURLConnection *connection; + _currentStatus = AudioscrobblerRequestingHandshakeStatus; _responseData = [[NSMutableData alloc] init]; - connection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self]; - - _handshakeCompleted = YES; + connection = [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30] delegate:self]; } - (BOOL)handshakeCompleted @@ -62,6 +69,62 @@ static AudioscrobblerController *_sharedController = nil; return _handshakeCompleted; } +- (void)submitTrack:(NSString *)title artist:(NSString *)artist album:(NSString *)album length:(int)length +{ + if (!_handshakeCompleted) { + [self attemptHandshake]; + return; + } + + //What we eventually want is a submission list that sends backlogs also + NSMutableURLRequest *request = [[NSURLRequest requestWithURL:_postURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30] mutableCopy]; + NSString *responseHash = @"", *requestBody; + + char *pass = "waffles"; + unsigned char *buffer; + EVP_MD_CTX ctx; + int i; + + buffer = malloc(EVP_MD_size(EVP_md5())); + + EVP_DigestInit(&ctx, EVP_md5()); + EVP_DigestUpdate(&ctx, pass, strlen(pass)); + EVP_DigestFinal(&ctx, buffer, NULL); + + for (i = 0; i < 16; i++) { + responseHash = [responseHash stringByAppendingFormat:@"%0.2x", buffer[i]]; + } + + free(buffer); + buffer = malloc(EVP_MD_size(EVP_md5())); + char *cat = (char *)[[responseHash stringByAppendingString:_md5Challenge] UTF8String]; + EVP_DigestInit(&ctx, EVP_md5()); + EVP_DigestUpdate(&ctx, cat, strlen(cat)); + EVP_DigestFinal(&ctx, buffer, NULL); + + responseHash = @""; + for (i = 0; i < 16; i++) { + responseHash = [responseHash stringByAppendingFormat:@"%0.2x", buffer[i]]; + } + free(buffer); + + requestBody = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)[NSString stringWithFormat:@"u=%@&s=%@&a[0]=%@&t[0]=%@&b[0]=%@&m[0]=&l[0]=%i&i[0]=%@", @"Tristrex", responseHash, artist, title, album, length, [[NSDate date] descriptionWithCalendarFormat:@"%Y-%m-%d %H:%M:%S" timeZone:nil locale:nil]], NULL, NULL, kCFStringEncodingUTF8); + [request setHTTPMethod:@"POST"]; + [request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]]; + _currentStatus = AudioscrobblerSubmittingTrackStatus; + _responseData = [[NSMutableData alloc] init]; + [NSURLConnection connectionWithRequest:request delegate:self]; + CFRelease(requestBody); + [request release]; +} + +- (void)handleAudioscrobblerNotification:(NSNotification *)note +{ + if ([[note name] isEqualToString:@"AudioscrobblerHandshakeComplete"]) { + [[AudioscrobblerController sharedController] submitTrack:@"Good Times Bad Times" artist:@"Led Zeppelin" album:@"Led Zeppelin I" length:166]; + } +} + #pragma mark - - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error @@ -77,7 +140,37 @@ static AudioscrobblerController *_sharedController = nil; - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *string = [[NSString alloc] initWithData:_responseData encoding:NSASCIIStringEncoding]; - NSLog(@"Rawr: %@", string); + + if (_currentStatus == AudioscrobblerRequestingHandshakeStatus) { + NSArray *lines = [string componentsSeparatedByString:@"\n"]; + NSString *responseAction; + if ([lines count] < 2) { + //We have an error + } + responseAction = [lines objectAtIndex:0]; + if ([responseAction isEqualToString:@"UPTODATE"]) { + if ([lines count] >= 4) { + _md5Challenge = [[lines objectAtIndex:1] retain]; + _postURL = [[NSURL alloc] initWithString:[lines objectAtIndex:2]]; + _handshakeCompleted = YES; + [[NSNotificationCenter defaultCenter] postNotificationName:@"AudioscrobblerHandshakeComplete" object:self]; + } else { + //We have an error + } + //Something + } else if (([responseAction length] > 6) && [[responseAction substringToIndex:5] isEqualToString:@"UPDATE"]) { + //Something plus update action + } else if (([responseAction length] > 6) && [[responseAction substringToIndex:5] isEqualToString:@"FAILED"]) { + //We have an error + } else if ([responseAction isEqualToString:@"BADUSER"]) { + //We have an error + } else { + //We have an error + } + } else if (_currentStatus == AudioscrobblerSubmittingTrackStatus) { + NSLog(string); + } + [string release]; }