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
/
BGLastFmHandshaker.m
69 lines (50 loc) · 2.22 KB
/
BGLastFmHandshaker.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
//
// 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