Added a currentSongPlayed method that returns the integer seconds played in the curre...
authorKent Sutherland <ksuther@ithinksw.com>
Tue, 8 Nov 2005 20:46:18 +0000 (20:46 +0000)
committerKent Sutherland <ksuther@ithinksw.com>
Tue, 8 Nov 2005 20:46:18 +0000 (20:46 +0000)
More changes to audioscrobbler support.

AudioscrobblerController.m
ITMTRemote.h
ITMTRemote.m
MainController.m
PreferencesController.m
iTunesRemote.m

index 4addcaf..79bad7b 100644 (file)
@@ -16,6 +16,9 @@
 #import <openssl/evp.h>
 #import <ITFoundation/ITDebug.h>
 
 #import <openssl/evp.h>
 #import <ITFoundation/ITDebug.h>
 
+#define AUDIOSCROBBLER_ID @"tst"
+#define AUDIOSCROBBLER_VERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
+
 static AudioscrobblerController *_sharedController = nil;
 
 @implementation AudioscrobblerController
 static AudioscrobblerController *_sharedController = nil;
 
 @implementation AudioscrobblerController
@@ -75,9 +78,9 @@ static AudioscrobblerController *_sharedController = nil;
                return;
        }
        
                return;
        }
        
-       if (!_handshakeCompleted) {
-               NSString *version = [[[NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:@"iTunes.app"]] infoDictionary] objectForKey:@"CFBundleVersion"], *user = @"Tristrex";
-               NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://post.audioscrobbler.com/?hs=true&p=1.1&c=tst&v=%@&u=%@", version, user]];
+       NSString *user = [[NSUserDefaults standardUserDefaults] stringForKey:@"audioscrobblerUser"];
+       if (!_handshakeCompleted && user) {
+               NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://post.audioscrobbler.com/?hs=true&p=1.1&c=%@&v=%@&u=%@", AUDIOSCROBBLER_ID, AUDIOSCROBBLER_VERSION, user]];
                
                _currentStatus = AudioscrobblerRequestingHandshakeStatus;
                _responseData = [[NSMutableData alloc] init];
                
                _currentStatus = AudioscrobblerRequestingHandshakeStatus;
                _responseData = [[NSMutableData alloc] init];
index f49b559..1f84703 100755 (executable)
@@ -300,7 +300,12 @@ er's process managment.
 - (NSString *)currentSongLength;
 
 /*!
 - (NSString *)currentSongLength;
 
 /*!
- * @method currentSongDuratioh
+ * @method currentSongPlayed
+ */
+- (int)currentSongPlayed;
+
+/*!
+ * @method currentSongDuration
  */
 - (int)currentSongDuration;
 
  */
 - (int)currentSongDuration;
 
index e4ff611..8431051 100755 (executable)
     return nil;
 }
 
     return nil;
 }
 
+- (int)currentSongPlayed
+{
+       return -1;
+}
+
 - (int)currentSongDuration
 {
        return -1;
 - (int)currentSongDuration
 {
        return -1;
index db0269f..d51b72f 100755 (executable)
@@ -469,8 +469,8 @@ static MainController *sharedController;
                                if (_audioscrobblerTimer) {
                                        [_audioscrobblerTimer invalidate];
                                }
                                if (_audioscrobblerTimer) {
                                        [_audioscrobblerTimer invalidate];
                                }
-                               if (length > 0) {
-                                       _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO];
+                               if (length > 30) {
+                                       _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES];
                                }
                        } else {
                                _audioscrobblerTimer = nil;
                                }
                        } else {
                                _audioscrobblerTimer = nil;
@@ -595,8 +595,8 @@ static MainController *sharedController;
                        if (_audioscrobblerTimer) {
                                [_audioscrobblerTimer invalidate];
                        }
                        if (_audioscrobblerTimer) {
                                [_audioscrobblerTimer invalidate];
                        }
-                       if (length > 0) {
-                               _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO];
+                       if (length > 30) {
+                               _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES];
                        }
                } else {
                        _audioscrobblerTimer = nil;
                        }
                } else {
                        _audioscrobblerTimer = nil;
@@ -614,20 +614,29 @@ static MainController *sharedController;
 
 - (void)submitAudioscrobblerTrack:(NSTimer *)timer
 {
 
 - (void)submitAudioscrobblerTrack:(NSTimer *)timer
 {
+       int interval = [timer timeInterval];
+       [timer invalidate];
+       _audioscrobblerTimer = nil;
+       ITDebugLog(@"Audioscrobbler: Attempting to submit current track");
        if ([df boolForKey:@"audioscrobblerEnabled"]) {
                NS_DURING
        if ([df boolForKey:@"audioscrobblerEnabled"]) {
                NS_DURING
-                       NSString *title = [[self currentRemote] currentSongTitle], *artist = [[self currentRemote] currentSongArtist];
-                       if (title && artist) {
-                               [[AudioscrobblerController sharedController] submitTrack:title
-                                                                                                                               artist:artist
-                                                                                                                               album:[[self currentRemote] currentSongAlbum]
-                                                                                                                               length:[[self currentRemote] currentSongDuration]];
+                       int elapsed = [[self currentRemote] currentSongPlayed];
+                       if ((abs(elapsed - interval) < 5) && ([[self currentRemote] playerPlayingState] == ITMTRemotePlayerPlaying)) {
+                               NSString *title = [[self currentRemote] currentSongTitle], *artist = [[self currentRemote] currentSongArtist];
+                               if (title && artist) {
+                                       ITDebugLog(@"Audioscrobbler: Submitting current track");
+                                       [[AudioscrobblerController sharedController] submitTrack:title
+                                                                                                                                       artist:artist
+                                                                                                                                       album:[[self currentRemote] currentSongAlbum]
+                                                                                                                                       length:[[self currentRemote] currentSongDuration]];
+                               }
+                       } else if (interval - elapsed > 0) {
+                               ITDebugLog(@"Audioscrobbler: Creating a new timer that will run in %i seconds", interval - elapsed);
+                               _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:(interval - elapsed) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES];
                        }
                NS_HANDLER
                        [self networkError:localException];
                NS_ENDHANDLER
                        }
                NS_HANDLER
                        [self networkError:localException];
                NS_ENDHANDLER
-               [timer invalidate];
-               _audioscrobblerTimer = nil;
        }
 }
 
        }
 }
 
index f8245d2..61e0c11 100755 (executable)
@@ -1105,6 +1105,7 @@ static PreferencesController *prefs = nil;
                        [audioscrobblerPasswordTextField setStringValue:password];
                }
        }
                        [audioscrobblerPasswordTextField setStringValue:password];
                }
        }
+       [audioscrobblerUseCacheCheckbox setState:[df boolForKey:@"audioscrobblerCacheSubmissions"]];
        
     [[NSNotificationCenter defaultCenter] addObserver:sharingTableView selector:@selector(reloadData) name:@"ITMTFoundNetService" object:nil];
     
        
     [[NSNotificationCenter defaultCenter] addObserver:sharingTableView selector:@selector(reloadData) name:@"ITMTFoundNetService" object:nil];
     
index 11ba443..dbd21fb 100755 (executable)
     return temp2;
 }
 
     return temp2;
 }
 
+- (int)currentSongPlayed
+{
+       UInt32 final;
+    ITDebugLog(@"Getting current song played time.");
+       final = [ITSendAEWithKey('pPos', 'core', 'getd', &savedPSN) int32Value];
+    ITDebugLog(@"Getting current song played time done.");
+    return final;
+}
+
 - (int)currentSongDuration
 {
     SInt32 temp1;
 - (int)currentSongDuration
 {
     SInt32 temp1;