public
Description: Mac OS X Last.fm scrobbler for iTunes and iPod
Homepage: http://www.scrobblepod.com
Clone URL: git://github.com/scrobblepod/scrobblepod.git
scrobblepod / BGLastFmHandshaker.m
100644 70 lines (50 sloc) 2.276 kb
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
//
// BGLastFmHandshaker.m
// LastFmProtocolTester
//
// Created by Ben Gummer on 8/07/07.
// Copyright 2007 Ben Gummer. All rights reserved.
//
 
#import "BGLastFmHandshaker.h"
#import "CocoaCryptoHashing.h"
#import "HubStrings.h"
 
@implementation BGLastFmHandshaker
 
-(id)init {
self = [super init];
if (self != nil) {
 
}
return self;
}
 
- (void) dealloc {
[super dealloc];
}
 
-(BGLastFmHandshakeResponse *)performHandshakeWithUsername:(NSString *)theUsername usingApiSessionKey:(NSString *)apiSessionKey {
 
NSString *currentUnixTime;
NSString *authenticationHash;
NSURL *handshakeURL;
 
int handshakeAttempts = 0;
BGLastFmHandshakeResponse *theResponse = [[BGLastFmHandshakeResponse alloc] init];
 
while ([theResponse sessionKey]==nil && ![theResponse didFail] && handshakeAttempts<3 ) {
 
currentUnixTime = [NSString stringWithFormat:@"%d",(int)[[NSDate date] timeIntervalSince1970]];
authenticationHash = [[NSString stringWithFormat:@"%@%@",[API_SECRET md5HexHash],currentUnixTime] md5HexHash];
 
handshakeURL = [NSURL URLWithString: [NSString stringWithFormat:@"http://post.audioscrobbler.com/?hs=true&p=1.2.1&c=sld&v=0.521&u=%@&t=%@&a=%@&sk=%@",theUsername,currentUnixTime,authenticationHash,apiSessionKey]];
 
NSMutableURLRequest *handshakeRequest = [[NSMutableURLRequest alloc] initWithURL:handshakeURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:2.0];
[handshakeRequest setHTTPMethod:@"GET"];
[handshakeRequest setTimeoutInterval:10.0];
 
NSError *handshakeAccessError;
NSHTTPURLResponse *response = nil;
NSData *handshakeResponseData = [NSURLConnection sendSynchronousRequest:handshakeRequest returningResponse:&response error:&handshakeAccessError];
 
[handshakeRequest release];
if (handshakeResponseData!=nil && [response statusCode]==200 && [handshakeAccessError code]!=-1001) {
NSString *handshakeResponseString = [[NSString alloc] initWithData:handshakeResponseData encoding:NSUTF8StringEncoding];
if (theResponse) [theResponse release];
theResponse = [[BGLastFmHandshakeResponse alloc] initWithHandshakeResponseString:handshakeResponseString];
[handshakeResponseString release];
} else {
[theResponse setDidFail:YES];
}
 
handshakeAttempts++;
 
}
 
return theResponse;
 
}
 
@end