Merge english metadata manually in SBTheTVDB is needed.
This commit is contained in:
1 parent
afb57a2052
commit
8f396fcca2
1 file changed
+245
-113
@@ -16,6 +16,8 @@
|
||||
|
||||
@implementation SBTheTVDB
|
||||
|
||||
#pragma mark - Public methods
|
||||
|
||||
- (NSArray<NSString *> *)languages
|
||||
{
|
||||
return SBTheTVDBConnection.defaultManager.languagues;
|
||||
@@ -60,6 +62,121 @@
|
||||
return results.allObjects;
|
||||
}
|
||||
|
||||
- (NSArray<SBMetadataResult *> *)searchTVSeries:(NSString *)seriesName language:(NSString *)language seasonNum:(NSString *)seasonNum episodeNum:(NSString *)episodeNum
|
||||
{
|
||||
if (!language) { language = @"en"; }
|
||||
|
||||
// search for series id
|
||||
NSSet<NSNumber *> *seriesIDs = [self searchSeriesID:seriesName language:language];
|
||||
|
||||
// fallback to english if the no results are found
|
||||
if (!seriesIDs.count) {
|
||||
seriesIDs = [self searchSeriesID:seriesName language:@"en"];
|
||||
}
|
||||
|
||||
NSMutableArray *results = [[NSMutableArray alloc] init];
|
||||
|
||||
for (NSDictionary *s in seriesIDs) {
|
||||
|
||||
NSNumber *seriesID = s[@"id"];
|
||||
|
||||
if (!seriesID || ![seriesID isKindOfClass:[NSNumber class]]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Series info
|
||||
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@", seriesID]];
|
||||
NSData *seriesInfoJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||
|
||||
if (!seriesInfoJSON) { continue; }
|
||||
|
||||
NSDictionary *seriesInfo = [NSJSONSerialization JSONObjectWithData:seriesInfoJSON options:0 error:NULL];
|
||||
|
||||
if (!seriesInfo || ![seriesInfo isKindOfClass:[NSDictionary class]]) { continue; }
|
||||
|
||||
seriesInfo = seriesInfo[@"data"];
|
||||
|
||||
if (!seriesInfo || ![seriesInfo isKindOfClass:[NSDictionary class]]) { continue; }
|
||||
|
||||
// Series actors
|
||||
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/actors", seriesID]];
|
||||
NSData *seriesActorsJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||
|
||||
if (!seriesActorsJSON) { continue; }
|
||||
|
||||
NSDictionary *seriesActorsDictionary = [NSJSONSerialization JSONObjectWithData:seriesActorsJSON options:0 error:NULL];
|
||||
|
||||
if (!seriesActorsDictionary || ![seriesActorsDictionary isKindOfClass:[NSDictionary class]]) { continue; }
|
||||
|
||||
NSArray<NSDictionary *> *seriesActors = seriesActorsDictionary[@"data"];
|
||||
|
||||
if (!seriesActors || ![seriesActors isKindOfClass:[NSArray class]]) { continue; }
|
||||
|
||||
// Series episodes info
|
||||
NSArray<SBMetadataResult *> *localizedResults = [self loadTVEpisodes:seriesInfo actors:seriesActors
|
||||
seasonNum:seasonNum episodeNum:episodeNum language:language];
|
||||
|
||||
// Check for null values, it might happens if there isn't a localized version
|
||||
SBTheTVDBNullValues nullValues = [self checkForNull:localizedResults];
|
||||
|
||||
if (nullValues) {
|
||||
if (nullValues & SBTheTVDBNullValuesEpisodes) {
|
||||
NSArray<SBMetadataResult *> *englishResults = [self loadTVEpisodes:seriesInfo actors:seriesActors
|
||||
seasonNum:seasonNum episodeNum:episodeNum language:@"en"];
|
||||
|
||||
if (englishResults) {
|
||||
[self merge:localizedResults with:englishResults];
|
||||
}
|
||||
}
|
||||
|
||||
if (nullValues & SBTheTVDBNullValuesSeries) {
|
||||
NSURL *englishUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@", seriesID]];
|
||||
NSData *englishSeriesInfoJSON = [SBTheTVDBConnection.defaultManager requestData:englishUrl language:@"en"];
|
||||
|
||||
if (englishSeriesInfoJSON) {
|
||||
|
||||
NSDictionary *englishSeriesInfo = [NSJSONSerialization JSONObjectWithData:englishSeriesInfoJSON options:0 error:NULL];
|
||||
|
||||
if (englishSeriesInfo || [englishSeriesInfo isKindOfClass:[NSDictionary class]]) {
|
||||
englishSeriesInfo = englishSeriesInfo[@"data"];
|
||||
[self merge:localizedResults withSeries:englishSeriesInfo];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[results addObjectsFromArray:localizedResults];
|
||||
}
|
||||
|
||||
NSArray<SBMetadataResult *> *resultsSorted = [results sortedArrayUsingComparator:^NSComparisonResult(SBMetadataResult *ep1, SBMetadataResult *ep2) {
|
||||
int v1 = [ep1[SBMetadataResultEpisodeNumber] intValue];
|
||||
int v2 = [ep2[SBMetadataResultEpisodeNumber] intValue];
|
||||
|
||||
int s1 = [ep1[SBMetadataResultSeason] intValue];
|
||||
int s2 = [ep2[SBMetadataResultSeason] intValue];
|
||||
|
||||
if (s1 == s2) {
|
||||
if (v1 < v2)
|
||||
return NSOrderedAscending;
|
||||
else if (v1 > v2)
|
||||
return NSOrderedDescending;
|
||||
}
|
||||
|
||||
if (s1 < s2)
|
||||
return NSOrderedAscending;
|
||||
else if (s1 > s2)
|
||||
return NSOrderedDescending;
|
||||
else
|
||||
return NSOrderedSame;
|
||||
}];
|
||||
|
||||
return resultsSorted;
|
||||
}
|
||||
|
||||
#pragma mark - Series ID
|
||||
|
||||
- (BOOL)seriesResult:(NSDictionary *)s matchName:(NSString *)seriesName
|
||||
{
|
||||
if ([seriesName isEqualTo:s[@"seriesName"]]) {
|
||||
@@ -111,128 +228,90 @@
|
||||
return [selectedSeries copy];
|
||||
}
|
||||
|
||||
- (NSArray<SBMetadataResult *> *)searchTVSeries:(NSString *)seriesName language:(NSString *)language seasonNum:(NSString *)seasonNum episodeNum:(NSString *)episodeNum
|
||||
#pragma mark - Null values check
|
||||
|
||||
typedef NS_OPTIONS(NSUInteger, SBTheTVDBNullValues) {
|
||||
SBTheTVDBNullValuesEpisodes = 1 << 0,
|
||||
SBTheTVDBNullValuesSeries = 1 << 1,
|
||||
};
|
||||
|
||||
- (SBTheTVDBNullValues)checkForNull:(NSArray<SBMetadataResult *> *)results
|
||||
{
|
||||
if (!language) { language = @"en"; }
|
||||
NSNull *null = [NSNull null];
|
||||
SBTheTVDBNullValues nullValues = 0;
|
||||
|
||||
// search for series id
|
||||
NSSet<NSNumber *> *seriesIDs = [self searchSeriesID:seriesName language:language];
|
||||
|
||||
// fallback to english if the no results are found
|
||||
if (!seriesIDs.count) {
|
||||
seriesIDs = [self searchSeriesID:seriesName language:@"en"];
|
||||
}
|
||||
|
||||
NSMutableArray *results = [[NSMutableArray alloc] init];
|
||||
|
||||
for (NSDictionary *s in seriesIDs) {
|
||||
|
||||
NSNumber *seriesID = s[@"id"];
|
||||
|
||||
if (!seriesID || ![seriesID isKindOfClass:[NSNumber class]]) {
|
||||
continue;
|
||||
for (SBMetadataResult *result in results) {
|
||||
if ([result[SBMetadataResultSeriesName] isEqualTo:null]) {
|
||||
nullValues |= SBTheTVDBNullValuesSeries;
|
||||
}
|
||||
if ([result[SBMetadataResultName] isEqualTo:null]) {
|
||||
nullValues |= SBTheTVDBNullValuesEpisodes;
|
||||
}
|
||||
if ([result[SBMetadataResultLongDescription] isEqualTo:null]) {
|
||||
nullValues |= SBTheTVDBNullValuesEpisodes;
|
||||
}
|
||||
if ([result[SBMetadataResultSeriesDescription] isEqualTo:null]) {
|
||||
nullValues |= SBTheTVDBNullValuesSeries;
|
||||
}
|
||||
|
||||
// Series info
|
||||
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@", seriesID]];
|
||||
NSData *seriesInfoJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||
|
||||
if (!seriesInfoJSON) { continue; }
|
||||
|
||||
NSDictionary *seriesInfo = [NSJSONSerialization JSONObjectWithData:seriesInfoJSON options:0 error:NULL];
|
||||
|
||||
if (!seriesInfo || ![seriesInfo isKindOfClass:[NSDictionary class]]) { continue; }
|
||||
|
||||
seriesInfo = seriesInfo[@"data"];
|
||||
|
||||
if (!seriesInfo || ![seriesInfo isKindOfClass:[NSDictionary class]]) { continue; }
|
||||
|
||||
// Series actors
|
||||
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/actors", seriesID]];
|
||||
NSData *seriesActorsJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||
|
||||
if (!seriesActorsJSON) { continue; }
|
||||
|
||||
NSDictionary *seriesActorsDictionary = [NSJSONSerialization JSONObjectWithData:seriesActorsJSON options:0 error:NULL];
|
||||
|
||||
if (!seriesActorsDictionary || ![seriesActorsDictionary isKindOfClass:[NSDictionary class]]) { continue; }
|
||||
|
||||
NSArray *seriesActors = seriesActorsDictionary[@"data"];
|
||||
|
||||
if (!seriesActors || ![seriesActors isKindOfClass:[NSArray class]]) { continue; }
|
||||
|
||||
// Series episodes info
|
||||
|
||||
if (seasonNum.length && episodeNum.length) {
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes/query?airedSeason=%@&airedEpisode=%@", seriesID, seasonNum, episodeNum]];
|
||||
} else if (seasonNum.length) {
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes/query?airedSeason=%@", seriesID, seasonNum]];
|
||||
}
|
||||
else {
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes", seriesID]];
|
||||
}
|
||||
|
||||
NSData *episodesJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||
|
||||
if (!episodesJSON) { continue; }
|
||||
|
||||
NSDictionary *episodes = [NSJSONSerialization JSONObjectWithData:episodesJSON options:0 error:NULL];
|
||||
|
||||
if (!episodes || ![episodes isKindOfClass:[NSDictionary class]]) { continue; }
|
||||
|
||||
// Decode the individual episodes
|
||||
|
||||
NSArray<NSDictionary *> *episodesArray = episodes[@"data"];
|
||||
|
||||
if ([episodesArray isKindOfClass:[NSArray class]]) {
|
||||
|
||||
for (NSDictionary *episode in episodesArray) {
|
||||
if (seasonNum && seasonNum.length) {
|
||||
NSString *episodeSeason = [episode[@"airedSeason"] stringValue];
|
||||
if ([episodeSeason isEqualToString:seasonNum]) {
|
||||
if (episodeNum && episodeNum.length) {
|
||||
NSString *episodeNumber = [episode[@"airedEpisodeNumber"] stringValue];
|
||||
if ([episodeNumber isEqualToString:episodeNum]) {
|
||||
[results addObject:[SBTheTVDB metadataForEpisode:episode series:seriesInfo actors:seriesActors]];
|
||||
}
|
||||
} else {
|
||||
[results addObject:[SBTheTVDB metadataForEpisode:episode series:seriesInfo actors:seriesActors]];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
[results addObject:[SBTheTVDB metadataForEpisode:episode series:seriesInfo actors:seriesActors]];
|
||||
}
|
||||
}
|
||||
if (nullValues & SBTheTVDBNullValuesEpisodes &&
|
||||
nullValues & SBTheTVDBNullValuesSeries) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
NSArray<SBMetadataResult *> *resultsSorted = [results sortedArrayUsingComparator:^NSComparisonResult(SBMetadataResult *ep1, SBMetadataResult *ep2) {
|
||||
int v1 = [ep1[SBMetadataResultEpisodeNumber] intValue];
|
||||
int v2 = [ep2[SBMetadataResultEpisodeNumber] intValue];
|
||||
|
||||
int s1 = [ep1[SBMetadataResultSeason] intValue];
|
||||
int s2 = [ep2[SBMetadataResultSeason] intValue];
|
||||
|
||||
if (s1 == s2) {
|
||||
if (v1 < v2)
|
||||
return NSOrderedAscending;
|
||||
else if (v1 > v2)
|
||||
return NSOrderedDescending;
|
||||
}
|
||||
|
||||
if (s1 < s2)
|
||||
return NSOrderedAscending;
|
||||
else if (s1 > s2)
|
||||
return NSOrderedDescending;
|
||||
else
|
||||
return NSOrderedSame;
|
||||
}];
|
||||
|
||||
return resultsSorted;
|
||||
return nullValues;
|
||||
}
|
||||
|
||||
- (void)merge:(NSArray<SBMetadataResult *> *)array with:(NSArray<SBMetadataResult *> *)array2
|
||||
{
|
||||
NSNull *null = [NSNull null];
|
||||
|
||||
if (array2.count != array.count) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSUInteger index = 0;
|
||||
for (SBMetadataResult *target in array) {
|
||||
SBMetadataResult *source = array2[index];
|
||||
|
||||
if ([target[SBMetadataResultSeriesName] isEqualTo:null]) {
|
||||
target[SBMetadataResultSeriesName] = source[SBMetadataResultSeriesName];
|
||||
}
|
||||
if ([target[SBMetadataResultName] isEqualTo:null]) {
|
||||
target[SBMetadataResultName] = source[SBMetadataResultName];
|
||||
}
|
||||
if ([target[SBMetadataResultLongDescription] isEqualTo:null]) {
|
||||
target[SBMetadataResultLongDescription] = source[SBMetadataResultLongDescription];
|
||||
target[SBMetadataResultDescription] = source[SBMetadataResultDescription];
|
||||
}
|
||||
if ([target[SBMetadataResultSeriesDescription] isEqualTo:null]) {
|
||||
target[SBMetadataResultSeriesDescription] = source[SBMetadataResultSeriesDescription];
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
- (void)merge:(NSArray<SBMetadataResult *> *)array withSeries:(NSDictionary *)series
|
||||
{
|
||||
NSNull *null = [NSNull null];
|
||||
NSString *name = series[@"seriesName"];
|
||||
NSString *overview = series[@"overview"];
|
||||
|
||||
for (SBMetadataResult *target in array) {
|
||||
if ([target[SBMetadataResultSeriesName] isEqualTo:null]) {
|
||||
target[SBMetadataResultSeriesName] = name;
|
||||
}
|
||||
if ([target[SBMetadataResultSeriesDescription] isEqualTo:null]) {
|
||||
target[SBMetadataResultSeriesDescription] = overview;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Helpers
|
||||
|
||||
+ (nullable NSString *)cleanList:(nullable NSArray<NSString *> *)s
|
||||
{
|
||||
NSMutableString *result = [NSMutableString string];
|
||||
@@ -272,6 +351,8 @@
|
||||
return result.length ? result : nil;
|
||||
}
|
||||
|
||||
#pragma mark - Episodes
|
||||
|
||||
- (void)loadITunesArtwork:(SBMetadataResult *)metadata
|
||||
{
|
||||
NSMutableArray *newArtworkThumbURLs = [NSMutableArray array];
|
||||
@@ -442,4 +523,55 @@
|
||||
return metadata;
|
||||
}
|
||||
|
||||
- (NSArray<SBMetadataResult *> *)loadTVEpisodes:(NSDictionary *)seriesInfo actors:(NSArray *)actors seasonNum:(NSString *)seasonNum episodeNum:(NSString *)episodeNum language:(NSString *)language
|
||||
{
|
||||
NSURL *url;
|
||||
NSMutableArray *results = [[NSMutableArray alloc] init];
|
||||
NSNumber *seriesID = seriesInfo[@"id"];
|
||||
|
||||
if (seasonNum.length && episodeNum.length) {
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes/query?airedSeason=%@&airedEpisode=%@", seriesID, seasonNum, episodeNum]];
|
||||
} else if (seasonNum.length) {
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes/query?airedSeason=%@", seriesID, seasonNum]];
|
||||
}
|
||||
else {
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes", seriesID]];
|
||||
}
|
||||
|
||||
NSData *episodesJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||
|
||||
if (!episodesJSON) { return @[]; }
|
||||
|
||||
NSDictionary *episodes = [NSJSONSerialization JSONObjectWithData:episodesJSON options:0 error:NULL];
|
||||
|
||||
if (!episodes || ![episodes isKindOfClass:[NSDictionary class]]) { return @[]; }
|
||||
|
||||
// Decode the individual episodes
|
||||
|
||||
NSArray<NSDictionary *> *episodesArray = episodes[@"data"];
|
||||
|
||||
if ([episodesArray isKindOfClass:[NSArray class]]) {
|
||||
|
||||
for (NSDictionary *episode in episodesArray) {
|
||||
if (seasonNum && seasonNum.length) {
|
||||
NSString *episodeSeason = [episode[@"airedSeason"] stringValue];
|
||||
if ([episodeSeason isEqualToString:seasonNum]) {
|
||||
if (episodeNum && episodeNum.length) {
|
||||
NSString *episodeNumber = [episode[@"airedEpisodeNumber"] stringValue];
|
||||
if ([episodeNumber isEqualToString:episodeNum]) {
|
||||
[results addObject:[SBTheTVDB metadataForEpisode:episode series:seriesInfo actors:actors]];
|
||||
}
|
||||
} else {
|
||||
[results addObject:[SBTheTVDB metadataForEpisode:episode series:seriesInfo actors:actors]];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
[results addObject:[SBTheTVDB metadataForEpisode:episode series:seriesInfo actors:actors]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user