Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
Fixed issue #4 and #6
Browse files Browse the repository at this point in the history
  • Loading branch information
johan committed Sep 10, 2013
1 parent d7d58a2 commit b32734e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion Traktable/ITApi.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ @implementation ITApi

- (NSString *)sha1Hash:(NSString *)input {

const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
const char *cstr = [input UTF8String];
NSData *data = [NSData dataWithBytes:cstr length:input.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];

Expand Down
31 changes: 14 additions & 17 deletions Traktable/ITLibrary.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
@interface ITLibrary()

- (id)init;
- (NSArray *)checkTracks:(NSArray *)tracks;
- (void)checkTracks:(NSArray *)tracks;
- (void)createDir:(NSString *)dir;

@property dispatch_queue_t queue;
@property dispatch_group_t dispatchGroup;

@end

@implementation ITLibrary

@synthesize iTunesBridge;
@synthesize dbQueue=_dbQueue;
@synthesize queue=_queue;
@synthesize dbFilePath;
@synthesize firstImport;

Expand All @@ -47,6 +47,7 @@ - (id)init {

_dbQueue = [FMDatabaseQueue databaseQueueWithPath:dbFilePath];
_queue = dispatch_queue_create("traktable.sync.queue", NULL);
_dispatchGroup = dispatch_group_create();

[self.dbQueue inDatabase:^(FMDatabase *db) {
FMResultSet *s = [db executeQuery:@"SELECT playedCount FROM library"];
Expand Down Expand Up @@ -118,16 +119,12 @@ - (iTunesTrack *)getTrack:(NSString *)persistentID type:(iTunesESpK)videoType {

- (void)importLibrary {

ITApi *api = [[ITApi alloc] init];
firstImport = YES;

NSArray *movies = [self getVideos:iTunesESpKMovies noCheck:YES];
NSArray *shows = [self getVideos:iTunesESpKTVShows noCheck:YES];

NSArray *seenMovies = [self checkTracks:movies];
if([seenMovies count] > 0)
[api seen:seenMovies type:iTunesEVdKMovie video:nil];

[self checkTracks:movies];
[self checkTracks:shows];

dispatch_sync(_queue, ^{
Expand All @@ -141,23 +138,19 @@ - (void)syncLibrary {
[self init];
return;
}

ITApi *api = [[ITApi alloc] init];


NSArray *movies = [self getVideos:iTunesESpKMovies noCheck:NO];
NSArray *shows = [self getVideos:iTunesESpKTVShows noCheck:NO];

firstImport = NO;

NSArray *seenMovies = [self checkTracks:movies];
if([seenMovies count] > 0)
[api seen:seenMovies type:iTunesEVdKMovie video:nil];

[self checkTracks:movies];
[self checkTracks:shows];

[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"ITLastSyncDate"];
}

- (NSArray *)checkTracks:(NSArray *)tracks {
- (void)checkTracks:(NSArray *)tracks {

NSMutableArray *seenVideos = [[NSMutableArray alloc] init];
ITVideo *video = [ITVideo new];
Expand All @@ -168,7 +161,8 @@ - (NSArray *)checkTracks:(NSArray *)tracks {

__block id playedCount;

dispatch_async(_queue, ^{
dispatch_group_wait(self.dispatchGroup, DISPATCH_TIME_FOREVER);
dispatch_group_async(self.dispatchGroup, self.queue, ^{

iTunesTrack *track = [tracks objectAtIndex:i];

Expand Down Expand Up @@ -249,7 +243,10 @@ - (NSArray *)checkTracks:(NSArray *)tracks {

}

return seenVideos;
dispatch_group_notify(self.dispatchGroup, self.queue, ^{
if([seenVideos count] > 0)
[api seen:seenVideos type:iTunesEVdKMovie video:nil];
});
}

- (void)updateTrackCount:(iTunesTrack *)track scrobbled:(BOOL)scrobble {
Expand Down

0 comments on commit b32734e

Please sign in to comment.