Permalink
Browse files

Add playlist bootstrapper class

- It takes a spotify playlist url, and returns the spotify track urls
- Switched zmq dispatch to use NSNotificationCenter on playlist
  bootstrapper
- Should probably ditch delegate pattern for player to be consistent
  with playlist bootstrapper
- Still pretty rough, wanted to get a stake in the ground
  • Loading branch information...
1 parent 0b75140 commit daf71e2515baf068ceceebcadbb71d5496ed344c @tonywok tonywok committed Mar 22, 2012
@@ -11,6 +11,7 @@
1522F232150E6647008E2EA3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1522F22C150E6647008E2EA3 /* main.m */; };
1522F233150E6647008E2EA3 /* SpotboxPlayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 1522F22E150E6647008E2EA3 /* SpotboxPlayer.m */; };
1522F234150E6647008E2EA3 /* ZmqDispatch.m in Sources */ = {isa = PBXBuildFile; fileRef = 1522F230150E6647008E2EA3 /* ZmqDispatch.m */; };
+ 152ADC35151A50C20042128D /* PlaylistBootstrap.m in Sources */ = {isa = PBXBuildFile; fileRef = 152ADC34151A50C20042128D /* PlaylistBootstrap.m */; };
15628E4C1509480600288D72 /* libzmq.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 15628E47150946EF00288D72 /* libzmq.dylib */; };
1566CCDE1509116E008ED86F /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1566CCDD1509116E008ED86F /* Foundation.framework */; };
15DD48E6150E648C002870E0 /* CocoaLibSpotify.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 15DD48E5150E648C002870E0 /* CocoaLibSpotify.framework */; };
@@ -42,6 +43,8 @@
1522F22E150E6647008E2EA3 /* SpotboxPlayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpotboxPlayer.m; sourceTree = "<group>"; };
1522F22F150E6647008E2EA3 /* ZmqDispatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZmqDispatch.h; sourceTree = "<group>"; };
1522F230150E6647008E2EA3 /* ZmqDispatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ZmqDispatch.m; sourceTree = "<group>"; };
+ 152ADC33151A50C20042128D /* PlaylistBootstrap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlaylistBootstrap.h; sourceTree = "<group>"; };
+ 152ADC34151A50C20042128D /* PlaylistBootstrap.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PlaylistBootstrap.m; sourceTree = "<group>"; };
15628E47150946EF00288D72 /* libzmq.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libzmq.dylib; path = usr/local/lib/libzmq.dylib; sourceTree = SDKROOT; };
15628E4F1509490300288D72 /* ZMQObjC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ZMQObjC.h; sourceTree = "<group>"; };
1566CCD91509116E008ED86F /* cocoa_spotify */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = cocoa_spotify; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -82,6 +85,8 @@
1522F22C150E6647008E2EA3 /* main.m */,
1522F22D150E6647008E2EA3 /* SpotboxPlayer.h */,
1522F22E150E6647008E2EA3 /* SpotboxPlayer.m */,
+ 152ADC33151A50C20042128D /* PlaylistBootstrap.h */,
+ 152ADC34151A50C20042128D /* PlaylistBootstrap.m */,
1522F22F150E6647008E2EA3 /* ZmqDispatch.h */,
1522F230150E6647008E2EA3 /* ZmqDispatch.m */,
);
@@ -196,6 +201,7 @@
1522F232150E6647008E2EA3 /* main.m in Sources */,
1522F233150E6647008E2EA3 /* SpotboxPlayer.m in Sources */,
1522F234150E6647008E2EA3 /* ZmqDispatch.m in Sources */,
+ 152ADC35151A50C20042128D /* PlaylistBootstrap.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,21 @@
+//
+// PlaylistBootstrap.h
+// cocoa_spotify
+//
+// Created by Tony Schneider on 3/21/12.
+// Copyright (c) 2012 Edgecase. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "ZmqDispatch.h"
+#import <CocoaLibSpotify/CocoaLibSpotify.h>
+
+@interface PlaylistBootstrap : NSObject
+
+- (id) initWithDispatcher:(ZmqDispatch *)aDispatcher;
+- (void) reportPlaylistTracks:(SPPlaylist *)playlist;
+- (void) loadTracksFromPlaylist:(NSURL *)playlist_url;
+
+@property(retain) ZmqDispatch *dispatcher;
+
+@end
@@ -0,0 +1,73 @@
+//
+// PlaylistBootstrap.m
+// cocoa_spotify
+//
+// Created by Tony Schneider on 3/21/12.
+// Copyright (c) 2012 Edgecase. All rights reserved.
+//
+
+#import "PlaylistBootstrap.h"
+
+@implementation PlaylistBootstrap
+
+@synthesize dispatcher;
+
+- (id) initWithDispatcher:(ZmqDispatch *)aDispatcher {
+ self = [super init];
+ if (self) {
+ self.dispatcher = aDispatcher;
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(load_playlist:)
+ name:@"load_playlist"
+ object:nil];
+ }
+ return self;
+}
+
+- (void) reportPlaylistTracks:(SPPlaylist *)playlist {
+ NSMutableArray *track_urls = [NSMutableArray array];
+ NSMutableArray *tracks = [playlist items];
+
+ for (int index = 0; index < [tracks count]; index++) {
+ SPPlaylistItem *item = [tracks objectAtIndex:index];
+ if ([item itemURLType] == SP_LINKTYPE_TRACK) {
+ [track_urls addObject:[item itemURL]];
+ }
+ }
+
+ NSString *playlist_url_str = [[playlist spotifyURL] absoluteString];
+ NSString *playlist_name = [playlist name];
+ NSString *tracks_str = [track_urls componentsJoinedByString:@","];
+ NSString *msg = [NSString stringWithFormat:@"spotbox:server::playlist_loaded::%@,%@,%@", playlist_url_str, playlist_name, tracks_str];
+ [[dispatcher pub] sendData:[msg dataUsingEncoding:NSUTF8StringEncoding] withFlags:ZMQ_NOBLOCK];
+}
+
+- (void) loadTracksFromPlaylist:(NSURL *)playlist_url {
+ SPPlaylist *playlist = [[SPSession sharedSession] playlistForURL:playlist_url];
+
+ if (playlist != nil) {
+ if (!playlist.isLoaded) {
+ [self performSelector:@selector(loadTracksFromPlaylist:) withObject:playlist_url afterDelay:0.1];
+ return;
+ }
+ }
+
+ [self reportPlaylistTracks:playlist];
+}
+
+//**************** ZmqDispatch Notifications *********************//
+
+- (void) load_playlist:(NSNotification *)notification {
+ NSString *playlist_str = [[[notification userInfo] valueForKey:@"args"] objectAtIndex:0];
+ NSURL *playlist_url = [[NSURL alloc] initWithString:playlist_str];
+
+ [self loadTracksFromPlaylist:playlist_url];
+}
+
+//************************* Dealloc *****************************//
+
+- (void) dealloc {
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
+
+@end
@@ -13,14 +13,17 @@
@protocol ZmqDispatchDelegate <NSObject>
+@optional
- (void) zmqDispatchDidReceivePlay:(NSString *)track_url;
- (void) zmqDispatchDidReceiveData;
- (void) zmqDispatchDidReceiveStop;
- (void) zmqDispatchDidReceivePause;
@end
-@interface ZmqDispatch : NSObject
+@interface ZmqDispatch : NSObject {
+ NSNotificationCenter *zmqNotificationCenter;
+}
@property (strong)ZMQSocket *pub;
@property (strong)ZMQSocket *sub;
@@ -68,6 +68,8 @@ - (void) receiveData:(NSTimer *)timer {
[delegate zmqDispatchDidReceiveStop];
} else if ([method_name isEqualToString:@"pause"] || [method_name isEqualToString:@"unpause"]) {
[delegate zmqDispatchDidReceivePause];
+ } else if ([method_name isEqualToString:@"load_playlist"]) {
+ [[NSNotificationCenter defaultCenter] postNotificationName:method_name object:self userInfo:parsedMessage];
} else {
NSLog(@"Unsupported method: %@ w/ raw msg: %@", method_name, msg);
}
View
@@ -10,6 +10,7 @@
#import "ZMQObjC.h"
#import <CocoaLibSpotify/CocoaLibSpotify.h>
#import "SpotboxPlayer.h"
+#import "PlaylistBootstrap.h"
#import "ZmqDispatch.h"
#import "config.h"
#include "spotify_appkey.c"
@@ -28,8 +29,12 @@ int main (int argc, const char * argv[]) {
userAgent:@"com.edgecase.spotbox"
error:nil];
- // Initialize spotify player
+ // Initialize playlist bootstrapper
+ PlaylistBootstrap *playlist_bootstrap = [[PlaylistBootstrap alloc] initWithDispatcher:dispatcher];
+
+ // Initialize spotify player
SpotboxPlayer *player = [[SpotboxPlayer alloc] initWithDispatcher:dispatcher];
+
[dispatcher setDelegate:player];
[[SPSession sharedSession] setDelegate:player];

0 comments on commit daf71e2

Please sign in to comment.