Permalink
Browse files

Added basic Last.fm client

  • Loading branch information...
1 parent c94a539 commit e52400eaa5f5cd71a819bc9042720586da11fb8a @indragiek committed Aug 27, 2012
View
@@ -7,3 +7,6 @@
[submodule "External/cocoalibspotify"]
path = External/cocoalibspotify
url = git://github.com/indragiek/cocoalibspotify.git
+[submodule "External/SSKeychain"]
+ path = External/SSKeychain
+ url = git://github.com/samsoffes/sskeychain.git
@@ -13,4 +13,9 @@
@return A normalized string by stripping out case and diacritic characters
*/
- (NSString *)SMK_normalizedString;
+
+/**
+ @return The string's MD5 hash
+ */
+- (NSString *)SMK_MD5Hash;
@end
@@ -7,6 +7,7 @@
//
#import "NSString+SMKAdditions.h"
+#import <CommonCrypto/CommonDigest.h>
@implementation NSString (SMKAdditions)
- (NSString *)SMK_normalizedString
@@ -16,4 +17,15 @@ - (NSString *)SMK_normalizedString
CFStringFold((__bridge CFMutableStringRef)result, kCFCompareCaseInsensitive | kCFCompareDiacriticInsensitive | kCFCompareWidthInsensitive, NULL);
return result;
}
+
+- (NSString *)SMK_MD5Hash
+{
+ const char *ptr = [self UTF8String];
+ unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
+ CC_MD5(ptr, (int)strlen(ptr), md5Buffer);
+ NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
+ for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
+ [output appendFormat:@"%02x",md5Buffer[i]];
+ return output;
+}
@end
@@ -38,8 +38,10 @@ - (id)init
_outputQueue = dispatch_queue_create("com.indragie.SNRMusicKit.artworkOutputQueue", DISPATCH_QUEUE_SERIAL);
_inputQueue = dispatch_queue_create("com.indragie.SNRMusicKit.artworkInputQueue", DISPATCH_QUEUE_SERIAL);
[[NSFileManager defaultManager] createDirectoryAtPath:[self _imageCacheDirectory] withIntermediateDirectories:YES attributes:nil error:nil];
+ __weak SMKArtworkCache *weakSelf = self;
dispatch_async(_inputQueue, ^{
- [self _pruneDiskCache];
+ SMKArtworkCache *strongSelf = weakSelf;
+ [strongSelf _pruneDiskCache];
});
}
return self;
Submodule SSKeychain added at e8f45d
@@ -0,0 +1,165 @@
+//
+// SMKLastFMClient.h
+// SNRMusicKit
+//
+// Created by Indragie Karunaratne on 2012-08-26.
+// Copyright (c) 2012 Indragie Karunaratne. All rights reserved.
+//
+
+#import "AFHTTPClient.h"
+
+@interface SMKLastFMClient : AFHTTPClient
+/**
+ API key and secret. Both of these can be obtained from Last.fm's developer page
+ <http://www.last.fm/api/account>
+ */
+@property (nonatomic, copy) NSString *APIKey;
+@property (nonatomic, copy) NSString *APISecret;
+
+/**
+ Username of the currently signed in user
+ */
+@property (nonatomic, copy, readonly) NSString *username;
+
+/**
+ Returns the shared instance ot SMKLastFMKClient
+ */
++ (instancetype)sharedInstance;
+
+/**
+ Attempts to log in with the specified username and fetch a previously saved session key
+ from the keychain.
+ @param username The login username
+ @return Whether the specified username has a saved session key. If not, you need to
+ use the authentication methods to retrieve and store the session key for future use, then
+ call this method again to log in.
+ */
+- (BOOL)loginWithUsername:(NSString *)username;
+
+/**
+ Logs out the current user from the Last.fm client.
+ */
+- (void)logout;
+
+#pragma mark -
+#pragma mark Authentication
+
+/**
+ Used by desktop & web authentication. Retrieves a session key using a given token and stores it in the OS X Keychain
+ @param token an authentication token
+ @param handler a completion handler block
+ */
+- (void)retrieveAndStoreSessionKeyWithToken:(NSString*)token
+ completionHandler:(void (^)(NSString *user, NSError *error))handler;
+
+#pragma mark -
+#pragma mark Web Authentication
+
+/**
+ Returns a web authentication URL that can have a callback URL.
+ @param callback Callback URL to open when the user has authenticated with Last.fm
+ See here for more info on custom auth handlers <http://www.last.fm/api/webauth#create_an_authentication_handler>
+ */
+- (NSURL*)webAuthenticationURLWithCallbackURL:(NSURL*)callback;
+
+#pragma mark -
+#pragma mark Desktop Authentication
+
+/**
+ Retrieves a new authentication token from the Last.fm API
+ @param handler a completion handler block
+ */
+- (void)retrieveAuthenticationToken:(void (^)(NSString *token, NSError *error))handler;
+
+/**
+ Returns an authentication URL that can be opened in a browser to authenticate your application with a user's Last.fm account
+ @param token an authentication token obtained from the -retrieveAuthenticationToken: method
+ @returns an authenticantion URL
+ */
+- (NSURL*)authenticationURLWithToken:(NSString*)token;
+
+#pragma mark -
+#pragma mark Mobile Authentication
+
+/**
+ Returns an authorization token with the specified credentials
+ @param username the user's username
+ @param password the user's password
+ @param handler a completion handler block
+ */
+- (void)retrieveAndStoreSessionKeyWithUsername:(NSString*)username
+ password:(NSString*)password
+ completionHandler:(void (^)(NSError *error))handler;
+
+#pragma mark -
+#pragma mark Keychain Access
+
+/**
+ Checks if the specified user has credentials stored in the keychain
+ @param user the username
+ @returns whether the user has stored credentials or not
+ */
++ (BOOL)userHasStoredCredentials:(NSString*)user;
+
+/**
+ Deletes the credentials for the specified user from the keychain
+ @param user the username
+ */
++ (void)removeCredentialsForUser:(NSString*)user;
+
+/**
+ Checks whether the current instance of SMKLastFMClient is authenticated
+ */
+- (BOOL)isAuthenticated;
+
+#pragma mark -
+#pragma mark Scrobbling/Now Playing
+
+/**
+ Scrobbles a track with the given parameters
+ @param name the track name
+ @param album the name of the track's album
+ @param artist the name of the track's artist
+ @param albumArtist the name of the track's album artist
+ @param track the track number
+ @param duration the duration of the track (must be an integer)
+ @param timestamp the timestamp at which the track was played --> use [NSNumber numberWithInteger:[[NSDate date] timeIntervalSince1970]] because the timestamp *must* be an integer
+ @param handler a completion handler block
+ */
+- (void)scrobbleTrackWithName:(NSString*)name
+ album:(NSString*)album
+ artist:(NSString*)artist
+ albumArtist:(NSString*)albumArtist
+ trackNumber:(NSInteger)trackNumber
+ duration:(NSInteger)duration
+ timestamp:(NSInteger)timestamp
+ completionHandler:(void (^)(NSDictionary *scrobbles, NSError *error))handler;
+
+/**
+ Sets the current user's now playing track with the given parameters
+ @param name the track name
+ @param album the name of the track's album
+ @param artist the name of the track's artist
+ @param albumArtist the name of the track's album artist
+ @param track the track number
+ @param duration the duration of the track
+ @param timestamp the timestamp at which the track was played --> use [NSNumber numberWithInteger:[[NSDate date] timeIntervalSince1970]] because the timestamp *must* be an integer
+ @param handler a completion handler block
+ */
+- (void)updateNowPlayingTrackWithName:(NSString*)name
+ album:(NSString*)album
+ artist:(NSString*)artist
+ albumArtist:(NSString*)albumArtist
+ trackNumber:(NSInteger)trackNumber
+ duration:(NSInteger)duration
+ completionHandler:(void (^)(NSDictionary *response, NSError *error))handler;
+
+/**
+ Loves the track with the specified information.
+ @param name The track name
+ @param artist The artist name
+ */
+- (void)loveTrackWithName:(NSString*)name
+ artist:(NSString*)artist
+ completionHandler:(void (^)(NSDictionary *response, NSError *error))handler;
+@end
Oops, something went wrong.

0 comments on commit e52400e

Please sign in to comment.