Skip to content

Commit

Permalink
Scrobble with album name, fixes #37 (#38)
Browse files Browse the repository at this point in the history
* scrape album info for scrobbles (#37)

* Simplify all queries

* Update changelog

Co-authored-by: Stephane Bruckert <stephane.bruckert@gmail.com>
  • Loading branch information
neilmenon and stephanebruckert authored Nov 7, 2021
1 parent da00524 commit 411b536
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion ShazamScrobbler/LastFmController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions ShazamScrobbler/MenuController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -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
Expand Down
22 changes: 18 additions & 4 deletions ShazamScrobbler/ShazamController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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]) {
Expand All @@ -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++;
}
Expand All @@ -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];
}
Expand Down
4 changes: 3 additions & 1 deletion ShazamScrobbler/Song.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 15 additions & 13 deletions ShazamScrobbler/Song.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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
@end

0 comments on commit 411b536

Please sign in to comment.