#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
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];
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;
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;
- (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
- 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
- [timer invalidate];
- _audioscrobblerTimer = nil;
}
}
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;