Skip to content

Commit

Permalink
Resolves #22
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanebruckert committed Jan 30, 2016
1 parent 8b8eb49 commit 7778a30
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 22 deletions.
66 changes: 47 additions & 19 deletions ShazamScrobbler/LastFmController.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,16 @@ + (bool)logout {
}

+ (void)nowPlaying:(Song*)song withTag:(NSInteger)tag {
if (lastShazamTag == tag) {
MenuController *menu = ((AppDelegate *)[NSApplication sharedApplication].delegate).menu ;
// We are going to need the not-to-scrobble list
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *notToScrobble = [[NSMutableArray alloc] initWithArray:[userDefaults objectForKey:@"notToScrobble"]];

MenuController *menu = ((AppDelegate *)[NSApplication sharedApplication].delegate).menu;
if ([notToScrobble containsObject: [NSNumber numberWithLong:tag]]){
NSMenuItem* item = [menu.main itemWithTag:tag];
[item setState:NSMixedState];
} else if (lastShazamTag == tag) {
// This is most likely the now playing song

// This restricts the previous song to be scrobbled
// if not played more than 'PLAYTIME' seconds
Expand All @@ -79,27 +87,47 @@ + (void)nowPlaying:(Song*)song withTag:(NSInteger)tag {
[operationQueue cancelAllOperations];
}

NSInteger seconds = [NowPlayingOperation secondsBeforeNowPlayingEnds:song.date];

// The song is displayed as "now playing" on last.fm for the next 'PLAYTIME' seconds
[[LastFm sharedInstance] sendNowPlayingTrack:song.song byArtist:song.artist onAlbum:nil withDuration:seconds successHandler:^(NSDictionary *result) {} failureHandler:^(NSError *error) {
// #ISSUE-22
[[LastFm sharedInstance] getRecentTracksForUserOrNil:[LastFm sharedInstance].username limit:1 successHandler:^(NSArray *result) {
// When we have 2 results it means that a song is currently playing on the last.fm user profile
if ((unsigned long)[result count] == 2) {
// A different scrobbler than ShazamScrobbler is "now playing"
// We don't want to scrobble the song detected by Shazam
// But will add the song to a list so that it will never be scrobbled
[notToScrobble addObject:[NSNumber numberWithLong:tag]];
[userDefaults setObject:notToScrobble forKey:@"notToScrobble"];
[userDefaults synchronize];

// Because the song was not scrobbled, we want to give it a grey icon
NSMenuItem* item = [menu.main itemWithTag:tag];
[item setState:NSMixedState];
} else {
// Scrobble the song if played more than 'PLAYTIME' seconds
// Will be cancelled if another song is played before
NowPlayingOperation *operation = [[NowPlayingOperation alloc] initWithSong:song successHandler:^() {
[LastFmController scrobble:song withTag:tag];
[menu setNowPlaying:false];
} failureHandler:^() {
// Song can't be scrobbled because it wasn't played more than 30 seconds
NSMenuItem* item = [menu.main itemWithTag:tag];
[item setState:NSMixedState];
}];
[operationQueue addOperation:operation];
[menu setNowPlaying:true];
};
} failureHandler:^(NSError *error) {
NSLog(@"Now playing error: %@", error);
}];

// This scrobbles the song if played more than 'PLAYTIME' seconds
// Will be cancelled if another song is played before
NowPlayingOperation *operation = [[NowPlayingOperation alloc] initWithSong:song successHandler:^() {
// Scrobble a track
[LastFmController scrobble:song withTag:tag];
[menu setNowPlaying:false];
} failureHandler:^() {
// Song can't be scrobbled because it wasn't played more than 30 seconds
NSMenuItem* item = [menu.main itemWithTag:tag];
[item setState:NSMixedState];
}];
[operationQueue addOperation:operation];
[menu setNowPlaying:true];
// // ROLLED BACK, we can't do this anymore (for now)
// NSInteger seconds = [NowPlayingOperation secondsBeforeNowPlayingEnds:song.date];
//
// // The song is displayed as "now playing" on last.fm for the next 'PLAYTIME' seconds
// [[LastFm sharedInstance] sendNowPlayingTrack:song.song byArtist:song.artist onAlbum:nil withDuration:seconds successHandler:^(NSDictionary *result) {} failureHandler:^(NSError *error) {
// NSLog(@"Now playing error: %@", error);
// }];
} else {
// This item was not in the not-to-scrobble list and could be scrobbled
[LastFmController scrobble:song withTag:tag];
}
}
Expand Down
9 changes: 6 additions & 3 deletions ShazamScrobbler/ShazamController.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ + (bool)init {
}
followingSong = currentSong;

NSMutableArray *notToScrobble = [[NSMutableArray alloc] initWithArray:[prefs objectForKey:@"notToScrobble"]];

// Set a different icon depending on the song status
NSMenuItem* item = [menu insertSong:currentSong withIndex:SONGS_START_INDEX + i++];
if (currentSong.tag > [prefs integerForKey:@"lastScrobble"]) {
// unscrobbled
// unscrobbled (waiting status)
[item setState:NSOffState];
} else if (timeIntervalWithFollowing < PLAYTIME) {
// not played long enough
} else if (timeIntervalWithFollowing < PLAYTIME || [notToScrobble containsObject: [NSNumber numberWithLong:currentSong.tag]]) {
// not played long enough or appearing in the not-to-scrobble list
// (most probably because another song was "now playing" at the Shazam tag time)
[item setState:NSMixedState];
} else {
// scrobbled
Expand Down

0 comments on commit 7778a30

Please sign in to comment.