From b321903333078589d1ebd248f275b97ea85c4e29 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Fri, 18 Sep 2015 12:41:15 +0200 Subject: [PATCH] Use NSJSONSerialization instead of JSONKit --- Classes/MetadataImporters/TheMovieDB3.m | 29 +++++------ Classes/MetadataImporters/iTunesStore.m | 66 ++++++++++++++----------- 2 files changed, 53 insertions(+), 42 deletions(-) diff --git a/Classes/MetadataImporters/TheMovieDB3.m b/Classes/MetadataImporters/TheMovieDB3.m index 3cfc5e3..053c6f6 100755 --- a/Classes/MetadataImporters/TheMovieDB3.m +++ b/Classes/MetadataImporters/TheMovieDB3.m @@ -9,7 +9,6 @@ #import #import #import -#import #import "SBMetadataSearchController.h" @@ -29,9 +28,10 @@ NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.themoviedb.org/3/search/movie?api_key=%@&query=%@&language=%@", API_KEY, [MetadataImporter urlEncoded:aMovieTitle], lang]]; NSData *jsonData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy]; if (jsonData) { - JSONDecoder *jsonDecoder = [JSONDecoder decoder]; - NSDictionary *d = [jsonDecoder objectWithData:jsonData]; - return [TheMovieDB3 metadataForResults:d]; + NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; + if ([d isKindOfClass:[NSDictionary class]]) { + return [TheMovieDB3 metadataForResults:d]; + } } return nil; } @@ -56,10 +56,9 @@ NSArray *posters = [r valueForKeyPath:@"images.posters"]; if (jsonData && posters && [posters isKindOfClass:[NSArray class]]) { - JSONDecoder *jsonDecoder = [JSONDecoder decoder]; - NSDictionary *config = [jsonDecoder objectWithData:jsonData]; + NSDictionary *config = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; - if ([config valueForKey:@"images"]) { + if ([config isKindOfClass:[NSDictionary class]] && [config valueForKey:@"images"]) { NSString *imageBaseUrl = [[config valueForKey:@"images"] valueForKey:@"secure_base_url"]; NSString *posterThumbnailSize = [[[config valueForKey:@"images"] valueForKey:@"poster_sizes"] objectAtIndex:0]; NSString *backdropThumbnailSize = [[[config valueForKey:@"images"] valueForKey:@"backdrop_sizes"] objectAtIndex:0]; @@ -102,13 +101,15 @@ NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.themoviedb.org/3/movie/%@?api_key=%@&language=%@&append_to_response=casts,releases,images", theMovieDBID, API_KEY, lang]]; NSData *jsonData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy]; if (jsonData) { - JSONDecoder *jsonDecoder = [JSONDecoder decoder]; - NSDictionary *d = [jsonDecoder objectWithData:jsonData]; - MP42Metadata *r = [TheMovieDB3 metadataForResult:d language:lang]; - if (r) { - [aMetadata mergeMetadata:r]; - } - aMetadata = [self artworksForResult:d metadata:aMetadata]; + NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; + if ([d isKindOfClass:[NSDictionary class]]) { + + MP42Metadata *r = [TheMovieDB3 metadataForResult:d language:lang]; + if (r) { + [aMetadata mergeMetadata:r]; + } + aMetadata = [self artworksForResult:d metadata:aMetadata]; + } } return aMetadata; diff --git a/Classes/MetadataImporters/iTunesStore.m b/Classes/MetadataImporters/iTunesStore.m index 8384dba..dd57556 100755 --- a/Classes/MetadataImporters/iTunesStore.m +++ b/Classes/MetadataImporters/iTunesStore.m @@ -8,7 +8,6 @@ #import #import -#import #import "iTunesStore.h" #import "SBMetadataSearchController.h" @@ -17,26 +16,40 @@ #pragma mark iTunes stores -- (NSArray *) languages { - NSString* iTunesStoresJSON = [[NSBundle mainBundle] pathForResource:@"iTunesStores" ofType:@"json"]; - JSONDecoder *jsonDecoder = [JSONDecoder decoder]; - NSArray *iTunesStores = [jsonDecoder objectWithData:[NSData dataWithContentsOfFile:iTunesStoresJSON]]; - NSMutableArray *results = [[NSMutableArray alloc] initWithCapacity:[iTunesStores count]]; - for (NSDictionary *store in iTunesStores) { - [results addObject:[NSString stringWithFormat:@"%@ (%@)", [store valueForKey:@"country"], [store valueForKey:@"language"]]]; - } +- (NSArray *)languages { + NSString *iTunesStoresJSON = [[NSBundle mainBundle] pathForResource:@"iTunesStores" ofType:@"json"]; + NSData *data = [NSData dataWithContentsOfFile:iTunesStoresJSON]; + + NSMutableArray *results = [[NSMutableArray alloc] init]; + + if (data) { + NSArray *iTunesStores = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + + if ([iTunesStores isKindOfClass:[NSArray class]]) { + for (NSDictionary *store in iTunesStores) { + [results addObject:[NSString stringWithFormat:@"%@ (%@)", [store valueForKey:@"country"], [store valueForKey:@"language"]]]; + } + } + } + return [results autorelease]; } -+ (NSDictionary *) getStoreFor:(NSString *)aLanguageString { - NSString* iTunesStoresJSON = [[NSBundle mainBundle] pathForResource:@"iTunesStores" ofType:@"json"]; - JSONDecoder *jsonDecoder = [JSONDecoder decoder]; - NSArray *iTunesStores = [jsonDecoder objectWithData:[NSData dataWithContentsOfFile:iTunesStoresJSON]]; - for (NSDictionary *store in iTunesStores) { - if (aLanguageString && [aLanguageString isEqualToString:[NSString stringWithFormat:@"%@ (%@)", [store valueForKey:@"country"], [store valueForKey:@"language"]]]) { - return store; - } - } ++ (NSDictionary *)getStoreFor:(NSString *)aLanguageString { + NSString *iTunesStoresJSON = [[NSBundle mainBundle] pathForResource:@"iTunesStores" ofType:@"json"]; + NSData *data = [NSData dataWithContentsOfFile:iTunesStoresJSON]; + + if (data) { + NSArray *iTunesStores = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; + + if ([iTunesStores isKindOfClass:[NSArray class]]) { + for (NSDictionary *store in iTunesStores) { + if (aLanguageString && [aLanguageString isEqualToString:[NSString stringWithFormat:@"%@ (%@)", [store valueForKey:@"country"], [store valueForKey:@"language"]]]) { + return store; + } + } + } + } return nil; } @@ -119,9 +132,10 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context) url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/search?country=%@&lang=%@&term=%@&attribute=showTerm&entity=tvEpisode&limit=200", country, [language lowercaseString], [MetadataImporter urlEncoded:aSeriesName]]]; } NSData *jsonData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy]; + if (jsonData) { - JSONDecoder *jsonDecoder = [JSONDecoder decoder]; - NSDictionary *d = [jsonDecoder objectWithData:jsonData]; + NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; + if ([d isKindOfClass:[NSDictionary class]]) { NSArray *results = [iTunesStore metadataForResults:d store:store]; if (([results count] == 0) && ![aLanguage isEqualToString:@"USA (English)"]) { @@ -158,8 +172,7 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context) NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/search?country=%@&lang=%@&term=%@&entity=tvEpisode", country, [language lowercaseString], [MetadataImporter urlEncoded:[NSString stringWithFormat:@"%@ %@", aSeriesName, aEpisodeTitle]]]]; NSData *jsonData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy]; if (jsonData) { - JSONDecoder *jsonDecoder = [JSONDecoder decoder]; - NSDictionary *d = [jsonDecoder objectWithData:jsonData]; + NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; if ([d isKindOfClass:[NSDictionary class]]) { NSArray *results = [iTunesStore metadataForResults:d store:store]; return [results firstObject]; @@ -178,8 +191,7 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context) NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/search?country=%@&lang=%@&term=%@&entity=movie", country, language, [MetadataImporter urlEncoded:aMovieName]]]; NSData *jsonData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy]; if (jsonData) { - JSONDecoder *jsonDecoder = [JSONDecoder decoder]; - NSDictionary *d = [jsonDecoder objectWithData:jsonData]; + NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; if ([d isKindOfClass:[NSDictionary class]]) { NSArray *results = [iTunesStore metadataForResults:d store:store]; return [results firstObject]; @@ -203,8 +215,7 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context) NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/search?country=%@&lang=%@&term=%@&entity=movie&limit=150", country, language, [MetadataImporter urlEncoded:aMovieTitle]]]; NSData *jsonData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy]; if (jsonData) { - JSONDecoder *jsonDecoder = [JSONDecoder decoder]; - NSDictionary *d = [jsonDecoder objectWithData:jsonData]; + NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; if ([d isKindOfClass:[NSDictionary class]]) { return [iTunesStore metadataForResults:d store:store]; } @@ -224,8 +235,7 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context) NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/lookup?country=%@&lang=%@&id=%@", country, [language lowercaseString], [[aMetadata tagsDict] valueForKey:@"playlistID"]]]; NSData *jsonData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy]; if (jsonData) { - JSONDecoder *jsonDecoder = [JSONDecoder decoder]; - NSDictionary *d = [jsonDecoder objectWithData:jsonData]; + NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; if ([d isKindOfClass:[NSDictionary class]]) { NSArray *resultsArray = [d valueForKey:@"results"]; if ([resultsArray count] > 0) {