diff --git a/CHANGELOG.md b/CHANGELOG.md index fdcc814..5e2791c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## CHANGELOG +### Unreleased + +- Scrobble with album name [issue #37](https://github.com/ShazamScrobbler/shazamscrobbler-macos/issues/37) + ### 1.2.3 (2017/01/26) - Fixed scrobbling that would not work in some cases - Fixed an issue where in some cases we would read the Shazam DB before it finishes storing the new tag information diff --git a/ShazamScrobbler/LastFmController.m b/ShazamScrobbler/LastFmController.m index 0b19535..0fa4c07 100644 --- a/ShazamScrobbler/LastFmController.m +++ b/ShazamScrobbler/LastFmController.m @@ -132,7 +132,7 @@ + (void)scrobble:(Song *)song withTag:(NSInteger)tag { if (seconds <= 0) { // Scrobble a track - [[LastFm sharedInstance] sendScrobbledTrack:song.song byArtist:song.artist onAlbum:nil withDuration:30 atTimestamp:(int)[song.date timeIntervalSince1970] successHandler:^(NSDictionary *result) { + [[LastFm sharedInstance] sendScrobbledTrack:song.song byArtist:song.artist onAlbum:song.album withDuration:30 atTimestamp:(int)[song.date timeIntervalSince1970] successHandler:^(NSDictionary *result) { // We need to re-check if the user is connected and the scrobbling is enabled // in case the configuration changed during the last 30 seconds diff --git a/ShazamScrobbler/MenuController.m b/ShazamScrobbler/MenuController.m index 987b2eb..5a4933e 100644 --- a/ShazamScrobbler/MenuController.m +++ b/ShazamScrobbler/MenuController.m @@ -75,11 +75,11 @@ - (IBAction)openAboutView:(id)sender } -(NSMenuItem*)insertResultSet:(FMResultSet*)rs withIndex:(int)i { - NSString *artist = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"ZNAME"]]; - NSString *track = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"ZTRACKNAME"]]; + NSString *artist = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"artist"]]; + NSString *track = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"track"]]; NSMenuItem * menuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"%@ - %@", artist, track] action:@selector(negateItem:) keyEquivalent:@""]; - menuItem.tag = [rs intForColumn:@"ZID"]; + menuItem.tag = [rs intForColumn:@"id"]; // state pictures NSImage* onState = [NSImage imageNamed:@"NSOnState"]; @@ -152,7 +152,7 @@ - (void)setNowPlaying:(bool)isNowPlaying { } - (void)insert:(FMResultSet*)rs { - if ([_main itemWithTag:[rs intForColumn:@"ZID"]] == nil) { + if ([_main itemWithTag:[rs intForColumn:@"id"]] == nil) { [self insertResultSet:rs withIndex:SONGS_START_INDEX]; if (_itemCount >= SONGS_LENGTH) { // If a song is "now playing", the list has one more element diff --git a/ShazamScrobbler/ShazamController.m b/ShazamScrobbler/ShazamController.m index 2d09581..547abd5 100644 --- a/ShazamScrobbler/ShazamController.m +++ b/ShazamScrobbler/ShazamController.m @@ -43,7 +43,11 @@ + (bool)init { if ([prefs integerForKey:@"lastScrobble"] < 0 || [prefs integerForKey:@"lastScrobble"] > last) { [prefs setInteger:last forKey:@"lastScrobble"]; }; - FMResultSet *rs = [database executeQuery:[NSString stringWithFormat:@"SELECT track.Z_PK as ZID, ZDATE, ZTRACKNAME, ZNAME FROM ZSHARTISTMO artist, ZSHTAGRESULTMO track WHERE artist.ZTAGRESULT = track.Z_PK ORDER BY ZID DESC LIMIT %d", SONGS_LENGTH]]; + FMResultSet *rs = [database executeQuery:[NSString stringWithFormat:@"\ + SELECT track.Z_PK as id, track.ZDATE as timestamp, track.ZTRACKNAME as track, artist.ZNAME as artist, album.ZVALUE as album \ + FROM ZSHARTISTMO artist, ZSHTAGRESULTMO track, ZSHMETADATAMO album \ + WHERE artist.ZTAGRESULT = track.Z_PK AND track.Z_PK = album.ZTAGRESULT AND album.ZKEY = 'Album' \ + ORDER BY id DESC LIMIT %d", SONGS_LENGTH]]; MenuController *menu = ((AppDelegate *)[NSApplication sharedApplication].delegate).menu; NSMutableArray* lastSongsArray = [[NSMutableArray alloc] initWithCapacity:SONGS_LENGTH]; @@ -139,7 +143,12 @@ + (void)findNewTags { lastShazamTag = [shazamLastTag intForColumn:@"Z_PK"]; // Get Shazam tags since the last Scrobble to last.fm - FMResultSet *shazamTagsSinceLastScrobble = [database executeQuery:[NSString stringWithFormat:@"select track.Z_PK as ZID, ZDATE, ZTRACKNAME, ZNAME from ZSHARTISTMO artist, ZSHTAGRESULTMO track where artist.ZTAGRESULT = track.Z_PK and track.Z_PK > %ld", lastScrobblePosition]]; + FMResultSet *shazamTagsSinceLastScrobble = [database executeQuery:[NSString stringWithFormat:@"\ + SELECT track.Z_PK as id, track.ZDATE as timestamp, track.ZTRACKNAME as track, artist.ZNAME as artist, album.ZVALUE as album \ + FROM ZSHARTISTMO artist, ZSHTAGRESULTMO track, ZSHMETADATAMO album \ + WHERE artist.ZTAGRESULT = track.Z_PK \ + AND track.Z_PK = album.ZTAGRESULT AND album.ZKEY = 'Album' \ + AND track.Z_PK > %ld", lastScrobblePosition]]; // While a new Shazam tag is found while ([shazamTagsSinceLastScrobble next]) { @@ -150,7 +159,7 @@ + (void)findNewTags { NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; if ([prefs integerForKey:@"scrobbling"] && [prefs stringForKey:@"session"] != nil) { Song *song = [[Song alloc] initWithResultSet:shazamTagsSinceLastScrobble]; - [LastFmController nowPlaying:song withTag:[shazamTagsSinceLastScrobble intForColumn:@"ZID"]]; + [LastFmController nowPlaying:song withTag:[shazamTagsSinceLastScrobble intForColumn:@"id"]]; } else { unscrobbledCount++; } @@ -167,7 +176,12 @@ + (Song*)createSongFromTag:(NSInteger)tag { FMDatabase *database = [FMDatabase databaseWithPath:[ShazamConstants getSqlitePath]]; if([database open]) { - FMResultSet *songWithGivenTag = [database executeQuery:[NSString stringWithFormat:@"select track.Z_PK as ZID, ZDATE, ZTRACKNAME, ZNAME from ZSHARTISTMO artist, ZSHTAGRESULTMO track where ZID = %ld", tag]]; + FMResultSet *songWithGivenTag = [database executeQuery:[NSString stringWithFormat:@"\ + SELECT track.Z_PK as id, track.ZDATE as timestamp, track.ZTRACKNAME as track, artist.ZNAME as artist, album.ZVALUE as album \ + FROM ZSHTAGRESULTMO track \ + JOIN ZSHARTISTMO artist ON track.Z_PK = artist.Z_PK \ + JOIN ZSHMETADATAMO album ON track.Z_PK = album.ZTAGRESULT \ + WHERE album.ZKEY = 'Album' and track.Z_PK = %ld", tag]]; [database close]; return [[Song alloc] initWithResultSet:songWithGivenTag]; } diff --git a/ShazamScrobbler/Song.h b/ShazamScrobbler/Song.h index c01fa27..ef09946 100644 --- a/ShazamScrobbler/Song.h +++ b/ShazamScrobbler/Song.h @@ -20,15 +20,17 @@ @property (copy, nonatomic) NSString *artist; @property (copy, nonatomic) NSString *song; @property (copy, nonatomic) NSDate *date; +@property (copy, nonatomic) NSString *album; @property NSInteger tag; @property (nonatomic) bool scrobbled; -- (id)initWithSong:(NSString *)song artist:(NSString *)artist date:(NSDate *)date tag:(NSInteger)tag; +- (id)initWithSong:(NSString *)song artist:(NSString *)artist date:(NSDate *)date album:(NSString *)album tag:(NSInteger)tag; - (id)initWithResultSet:(FMResultSet *)rs; + (void)setDefaultModel:(NSString *)aModel; - (void)setArtist:(NSString *)aArtist; - (void)setDate:(NSDate *)aDate; +- (void)setAlbum:(NSString *)aAlbum; - (NSString *)description; @end diff --git a/ShazamScrobbler/Song.m b/ShazamScrobbler/Song.m index fb6ba6e..0a43488 100644 --- a/ShazamScrobbler/Song.m +++ b/ShazamScrobbler/Song.m @@ -13,11 +13,12 @@ @implementation Song -- (id)initWithSong:(NSString *)song artist:(NSString *)artist date:(NSDate *)date tag:(NSInteger)tag{ +- (id)initWithSong:(NSString *)song artist:(NSString *)artist date:(NSDate *)date album:(NSString *)album tag:(NSInteger)tag{ self = [super init]; if (self) { _song = [song copy]; _artist = artist; + _album = album; _scrobbled = NO; _date = date; _tag = tag; @@ -27,23 +28,20 @@ - (id)initWithSong:(NSString *)song artist:(NSString *)artist date:(NSDate *)dat - (id)initWithResultSet:(FMResultSet *)rs { self = [super init]; - NSInteger tag = [rs intForColumn:@"ZID"]; - NSString *artist = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"ZNAME"]]; - NSString *track = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"ZTRACKNAME"]]; - NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:[[rs stringForColumn:@"ZDATE"] doubleValue]]; if (self) { - _song = [track copy]; - _artist = [artist copy]; - _scrobbled = NO; - _date = [date copy]; - _tag = tag; + NSInteger tag = [rs intForColumn:@"id"]; + NSString *artist = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"artist"]]; + NSString *track = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"track"]]; + NSString *album = [NSString stringWithFormat:@"%@",[rs stringForColumn:@"album"]]; + NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:[[rs stringForColumn:@"timestamp"] doubleValue]]; + self = [self initWithSong:track artist:artist date:date album:album tag:tag]; } return self; } - (id)init { // Forward to the "designated" initialization method - return [self initWithSong:_defaultModel artist:nil date:nil tag:0]; + return [self initWithSong:_defaultModel artist:nil date:nil album:nil tag:0]; } + (void)setDefaultModel:(NSString *)aModel { @@ -58,8 +56,12 @@ - (void)setDate:(NSDate *)aDate { _date = [aDate copy]; } +- (void)setAlbum:(NSString *)aAlbum { + _album = [aAlbum copy]; +} + - (NSString *)description { - return [NSString stringWithFormat:@"Date=%@; Artist=%@; Song=%@",_date, _artist, _song]; + return [NSString stringWithFormat:@"Date=%@; Artist=%@; Song=%@; Album=%@",_date, _artist, _song, _album]; } -@end \ No newline at end of file +@end