Refactor some code out of SBTheTVDB
This commit is contained in:
1 parent
8f396fcca2
commit
daa7fc3604
3 files changed
+208
-168
No files matched your search
@@ -35,23 +35,10 @@
|
|||||||
language = [MP42Languages.defaultManager extendedTagForLocalizedLang:language];
|
language = [MP42Languages.defaultManager extendedTagForLocalizedLang:language];
|
||||||
|
|
||||||
// search for series
|
// search for series
|
||||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/search/series?name=%@", [SBMetadataHelper urlEncoded:seriesName]]];
|
NSArray<NSDictionary *> *series = [SBTheTVDBConnection.defaultManager fetchSeries:[SBMetadataHelper urlEncoded:seriesName] language:language];
|
||||||
NSData *seriesJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
|
||||||
|
|
||||||
if (seriesJSON) {
|
for (NSDictionary *s in series) {
|
||||||
|
[results addObject:s[@"seriesName"]];
|
||||||
NSDictionary *series = [NSJSONSerialization JSONObjectWithData:seriesJSON options:0 error:NULL];
|
|
||||||
|
|
||||||
if (series || [series isKindOfClass:[NSDictionary class]]) {
|
|
||||||
|
|
||||||
NSArray<NSDictionary *> *seriesArray = series[@"data"];
|
|
||||||
|
|
||||||
if ([seriesArray isKindOfClass:[NSArray class]] && seriesArray.count) {
|
|
||||||
for (NSDictionary *s in seriesArray) {
|
|
||||||
[results addObject:s[@"seriesName"]];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fall back to english
|
// Fall back to english
|
||||||
@@ -85,34 +72,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Series info
|
// Series info
|
||||||
|
NSDictionary *seriesInfo = [SBTheTVDBConnection.defaultManager fetchSeriesInfo:seriesID language:language];
|
||||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@", seriesID]];
|
if (seriesInfo == nil) { continue; }
|
||||||
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
|
// Series actors
|
||||||
|
NSArray<NSDictionary *> *seriesActors = [SBTheTVDBConnection.defaultManager fetchSeriesActors:seriesID language:language];
|
||||||
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
|
// Series episodes info
|
||||||
NSArray<SBMetadataResult *> *localizedResults = [self loadTVEpisodes:seriesInfo actors:seriesActors
|
NSArray<SBMetadataResult *> *localizedResults = [self loadTVEpisodes:seriesInfo actors:seriesActors
|
||||||
@@ -132,17 +96,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (nullValues & SBTheTVDBNullValuesSeries) {
|
if (nullValues & SBTheTVDBNullValuesSeries) {
|
||||||
NSURL *englishUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@", seriesID]];
|
NSDictionary *englishSeriesInfo = [SBTheTVDBConnection.defaultManager fetchSeriesInfo:seriesID language:@"en"];
|
||||||
NSData *englishSeriesInfoJSON = [SBTheTVDBConnection.defaultManager requestData:englishUrl language:@"en"];
|
|
||||||
|
|
||||||
if (englishSeriesInfoJSON) {
|
if (englishSeriesInfo) {
|
||||||
|
[self merge:localizedResults withSeries:englishSeriesInfo];
|
||||||
NSDictionary *englishSeriesInfo = [NSJSONSerialization JSONObjectWithData:englishSeriesInfoJSON options:0 error:NULL];
|
|
||||||
|
|
||||||
if (englishSeriesInfo || [englishSeriesInfo isKindOfClass:[NSDictionary class]]) {
|
|
||||||
englishSeriesInfo = englishSeriesInfo[@"data"];
|
|
||||||
[self merge:localizedResults withSeries:englishSeriesInfo];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -199,30 +156,19 @@
|
|||||||
|
|
||||||
- (NSSet<NSNumber *> *)searchSeriesID:(NSString *)seriesName language:(NSString *)language
|
- (NSSet<NSNumber *> *)searchSeriesID:(NSString *)seriesName language:(NSString *)language
|
||||||
{
|
{
|
||||||
// search for series id
|
|
||||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/search/series?name=%@", [SBMetadataHelper urlEncoded:seriesName]]];
|
|
||||||
NSData *seriesJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
|
||||||
|
|
||||||
if (!seriesJSON) { return [NSSet set]; }
|
|
||||||
|
|
||||||
NSDictionary *series = [NSJSONSerialization JSONObjectWithData:seriesJSON options:0 error:NULL];
|
|
||||||
|
|
||||||
if (!series || ![series isKindOfClass:[NSDictionary class]]) { return [NSSet set]; }
|
|
||||||
|
|
||||||
NSArray<NSDictionary *> *seriesObject = series[@"data"];
|
|
||||||
NSMutableSet<NSDictionary *> *selectedSeries = [[NSMutableSet alloc] init];
|
NSMutableSet<NSDictionary *> *selectedSeries = [[NSMutableSet alloc] init];
|
||||||
|
|
||||||
if ([seriesObject isKindOfClass:[NSArray class]] && seriesObject.count) {
|
// search for series id
|
||||||
|
NSArray<NSDictionary *> *series = [SBTheTVDBConnection.defaultManager fetchSeries:[SBMetadataHelper urlEncoded:seriesName] language:language];
|
||||||
|
|
||||||
for (NSDictionary *s in seriesObject) {
|
for (NSDictionary *s in series) {
|
||||||
if ([self seriesResult:s matchName:seriesName]) {
|
if ([self seriesResult:s matchName:seriesName]) {
|
||||||
[selectedSeries addObject:s];
|
[selectedSeries addObject:s];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!selectedSeries.count) {
|
if (!selectedSeries.count && series.count) {
|
||||||
[selectedSeries addObject:seriesObject.firstObject];
|
[selectedSeries addObject:series.firstObject];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return [selectedSeries copy];
|
return [selectedSeries copy];
|
||||||
@@ -388,38 +334,28 @@ typedef NS_OPTIONS(NSUInteger, SBTheTVDBNullValues) {
|
|||||||
[newArtworkProviderNames addObjectsFromArray:metadata.artworkProviderNames];
|
[newArtworkProviderNames addObjectsFromArray:metadata.artworkProviderNames];
|
||||||
|
|
||||||
// get additionals images
|
// get additionals images
|
||||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/images/query?keyType=%@", metadata[@"TheTVDB Series ID"], type]];
|
NSArray *images = [SBTheTVDBConnection.defaultManager fetchSeriesImages:metadata[@"TheTVDB Series ID"] type:type language:language];
|
||||||
NSData *imagesJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
|
||||||
|
|
||||||
if (imagesJSON) {
|
if (images && [images isKindOfClass:[NSArray class]]) {
|
||||||
NSDictionary *imagesData = [NSJSONSerialization JSONObjectWithData:imagesJSON options:0 error:NULL];
|
|
||||||
|
|
||||||
if (imagesData || [imagesData isKindOfClass:[NSDictionary class]]) {
|
for (NSDictionary *image in images) {
|
||||||
|
NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://thetvdb.com/banners/%@", image[@"fileName"]]];
|
||||||
|
NSURL *thumbURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://thetvdb.com/banners/%@", image[@"thumbnail"]]];
|
||||||
|
|
||||||
NSArray *images = imagesData[@"data"];
|
BOOL toBeAdded = YES;
|
||||||
|
|
||||||
if (images && [images isKindOfClass:[NSArray class]]) {
|
if ([type isEqualToString:@"season"]) {
|
||||||
|
NSString *subKey = image[@"subKey"];
|
||||||
for (NSDictionary *image in images) {
|
NSString *season = [metadata[SBMetadataResultSeason] stringValue];
|
||||||
NSURL *fileURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://thetvdb.com/banners/%@", image[@"fileName"]]];
|
if (subKey && [subKey isKindOfClass:[NSString class]] && ![subKey isEqualToString:season]) {
|
||||||
NSURL *thumbURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://thetvdb.com/banners/%@", image[@"thumbnail"]]];
|
toBeAdded = NO;
|
||||||
|
|
||||||
BOOL toBeAdded = YES;
|
|
||||||
|
|
||||||
if ([type isEqualToString:@"season"]) {
|
|
||||||
NSString *subKey = image[@"subKey"];
|
|
||||||
NSString *season = [metadata[SBMetadataResultSeason] stringValue];
|
|
||||||
if (subKey && [subKey isKindOfClass:[NSString class]] && ![subKey isEqualToString:season]) {
|
|
||||||
toBeAdded = NO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (toBeAdded) {
|
|
||||||
[newArtworkThumbURLs addObject:thumbURL];
|
|
||||||
[newArtworkFullsizeURLs addObject:fileURL];
|
|
||||||
[newArtworkProviderNames addObject:[NSString stringWithFormat:@"TheTVDB|%@", type]];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (toBeAdded) {
|
||||||
|
[newArtworkThumbURLs addObject:thumbURL];
|
||||||
|
[newArtworkFullsizeURLs addObject:fileURL];
|
||||||
|
[newArtworkProviderNames addObject:[NSString stringWithFormat:@"TheTVDB|%@", type]];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -428,53 +364,43 @@ typedef NS_OPTIONS(NSUInteger, SBTheTVDBNullValues) {
|
|||||||
metadata.artworkProviderNames = newArtworkProviderNames;
|
metadata.artworkProviderNames = newArtworkProviderNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (SBMetadataResult *)loadTVMetadata:(SBMetadataResult *)aMetadata language:(NSString *)aLanguage
|
- (SBMetadataResult *)loadTVMetadata:(SBMetadataResult *)metadata language:(NSString *)language
|
||||||
{
|
{
|
||||||
// Get additional episodes info
|
// Get additional episodes info
|
||||||
NSNumber *episodesID = aMetadata[@"TheTVDB Episodes ID"];
|
NSNumber *episodesID = metadata[@"TheTVDB Episodes ID"];
|
||||||
NSURL *episodesUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/episodes/%@", episodesID]];
|
NSDictionary *episodesInfo = [SBTheTVDBConnection.defaultManager fetchEpisodesInfo:episodesID language:language];
|
||||||
NSData *episodesJSON = [SBTheTVDBConnection.defaultManager requestData:episodesUrl language:aLanguage];
|
|
||||||
|
|
||||||
if (episodesJSON) {
|
if (episodesInfo) {
|
||||||
NSDictionary *episodesInfo = [NSJSONSerialization JSONObjectWithData:episodesJSON options:0 error:NULL];
|
metadata[SBMetadataResultDirector] = [SBTheTVDB cleanList:episodesInfo[@"directors"]];
|
||||||
|
metadata[SBMetadataResultScreenwriters] = [SBTheTVDB cleanList:episodesInfo[@"writers"]];
|
||||||
|
|
||||||
if (episodesInfo || [episodesInfo isKindOfClass:[NSDictionary class]]) {
|
NSString *actors = metadata[SBMetadataResultCast];
|
||||||
NSDictionary *episodesData = episodesInfo[@"data"];
|
NSString *gueststars = [SBTheTVDB cleanList:episodesInfo[@"guestStars"]];
|
||||||
|
|
||||||
if (episodesData || [episodesData isKindOfClass:[NSDictionary class]]) {
|
if (actors.length && gueststars.length) {
|
||||||
|
metadata[SBMetadataResultCast] = [NSString stringWithFormat:@"%@, %@", actors, gueststars];
|
||||||
|
}
|
||||||
|
else if (gueststars.length) {
|
||||||
|
metadata[SBMetadataResultCast] = gueststars;
|
||||||
|
}
|
||||||
|
|
||||||
aMetadata[SBMetadataResultDirector] = [SBTheTVDB cleanList:episodesData[@"directors"]];
|
// Episodes artwork
|
||||||
aMetadata[SBMetadataResultScreenwriters] = [SBTheTVDB cleanList:episodesData[@"writers"]];
|
NSString *artworkFilename = episodesInfo[@"filename"];
|
||||||
|
if (artworkFilename && [artworkFilename isKindOfClass:[NSString class]] && artworkFilename.length) {
|
||||||
|
NSURL *artworkURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://thetvdb.com/banners/%@", artworkFilename]];
|
||||||
|
|
||||||
NSString *actors = aMetadata[SBMetadataResultCast];
|
metadata.artworkThumbURLs = @[artworkURL];
|
||||||
NSString *gueststars = [SBTheTVDB cleanList:episodesData[@"guestStars"]];
|
metadata.artworkFullsizeURLs = @[artworkURL];
|
||||||
|
metadata.artworkProviderNames = @[@"TheTVDB|episode"];
|
||||||
if (actors.length && gueststars.length) {
|
|
||||||
aMetadata[SBMetadataResultCast] = [NSString stringWithFormat:@"%@, %@", actors, gueststars];
|
|
||||||
}
|
|
||||||
else if (gueststars.length) {
|
|
||||||
aMetadata[SBMetadataResultCast] = gueststars;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Episodes artwork
|
|
||||||
NSString *artworkFilename = episodesData[@"filename"];
|
|
||||||
if (artworkFilename && [artworkFilename isKindOfClass:[NSString class]] && artworkFilename.length) {
|
|
||||||
NSURL *artworkURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://thetvdb.com/banners/%@", artworkFilename]];
|
|
||||||
|
|
||||||
aMetadata.artworkThumbURLs = @[artworkURL];
|
|
||||||
aMetadata.artworkFullsizeURLs = @[artworkURL];
|
|
||||||
aMetadata.artworkProviderNames = @[@"TheTVDB|episode"];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// get additionals images
|
// get additionals images
|
||||||
[self loadITunesArtwork:aMetadata];
|
[self loadITunesArtwork:metadata];
|
||||||
[self loadTVImages:aMetadata type:@"season" language:aLanguage];
|
[self loadTVImages:metadata type:@"season" language:language];
|
||||||
[self loadTVImages:aMetadata type:@"poster" language:aLanguage];
|
[self loadTVImages:metadata type:@"poster" language:language];
|
||||||
|
|
||||||
return aMetadata;
|
return metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (SBMetadataResult *)metadataForEpisode:(NSDictionary *)episode series:(NSDictionary *)series actors:(NSArray *)actors
|
+ (SBMetadataResult *)metadataForEpisode:(NSDictionary *)episode series:(NSDictionary *)series actors:(NSArray *)actors
|
||||||
@@ -525,49 +451,26 @@ typedef NS_OPTIONS(NSUInteger, SBTheTVDBNullValues) {
|
|||||||
|
|
||||||
- (NSArray<SBMetadataResult *> *)loadTVEpisodes:(NSDictionary *)seriesInfo actors:(NSArray *)actors seasonNum:(NSString *)seasonNum episodeNum:(NSString *)episodeNum language:(NSString *)language
|
- (NSArray<SBMetadataResult *> *)loadTVEpisodes:(NSDictionary *)seriesInfo actors:(NSArray *)actors seasonNum:(NSString *)seasonNum episodeNum:(NSString *)episodeNum language:(NSString *)language
|
||||||
{
|
{
|
||||||
NSURL *url;
|
|
||||||
NSMutableArray *results = [[NSMutableArray alloc] init];
|
NSMutableArray *results = [[NSMutableArray alloc] init];
|
||||||
NSNumber *seriesID = seriesInfo[@"id"];
|
NSNumber *seriesID = seriesInfo[@"id"];
|
||||||
|
|
||||||
if (seasonNum.length && episodeNum.length) {
|
NSArray<NSDictionary *> *episodes = [SBTheTVDBConnection.defaultManager fetchEpisodes:seriesID season:seasonNum number:episodeNum language:language];
|
||||||
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];
|
for (NSDictionary *episode in episodes) {
|
||||||
|
if (seasonNum && seasonNum.length) {
|
||||||
if (!episodesJSON) { return @[]; }
|
NSString *episodeSeason = [episode[@"airedSeason"] stringValue];
|
||||||
|
if ([episodeSeason isEqualToString:seasonNum]) {
|
||||||
NSDictionary *episodes = [NSJSONSerialization JSONObjectWithData:episodesJSON options:0 error:NULL];
|
if (episodeNum && episodeNum.length) {
|
||||||
|
NSString *episodeNumber = [episode[@"airedEpisodeNumber"] stringValue];
|
||||||
if (!episodes || ![episodes isKindOfClass:[NSDictionary class]]) { return @[]; }
|
if ([episodeNumber isEqualToString:episodeNum]) {
|
||||||
|
|
||||||
// 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]];
|
[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]];
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
[results addObject:[SBTheTVDB metadataForEpisode:episode series:seriesInfo actors:actors]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,13 @@
|
|||||||
|
|
||||||
@property (nonatomic, readonly) NSArray<NSString *> *languagues;
|
@property (nonatomic, readonly) NSArray<NSString *> *languagues;
|
||||||
|
|
||||||
- (NSData *)requestData:(NSURL *)url language:(NSString *)language;
|
- (NSArray<NSDictionary *> *)fetchSeries:(NSString *)seriesName language:(NSString *)language;
|
||||||
|
- (NSDictionary *)fetchSeriesInfo:(NSNumber *)seriesID language:(NSString *)language;
|
||||||
|
- (NSArray<NSDictionary *> *)fetchSeriesActors:(NSNumber *)seriesID language:(NSString *)language;
|
||||||
|
- (NSArray<NSDictionary *> *)fetchSeriesImages:(NSNumber *)seriesID type:(NSString *)type language:(NSString *)language;
|
||||||
|
|
||||||
|
- (NSArray<NSDictionary *> *)fetchEpisodes:(NSNumber *)seriesID season:(NSString *)season number:(NSString *)number language:(NSString *)language;
|
||||||
|
- (NSDictionary *)fetchEpisodesInfo:(NSNumber *)episodeID language:(NSString *)language;
|
||||||
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
@@ -176,4 +176,134 @@
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#pragma mark - wrapper
|
||||||
|
|
||||||
|
- (NSArray<NSDictionary *> *)fetchSeries:(NSString *)seriesName language:(NSString *)language
|
||||||
|
{
|
||||||
|
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/search/series?name=%@", seriesName]];
|
||||||
|
NSData *seriesJSON = [self requestData:url language:language];
|
||||||
|
|
||||||
|
if (seriesJSON) {
|
||||||
|
|
||||||
|
NSDictionary *series = [NSJSONSerialization JSONObjectWithData:seriesJSON options:0 error:NULL];
|
||||||
|
|
||||||
|
if (series || [series isKindOfClass:[NSDictionary class]]) {
|
||||||
|
|
||||||
|
NSArray<NSDictionary *> *seriesArray = series[@"data"];
|
||||||
|
|
||||||
|
if ([seriesArray isKindOfClass:[NSArray class]] && seriesArray.count) {
|
||||||
|
return seriesArray;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return @[];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (nullable NSDictionary *)fetchSeriesInfo:(NSNumber *)seriesID language:(NSString *)language
|
||||||
|
{
|
||||||
|
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@", seriesID]];
|
||||||
|
NSData *seriesInfoJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||||
|
|
||||||
|
if (!seriesInfoJSON) { return nil; }
|
||||||
|
|
||||||
|
NSDictionary *seriesInfo = [NSJSONSerialization JSONObjectWithData:seriesInfoJSON options:0 error:NULL];
|
||||||
|
|
||||||
|
if (!seriesInfo || ![seriesInfo isKindOfClass:[NSDictionary class]]) { return nil; }
|
||||||
|
|
||||||
|
seriesInfo = seriesInfo[@"data"];
|
||||||
|
|
||||||
|
if (!seriesInfo || ![seriesInfo isKindOfClass:[NSDictionary class]]) { return nil; }
|
||||||
|
|
||||||
|
return seriesInfo;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSArray<NSDictionary *> *)fetchSeriesActors:(NSNumber *)seriesID language:(NSString *)language
|
||||||
|
{
|
||||||
|
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/actors", seriesID]];
|
||||||
|
NSData *seriesActorsJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||||
|
|
||||||
|
if (!seriesActorsJSON) { return @[]; }
|
||||||
|
|
||||||
|
NSDictionary *seriesActorsDictionary = [NSJSONSerialization JSONObjectWithData:seriesActorsJSON options:0 error:NULL];
|
||||||
|
|
||||||
|
if (!seriesActorsDictionary || ![seriesActorsDictionary isKindOfClass:[NSDictionary class]]) { return @[]; }
|
||||||
|
|
||||||
|
NSArray<NSDictionary *> *seriesActors = seriesActorsDictionary[@"data"];
|
||||||
|
|
||||||
|
if (!seriesActors || ![seriesActors isKindOfClass:[NSArray class]]) { return @[]; }
|
||||||
|
|
||||||
|
return seriesActors;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSArray<NSDictionary *> *)fetchSeriesImages:(NSNumber *)seriesID type:(NSString *)type language:(NSString *)language;
|
||||||
|
{
|
||||||
|
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/images/query?keyType=%@", seriesID, type]];
|
||||||
|
NSData *imagesJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||||
|
|
||||||
|
if (!imagesJSON) { return @[]; }
|
||||||
|
|
||||||
|
NSDictionary *imagesDictionary = [NSJSONSerialization JSONObjectWithData:imagesJSON options:0 error:NULL];
|
||||||
|
|
||||||
|
if (!imagesDictionary || ![imagesDictionary isKindOfClass:[NSDictionary class]]) { return @[]; }
|
||||||
|
|
||||||
|
NSArray<NSDictionary *> *imagesArray = imagesDictionary[@"data"];
|
||||||
|
|
||||||
|
if (!imagesArray || ![imagesArray isKindOfClass:[NSArray class]]) { return @[]; }
|
||||||
|
|
||||||
|
return imagesArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSArray<NSDictionary *> *)fetchEpisodes:(NSNumber *)seriesID season:(NSString *)season number:(NSString *)number language:(NSString *)language;
|
||||||
|
{
|
||||||
|
NSURL *url;
|
||||||
|
if (season.length && number.length) {
|
||||||
|
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes/query?airedSeason=%@&airedEpisode=%@",
|
||||||
|
seriesID, season, number]];
|
||||||
|
} else if (season.length) {
|
||||||
|
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes/query?airedSeason=%@",
|
||||||
|
seriesID, season]];
|
||||||
|
}
|
||||||
|
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]]) {
|
||||||
|
return episodesArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
return @[];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSDictionary *)fetchEpisodesInfo:(NSNumber *)episodeID language:(NSString *)language;
|
||||||
|
{
|
||||||
|
NSURL *episodesUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/episodes/%@", episodeID]];
|
||||||
|
NSData *episodesJSON = [SBTheTVDBConnection.defaultManager requestData:episodesUrl language:language];
|
||||||
|
|
||||||
|
if (!episodesJSON) { return nil; }
|
||||||
|
|
||||||
|
NSDictionary *episodesInfo = [NSJSONSerialization JSONObjectWithData:episodesJSON options:0 error:NULL];
|
||||||
|
|
||||||
|
if (!episodesInfo || ![episodesInfo isKindOfClass:[NSDictionary class]]) { return nil; }
|
||||||
|
|
||||||
|
episodesInfo = episodesInfo[@"data"];
|
||||||
|
|
||||||
|
if (!episodesInfo || ![episodesInfo isKindOfClass:[NSDictionary class]]) { return nil; }
|
||||||
|
|
||||||
|
return episodesInfo;
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
Reference in new issue
Block a user