Permalink
Browse files

Merge pull request #25 from mxcl/track.getFingerprintMetadata

Track.get fingerprint metadata
  • Loading branch information...
2 parents 37ca49e + b22e9f4 commit 48739524adc0c8f3c0ddea89bdb516e2f1680c91 @mxcl committed on GitHub Jul 23, 2016
Showing with 61 additions and 8 deletions.
  1. +8 −0 NSXMLNode+mxcl.h
  2. +25 −0 NSXMLNode+mxcl.m
  3. +6 −3 lastfm.h
  4. +22 −5 lastfm.m
View
@@ -0,0 +1,8 @@
+// Created by Max Howell on 09/07/2010.
+
+@interface NSXMLNode(mxcl)
+
+-(NSXMLNode*)childNamed:(NSString*)name;
+-(NSArray*)childrenAsStrings;
+
+@end
View
@@ -0,0 +1,25 @@
+// Created by Max Howell on 09/07/2010.
+
+@implementation NSXMLNode(mxcl)
+
+-(NSXMLNode*)childNamed:(NSString*)name
+{
+ NSEnumerator* e = [self.children objectEnumerator];
+ NSXMLNode *node;
+ while (node = [e nextObject])
+ if ([node.name isEqualToString:name])
+ return node;
+ return nil;
+}
+
+-(NSArray*)childrenAsStrings
+{
+ NSMutableArray* strings = [NSMutableArray arrayWithCapacity:self.children.count];
+ NSEnumerator* e = [self.children objectEnumerator];
+ NSXMLNode* node;
+ while (node = [e nextObject])
+ [strings addObject:[node stringValue]];
+ return strings;
+}
+
+@end
View
@@ -40,16 +40,19 @@
@property(readonly) NSString* username;
-+(NSString*)urlEncode:(NSString*)url_component; // Last.fm has special URL encoding rules
-+(NSURL*)urlForUser:(NSString*)username; // the user's profile page
-
-(id)initWithDelegate:(id)delegate;
-(bool)love:(NSDictionary*)track;
-(void)share:(NSDictionary*)track with:(NSString*)username;
-(void)updateNowPlaying:(NSDictionary*)track;
-(void)scrobble:(NSDictionary*)track startTime:(time_t)start_time;
+// only sets Track and Artist
+-(NSDictionary*)getFingerprintMetadata:(unsigned long long)fpid;
+
+// these do not make a network connection to Last.fm
+(NSTimeInterval)scrobblePointForTrackWithDurationInSeconds:(NSTimeInterval)duration;
++(NSString*)urlEncode:(NSString*)url_component; // Last.fm has special URL encoding rules
++(NSURL*)urlForUser:(NSString*)username; // the user's profile page
@end
View
@@ -22,6 +22,7 @@
#import "lastfm_api.h"
#import "NSDictionary+Track.h"
#import <CommonCrypto/CommonDigest.h>
+#import "NSXMLNode+mxcl.h"
#define KEYCHAIN_NAME "fm.last.Audioscrobbler"
@@ -329,7 +330,7 @@ -(NSXMLDocument*)readResponse:(NSMutableURLRequest*)rq
NSString* method = extract_method(rq);
NSLog(@"Response from `%@':\n%@", method, [xml.rootElement XMLStringWithOptions:NSXMLNodePrettyPrint]);
- NSXMLElement* ee = [xml.rootElement elementsForName:@"error"].lastObject;
+ NSXMLElement* ee = [xml.rootElement childNamed:@"error"];
if (!ee)
@throw [LastfmError badResponse:method];
@@ -350,7 +351,7 @@ -(NSXMLDocument*)readResponse:(NSMutableURLRequest*)rq
-(NSString*)getToken
{
NSXMLDocument* xml = [self get:[NSMutableDictionary dictionary] to:@"auth.gettoken"];
- return [[xml.rootElement elementsForName:@"token"].lastObject stringValue];
+ return [[xml.rootElement childNamed:@"token"] stringValue];
}
static void inline save(NSString* username, NSString* sk)
@@ -382,9 +383,9 @@ -(void)getSession
[token release]; // consumed
token = nil;
- NSXMLElement* session = [xml.rootElement elementsForName:@"session"].lastObject;
- sk = [[[session elementsForName:@"key"].lastObject stringValue] retain];
- username = [[[session elementsForName:@"name"].lastObject stringValue] retain];
+ NSXMLElement* session = [xml.rootElement childNamed:@"session"];
+ sk = [[[session childNamed:@"key"] stringValue] retain];
+ username = [[[session childNamed:@"name"] stringValue] retain];
if (!username || !sk)
@throw [LastfmError badResponse:@"auth.getsession"];
@@ -470,6 +471,22 @@ -(void)updateNowPlaying:(NSDictionary*)track
}
}
+-(NSDictionary*)getFingerprintMetadata:(unsigned long long)fpid
+{
+ NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLongLong:fpid] forKey:@"fingerprintid"];
+ NSXMLNode* xml = [self request:POST params:dict to:@"user.updateNowPlaying"];
+
+ NSXMLNode* track = [xml.rootElement childNamed:@"track"];
+ NSString* title = [track childNamed:@"name"];
+ NSString* artist = [[track childNamed:@"artist"] childNamed:@"name"];
+
+ [dict removeAllObjects];
+ [dict setObject:title forKey:@"Track"];
+ [dict setObject:artist forKey:@"Artist"];
+
+ return dict;
+}
+
-(id)initWithDelegate:(id)d
{
token = nil;

0 comments on commit 4873952

Please sign in to comment.