Switched all the tvdb server calls to the json api.
This commit is contained in:
1 parent
fd01fb901b
commit
0c5ea0181b
4 files changed
+287
-160
No files matched your search
@@ -10,51 +10,15 @@
|
|||||||
#import <MP42Foundation/MP42Languages.h>
|
#import <MP42Foundation/MP42Languages.h>
|
||||||
|
|
||||||
#import "SBTheTVDB.h"
|
#import "SBTheTVDB.h"
|
||||||
|
#import "SBTheTVDBConnection.h"
|
||||||
|
|
||||||
#import "SBiTunesStore.h"
|
#import "SBiTunesStore.h"
|
||||||
#import "XMLReader.h"
|
|
||||||
|
|
||||||
#define API_KEY @"3498815BE9484A62"
|
|
||||||
|
|
||||||
static NSArray<NSString *> *TVDBlanguages;
|
|
||||||
static NSString *globalToken;
|
|
||||||
|
|
||||||
@interface SBTheTVDB ()
|
|
||||||
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
@implementation SBTheTVDB
|
@implementation SBTheTVDB
|
||||||
|
|
||||||
+ (void)initialize
|
|
||||||
{
|
|
||||||
if (self == [SBTheTVDB class]) {
|
|
||||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://thetvdb.com/api/%@/languages.xml", API_KEY]];
|
|
||||||
NSData *languagesXML = [SBMetadataHelper downloadDataFromURL:url cachePolicy:SBReturnCacheElseLoad];
|
|
||||||
NSDictionary *languages = [XMLReader dictionaryForXMLData:languagesXML error:NULL];
|
|
||||||
|
|
||||||
if (languages) {
|
|
||||||
NSArray *languagesArray = [languages retrieveArrayForPath:@"Languages.Language"];
|
|
||||||
NSMutableArray<NSString *> *languagesResult = [NSMutableArray array];
|
|
||||||
|
|
||||||
if ([languagesArray isKindOfClass:[NSArray class]] && languagesArray.count) {
|
|
||||||
MP42Languages *langManager = MP42Languages.defaultManager;
|
|
||||||
for (NSDictionary *language in languagesArray) {
|
|
||||||
NSString *lang = [language valueForKeyPath:@"abbreviation.text"];
|
|
||||||
if (lang && [lang isKindOfClass:[NSString class]]) {
|
|
||||||
[languagesResult addObject:[langManager extendedTagForISO_639_1:lang]];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TVDBlanguages = [languagesResult copy];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSArray<NSString *> *)languages
|
- (NSArray<NSString *> *)languages
|
||||||
{
|
{
|
||||||
return TVDBlanguages;
|
return SBTheTVDBConnection.defaultManager.languagues;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (SBMetadataImporterLanguageType)languageType
|
- (SBMetadataImporterLanguageType)languageType
|
||||||
@@ -62,81 +26,37 @@ static NSString *globalToken;
|
|||||||
return SBMetadataImporterLanguageTypeISO;
|
return SBMetadataImporterLanguageTypeISO;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)updateToken
|
- (NSArray<NSString *> *)searchTVSeries:(NSString *)seriesName language:(NSString *)language
|
||||||
{
|
{
|
||||||
NSDictionary *apiKey = @{@"apikey" : API_KEY};
|
NSMutableSet *results = [NSMutableSet set];
|
||||||
NSData *jsonApiKey = [NSJSONSerialization dataWithJSONObject:apiKey options:0 error:NULL];
|
|
||||||
|
|
||||||
NSDictionary *headerOptions = @{@"Content-Type" : @"application/json",
|
language = [MP42Languages.defaultManager extendedTagForLocalizedLang:language];
|
||||||
@"Accept" : @"application/json"};
|
|
||||||
NSURL *url = [NSURL URLWithString:@"https://api.thetvdb.com/login"];
|
|
||||||
NSData *data = [SBMetadataHelper downloadDataFromURL:url HTTPMethod:@"POST" HTTPBody:jsonApiKey headerOptions:headerOptions cachePolicy:SBDefaultPolicy];
|
|
||||||
|
|
||||||
if (data) {
|
// search for series
|
||||||
id response = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
|
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/search/series?name=%@", [SBMetadataHelper urlEncoded:seriesName]]];
|
||||||
if ([response isKindOfClass:[NSDictionary class]]) {
|
NSData *seriesJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||||
NSString *token = response[@"token"];
|
|
||||||
if ([token isKindOfClass:[NSString class]]) {
|
if (seriesJSON) {
|
||||||
globalToken = token;
|
|
||||||
|
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"]];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
- (NSData *)requestData:(NSURL *)url language:(NSString *)language
|
if (![language isEqualToString:@"en"]) {
|
||||||
{
|
[results addObjectsFromArray:[self searchTVSeries:seriesName language:@"en"]];
|
||||||
if (globalToken == nil) {
|
|
||||||
[self updateToken];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NSDictionary *headerOptions = @{@"Authorization" : [NSString stringWithFormat:@"Bearer %@", globalToken],
|
return results.allObjects;
|
||||||
@"Content-Type" : @"application/json",
|
|
||||||
@"Accept" : @"application/json",
|
|
||||||
@"Accept-Language" : language};
|
|
||||||
NSData *data = [SBMetadataHelper downloadDataFromURL:url HTTPMethod:@"GET" HTTPBody:nil headerOptions:headerOptions cachePolicy:SBDefaultPolicy];
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
NSInteger sortSBMetadataResult(id ep1, id ep2, void *context)
|
|
||||||
{
|
|
||||||
int v1 = [((SBMetadataResult *) ep1)[SBMetadataResultEpisodeNumber] intValue];
|
|
||||||
int v2 = [((SBMetadataResult *) ep2)[SBMetadataResultEpisodeNumber] intValue];
|
|
||||||
|
|
||||||
int s1 = [((SBMetadataResult *) ep1)[SBMetadataResultSeason] intValue];
|
|
||||||
int s2 = [((SBMetadataResult *) 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (NSArray<NSString *> *)searchTVSeries:(NSString *)aSeriesName language:(NSString *)aLanguage
|
|
||||||
{
|
|
||||||
// search for series
|
|
||||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://thetvdb.com/api/GetSeries.php?seriesname=%@&language=all", [SBMetadataHelper urlEncoded:aSeriesName]]];
|
|
||||||
|
|
||||||
NSData *seriesXML = [SBMetadataHelper downloadDataFromURL:url cachePolicy:SBDefaultPolicy];
|
|
||||||
NSDictionary *series = [XMLReader dictionaryForXMLData:seriesXML error:NULL];
|
|
||||||
|
|
||||||
if (!series) { return @[]; }
|
|
||||||
|
|
||||||
NSArray *seriesArray = [series retrieveArrayForPath:@"Data.Series"];
|
|
||||||
NSMutableSet *results = [[NSMutableSet alloc] initWithCapacity:seriesArray.count];
|
|
||||||
for (NSDictionary *s in seriesArray) {
|
|
||||||
[results addObject:[s retrieveForPath:@"SeriesName.text"]];
|
|
||||||
}
|
|
||||||
return results.allObjects;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)seriesResult:(NSDictionary *)s matchName:(NSString *)seriesName
|
- (BOOL)seriesResult:(NSDictionary *)s matchName:(NSString *)seriesName
|
||||||
@@ -159,28 +79,25 @@ NSInteger sortSBMetadataResult(id ep1, id ep2, void *context)
|
|||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSArray<SBMetadataResult *> *)searchTVSeries:(NSString *)aSeriesName language:(NSString *)aLanguage seasonNum:(NSString *)aSeasonNum episodeNum:(NSString *)aEpisodeNum
|
- (NSSet<NSNumber *> *)searchSeriesID:(NSString *)seriesName language:(NSString *)language
|
||||||
{
|
{
|
||||||
NSString *lang = aLanguage;
|
// search for series id
|
||||||
if (!lang) { lang = @"en"; }
|
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]; }
|
||||||
|
|
||||||
// search for series
|
NSDictionary *series = [NSJSONSerialization JSONObjectWithData:seriesJSON options:0 error:NULL];
|
||||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/search/series?name=%@", [SBMetadataHelper urlEncoded:aSeriesName]]];
|
|
||||||
NSData *seriesJSON = [self requestData:url language:lang];
|
|
||||||
|
|
||||||
if (!seriesJSON) { return @[]; }
|
if (!series || ![series isKindOfClass:[NSDictionary class]]) { return [NSSet set]; }
|
||||||
|
|
||||||
NSDictionary *series = [NSJSONSerialization JSONObjectWithData:seriesJSON options:0 error:NULL];
|
NSArray<NSDictionary *> *seriesObject = series[@"data"];
|
||||||
|
NSMutableSet<NSDictionary *> *selectedSeries = [[NSMutableSet alloc] init];
|
||||||
|
|
||||||
if (!series || ![series isKindOfClass:[NSDictionary class]]) { return @[]; }
|
if ([seriesObject isKindOfClass:[NSArray class]] && seriesObject.count) {
|
||||||
|
|
||||||
NSArray<NSDictionary *> *seriesObject = series[@"data"];
|
|
||||||
NSMutableSet<NSDictionary *> *selectedSeries = [[NSMutableSet alloc] init];
|
|
||||||
|
|
||||||
if ([seriesObject isKindOfClass:[NSArray class]] && seriesObject.count) {
|
|
||||||
|
|
||||||
for (NSDictionary *s in seriesObject) {
|
for (NSDictionary *s in seriesObject) {
|
||||||
if ([self seriesResult:s matchName:aSeriesName]) {
|
if ([self seriesResult:s matchName:seriesName]) {
|
||||||
[selectedSeries addObject:s];
|
[selectedSeries addObject:s];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,29 +105,39 @@ NSInteger sortSBMetadataResult(id ep1, id ep2, void *context)
|
|||||||
if (!selectedSeries.count) {
|
if (!selectedSeries.count) {
|
||||||
[selectedSeries addObject:seriesObject.firstObject];
|
[selectedSeries addObject:seriesObject.firstObject];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return [selectedSeries copy];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (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];
|
NSMutableArray *results = [[NSMutableArray alloc] init];
|
||||||
|
|
||||||
for (NSDictionary *s in selectedSeries) {
|
for (NSDictionary *s in seriesIDs) {
|
||||||
|
|
||||||
NSNumber *seriesID = s[@"id"];
|
NSNumber *seriesID = s[@"id"];
|
||||||
NSString *seriesName = s[@"seriesName"];
|
|
||||||
|
|
||||||
if (!seriesID || ![seriesID isKindOfClass:[NSNumber class]]) {
|
if (!seriesID || ![seriesID isKindOfClass:[NSNumber class]]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!seriesName || ![seriesName isKindOfClass:[NSString class]]) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Series info
|
// Series info
|
||||||
|
|
||||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@", seriesID]];
|
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@", seriesID]];
|
||||||
NSData *seriesInfoJSON = [self requestData:url language:lang];
|
NSData *seriesInfoJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||||
|
|
||||||
if (!seriesJSON) { continue; }
|
if (!seriesInfoJSON) { continue; }
|
||||||
|
|
||||||
NSDictionary *seriesInfo = [NSJSONSerialization JSONObjectWithData:seriesInfoJSON options:0 error:NULL];
|
NSDictionary *seriesInfo = [NSJSONSerialization JSONObjectWithData:seriesInfoJSON options:0 error:NULL];
|
||||||
|
|
||||||
@@ -223,7 +150,7 @@ NSInteger sortSBMetadataResult(id ep1, id ep2, void *context)
|
|||||||
// Series actors
|
// Series actors
|
||||||
|
|
||||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/actors", seriesID]];
|
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/actors", seriesID]];
|
||||||
NSData *seriesActorsJSON = [self requestData:url language:lang];
|
NSData *seriesActorsJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||||
|
|
||||||
if (!seriesActorsJSON) { continue; }
|
if (!seriesActorsJSON) { continue; }
|
||||||
|
|
||||||
@@ -237,16 +164,16 @@ NSInteger sortSBMetadataResult(id ep1, id ep2, void *context)
|
|||||||
|
|
||||||
// Series episodes info
|
// Series episodes info
|
||||||
|
|
||||||
if (aSeasonNum.length && aEpisodeNum.length) {
|
if (seasonNum.length && episodeNum.length) {
|
||||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes/query?airedSeason=%@&airedEpisode=%@", seriesID, aSeasonNum, aEpisodeNum]];
|
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes/query?airedSeason=%@&airedEpisode=%@", seriesID, seasonNum, episodeNum]];
|
||||||
} else if (aSeasonNum.length) {
|
} else if (seasonNum.length) {
|
||||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes/query?airedSeason=%@", seriesID, aSeasonNum]];
|
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes/query?airedSeason=%@", seriesID, seasonNum]];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes", seriesID]];
|
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/episodes", seriesID]];
|
||||||
}
|
}
|
||||||
|
|
||||||
NSData *episodesJSON = [self requestData:url language:lang];
|
NSData *episodesJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||||
|
|
||||||
if (!episodesJSON) { continue; }
|
if (!episodesJSON) { continue; }
|
||||||
|
|
||||||
@@ -261,12 +188,12 @@ NSInteger sortSBMetadataResult(id ep1, id ep2, void *context)
|
|||||||
if ([episodesArray isKindOfClass:[NSArray class]]) {
|
if ([episodesArray isKindOfClass:[NSArray class]]) {
|
||||||
|
|
||||||
for (NSDictionary *episode in episodesArray) {
|
for (NSDictionary *episode in episodesArray) {
|
||||||
if (aSeasonNum && aSeasonNum.length) {
|
if (seasonNum && seasonNum.length) {
|
||||||
NSString *episodeSeason = [episode[@"airedSeason"] stringValue];
|
NSString *episodeSeason = [episode[@"airedSeason"] stringValue];
|
||||||
if ([episodeSeason isEqualToString:aSeasonNum]) {
|
if ([episodeSeason isEqualToString:seasonNum]) {
|
||||||
if (aEpisodeNum && aEpisodeNum.length) {
|
if (episodeNum && episodeNum.length) {
|
||||||
NSString *episodeNumber = [episode[@"airedEpisodeNumber"] stringValue];
|
NSString *episodeNumber = [episode[@"airedEpisodeNumber"] stringValue];
|
||||||
if ([episodeNumber isEqualToString:aEpisodeNum]) {
|
if ([episodeNumber isEqualToString:episodeNum]) {
|
||||||
[results addObject:[SBTheTVDB metadataForEpisode:episode series:seriesInfo actors:seriesActors]];
|
[results addObject:[SBTheTVDB metadataForEpisode:episode series:seriesInfo actors:seriesActors]];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -280,8 +207,28 @@ NSInteger sortSBMetadataResult(id ep1, id ep2, void *context)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}];
|
||||||
|
|
||||||
NSArray<SBMetadataResult *> *resultsSorted = [results sortedArrayUsingFunction:sortSBMetadataResult context:NULL];
|
|
||||||
return resultsSorted;
|
return resultsSorted;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -360,7 +307,7 @@ NSInteger sortSBMetadataResult(id ep1, id ep2, void *context)
|
|||||||
|
|
||||||
// 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]];
|
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/series/%@/images/query?keyType=%@", metadata[@"TheTVDB Series ID"], type]];
|
||||||
NSData *imagesJSON = [self requestData:url language:language];
|
NSData *imagesJSON = [SBTheTVDBConnection.defaultManager requestData:url language:language];
|
||||||
|
|
||||||
if (imagesJSON) {
|
if (imagesJSON) {
|
||||||
NSDictionary *imagesData = [NSJSONSerialization JSONObjectWithData:imagesJSON options:0 error:NULL];
|
NSDictionary *imagesData = [NSJSONSerialization JSONObjectWithData:imagesJSON options:0 error:NULL];
|
||||||
@@ -404,7 +351,7 @@ NSInteger sortSBMetadataResult(id ep1, id ep2, void *context)
|
|||||||
// Get additional episodes info
|
// Get additional episodes info
|
||||||
NSNumber *episodesID = aMetadata[@"TheTVDB Episodes ID"];
|
NSNumber *episodesID = aMetadata[@"TheTVDB Episodes ID"];
|
||||||
NSURL *episodesUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/episodes/%@", episodesID]];
|
NSURL *episodesUrl = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.thetvdb.com/episodes/%@", episodesID]];
|
||||||
NSData *episodesJSON = [self requestData:episodesUrl language:aLanguage];
|
NSData *episodesJSON = [SBTheTVDBConnection.defaultManager requestData:episodesUrl language:aLanguage];
|
||||||
|
|
||||||
if (episodesJSON) {
|
if (episodesJSON) {
|
||||||
NSDictionary *episodesInfo = [NSJSONSerialization JSONObjectWithData:episodesJSON options:0 error:NULL];
|
NSDictionary *episodesInfo = [NSJSONSerialization JSONObjectWithData:episodesJSON options:0 error:NULL];
|
||||||
@@ -448,42 +395,42 @@ NSInteger sortSBMetadataResult(id ep1, id ep2, void *context)
|
|||||||
return aMetadata;
|
return aMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (SBMetadataResult *)metadataForEpisode:(NSDictionary *)aEpisode series:(NSDictionary *)aSeries actors:(NSArray *)actors
|
+ (SBMetadataResult *)metadataForEpisode:(NSDictionary *)episode series:(NSDictionary *)series actors:(NSArray *)actors
|
||||||
{
|
{
|
||||||
SBMetadataResult *metadata = [[SBMetadataResult alloc] init];
|
SBMetadataResult *metadata = [[SBMetadataResult alloc] init];
|
||||||
|
|
||||||
metadata.mediaKind = 10; // TV show
|
metadata.mediaKind = 10; // TV show
|
||||||
|
|
||||||
// TV Show
|
// TV Show
|
||||||
metadata[@"TheTVDB Series ID"] = aSeries[@"id"];
|
metadata[@"TheTVDB Series ID"] = series[@"id"];
|
||||||
metadata[SBMetadataResultSeriesName] = aSeries[@"seriesName"];
|
metadata[SBMetadataResultSeriesName] = series[@"seriesName"];
|
||||||
metadata[SBMetadataResultSeriesDescription] = aSeries[@"overview"];
|
metadata[SBMetadataResultSeriesDescription] = series[@"overview"];
|
||||||
metadata[SBMetadataResultGenre] = [SBTheTVDB cleanList:aSeries[@"genre"]];
|
metadata[SBMetadataResultGenre] = [SBTheTVDB cleanList:series[@"genre"]];
|
||||||
|
|
||||||
// Episode
|
// Episode
|
||||||
metadata[@"TheTVDB Episodes ID"] = aEpisode[@"id"];
|
metadata[@"TheTVDB Episodes ID"] = episode[@"id"];
|
||||||
metadata[SBMetadataResultName] = aEpisode[@"episodeName"];
|
metadata[SBMetadataResultName] = episode[@"episodeName"];
|
||||||
metadata[SBMetadataResultReleaseDate] = aEpisode[@"firstAired"];
|
metadata[SBMetadataResultReleaseDate] = episode[@"firstAired"];
|
||||||
metadata[SBMetadataResultDescription] = aEpisode[@"overview"];
|
metadata[SBMetadataResultDescription] = episode[@"overview"];
|
||||||
metadata[SBMetadataResultLongDescription] = aEpisode[@"overview"];
|
metadata[SBMetadataResultLongDescription] = episode[@"overview"];
|
||||||
|
|
||||||
NSString *ratingString = aSeries[@"rating"];
|
NSString *ratingString = series[@"rating"];
|
||||||
if (ratingString.length) {
|
if (ratingString.length) {
|
||||||
metadata[SBMetadataResultRating] = [[MP42Ratings defaultManager] ratingStringForiTunesCountry:@"USA"
|
metadata[SBMetadataResultRating] = [[MP42Ratings defaultManager] ratingStringForiTunesCountry:@"USA"
|
||||||
media:@"TV"
|
media:@"TV"
|
||||||
ratingString:ratingString];
|
ratingString:ratingString];
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata[SBMetadataResultNetwork] = aSeries[@"network"];
|
metadata[SBMetadataResultNetwork] = series[@"network"];
|
||||||
metadata[SBMetadataResultSeason] = aEpisode[@"airedSeason"];
|
metadata[SBMetadataResultSeason] = episode[@"airedSeason"];
|
||||||
|
|
||||||
NSString *episodeID = [NSString stringWithFormat:@"%d%02d",
|
NSString *episodeID = [NSString stringWithFormat:@"%d%02d",
|
||||||
[aEpisode[@"airedSeason"] intValue],
|
[episode[@"airedSeason"] intValue],
|
||||||
[aEpisode[@"airedEpisodeNumber"] intValue]];
|
[episode[@"airedEpisodeNumber"] intValue]];
|
||||||
|
|
||||||
metadata[SBMetadataResultEpisodeID] = episodeID;
|
metadata[SBMetadataResultEpisodeID] = episodeID;
|
||||||
metadata[SBMetadataResultEpisodeNumber] = aEpisode[@"airedEpisodeNumber"];
|
metadata[SBMetadataResultEpisodeNumber] = episode[@"airedEpisodeNumber"];
|
||||||
metadata[SBMetadataResultTrackNumber] = aEpisode[@"airedEpisodeNumber"];
|
metadata[SBMetadataResultTrackNumber] = episode[@"airedEpisodeNumber"];
|
||||||
|
|
||||||
// Actors
|
// Actors
|
||||||
metadata[SBMetadataResultCast] = [SBTheTVDB cleanActorsList:actors];
|
metadata[SBMetadataResultCast] = [SBTheTVDB cleanActorsList:actors];
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// SBTheTVDBConnection.h
|
||||||
|
// Subler
|
||||||
|
//
|
||||||
|
// Created by Damiano Galassi on 24/05/17.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
|
@interface SBTheTVDBConnection : NSObject
|
||||||
|
|
||||||
|
@property (class, readonly) SBTheTVDBConnection *defaultManager;
|
||||||
|
|
||||||
|
@property (nonatomic, readonly) NSArray<NSString *> *languagues;
|
||||||
|
|
||||||
|
- (NSData *)requestData:(NSURL *)url language:(NSString *)language;
|
||||||
|
|
||||||
|
@end
|
||||||
@@ -0,0 +1,155 @@
|
|||||||
|
//
|
||||||
|
// SBTheTVDBConnection.m
|
||||||
|
// Subler
|
||||||
|
//
|
||||||
|
// Created by Damiano Galassi on 24/05/17.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
#import "SBTheTVDBConnection.h"
|
||||||
|
|
||||||
|
#import <MP42Foundation/MP42Ratings.h>
|
||||||
|
#import <MP42Foundation/MP42Languages.h>
|
||||||
|
|
||||||
|
#import "SBMetadataHelper.h"
|
||||||
|
|
||||||
|
#define API_KEY @"3498815BE9484A62"
|
||||||
|
|
||||||
|
@interface SBTheTVDBConnection ()
|
||||||
|
|
||||||
|
@property (nonatomic, readwrite, nullable) NSString *token;
|
||||||
|
@property (nonatomic, readwrite) NSArray<NSString *> *languagues;
|
||||||
|
|
||||||
|
@property (nonatomic, readonly) dispatch_queue_t queue;
|
||||||
|
@property (nonatomic, readonly) dispatch_queue_t tokenQueue;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation SBTheTVDBConnection
|
||||||
|
|
||||||
|
+ (instancetype)defaultManager
|
||||||
|
{
|
||||||
|
static dispatch_once_t pred;
|
||||||
|
static SBTheTVDBConnection *shared = nil;
|
||||||
|
|
||||||
|
dispatch_once(&pred, ^{
|
||||||
|
shared = [[SBTheTVDBConnection alloc] init];
|
||||||
|
});
|
||||||
|
return shared;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)init
|
||||||
|
{
|
||||||
|
self = [super init];
|
||||||
|
if (self) {
|
||||||
|
_queue = dispatch_queue_create("org.subler.TheTVDBQueue", DISPATCH_QUEUE_SERIAL);
|
||||||
|
_tokenQueue = dispatch_queue_create("org.subler.TheTVDBTokenQueue", DISPATCH_QUEUE_SERIAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)updateToken
|
||||||
|
{
|
||||||
|
NSDictionary *apiKey = @{@"apikey" : API_KEY};
|
||||||
|
NSData *jsonApiKey = [NSJSONSerialization dataWithJSONObject:apiKey options:0 error:NULL];
|
||||||
|
|
||||||
|
NSDictionary *headerOptions = @{@"Content-Type" : @"application/json",
|
||||||
|
@"Accept" : @"application/json"};
|
||||||
|
|
||||||
|
NSURL *url = [NSURL URLWithString:@"https://api.thetvdb.com/login"];
|
||||||
|
NSData *data = [SBMetadataHelper downloadDataFromURL:url HTTPMethod:@"POST" HTTPBody:jsonApiKey headerOptions:headerOptions cachePolicy:SBDefaultPolicy];
|
||||||
|
|
||||||
|
if (data) {
|
||||||
|
id response = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
|
||||||
|
if ([response isKindOfClass:[NSDictionary class]]) {
|
||||||
|
NSString *token = response[@"token"];
|
||||||
|
if ([token isKindOfClass:[NSString class]]) {
|
||||||
|
_token = token;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)updateLanguages
|
||||||
|
{
|
||||||
|
NSURL *url = [NSURL URLWithString:@"https://api.thetvdb.com/languages"];
|
||||||
|
NSData *languagesJSON = [self requestData:url language:@"en"];
|
||||||
|
|
||||||
|
NSDictionary *languagesDict = [NSJSONSerialization JSONObjectWithData:languagesJSON options:0 error:NULL];
|
||||||
|
|
||||||
|
if (languagesDict || [languagesDict isKindOfClass:[NSDictionary class]]) {
|
||||||
|
|
||||||
|
NSArray<NSDictionary *> *languagesArray = languagesDict[@"data"];
|
||||||
|
|
||||||
|
if (languagesArray || [languagesArray isKindOfClass:[NSArray class]]) {
|
||||||
|
|
||||||
|
MP42Languages *langManager = MP42Languages.defaultManager;
|
||||||
|
NSMutableArray<NSString *> *result = [NSMutableArray array];
|
||||||
|
|
||||||
|
for (NSDictionary *lang in languagesArray) {
|
||||||
|
if ([lang isKindOfClass:[NSDictionary class]]) {
|
||||||
|
NSString *abbreviation = lang[@"abbreviation"];
|
||||||
|
if (abbreviation && [abbreviation isKindOfClass:[NSString class]]) {
|
||||||
|
[result addObject:[langManager extendedTagForISO_639_1:abbreviation]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self->_languagues = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSString *)token
|
||||||
|
{
|
||||||
|
__block NSString *token = nil;
|
||||||
|
|
||||||
|
dispatch_sync(_tokenQueue, ^{
|
||||||
|
|
||||||
|
if (self->_token == nil) {
|
||||||
|
[self updateToken];
|
||||||
|
}
|
||||||
|
|
||||||
|
token = self->_token;
|
||||||
|
});
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSArray<NSString *> *)languagues
|
||||||
|
{
|
||||||
|
__block NSArray<NSString *> *languages = @[];
|
||||||
|
|
||||||
|
dispatch_sync(_queue, ^{
|
||||||
|
|
||||||
|
if (self->_languagues == nil) {
|
||||||
|
[self updateLanguages];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->_languagues) {
|
||||||
|
languages = self->_languagues;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return languages;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (NSData *)requestData:(NSURL *)url language:(NSString *)language
|
||||||
|
{
|
||||||
|
NSString *token = self.token;
|
||||||
|
|
||||||
|
if (token == nil) {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
|
NSDictionary *headerOptions = @{@"Authorization" : [NSString stringWithFormat:@"Bearer %@", token],
|
||||||
|
@"Content-Type" : @"application/json",
|
||||||
|
@"Accept" : @"application/json",
|
||||||
|
@"Accept-Language" : language};
|
||||||
|
NSData *data = [SBMetadataHelper downloadDataFromURL:url HTTPMethod:@"GET" HTTPBody:nil headerOptions:headerOptions cachePolicy:SBDefaultPolicy];
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
@@ -30,6 +30,7 @@
|
|||||||
A93DD7681DACCE930052749A /* SBComboBoxCellView.m in Sources */ = {isa = PBXBuildFile; fileRef = A93DD7671DACCE930052749A /* SBComboBoxCellView.m */; };
|
A93DD7681DACCE930052749A /* SBComboBoxCellView.m in Sources */ = {isa = PBXBuildFile; fileRef = A93DD7671DACCE930052749A /* SBComboBoxCellView.m */; };
|
||||||
A94721931D49DC0000858DD8 /* SBMetadataPrefsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A94721911D49DC0000858DD8 /* SBMetadataPrefsViewController.m */; };
|
A94721931D49DC0000858DD8 /* SBMetadataPrefsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A94721911D49DC0000858DD8 /* SBMetadataPrefsViewController.m */; };
|
||||||
A94A6A591ED1C5CC00049F7D /* ParseFilename in Resources */ = {isa = PBXBuildFile; fileRef = A94A6A581ED1C5CA00049F7D /* ParseFilename */; };
|
A94A6A591ED1C5CC00049F7D /* ParseFilename in Resources */ = {isa = PBXBuildFile; fileRef = A94A6A581ED1C5CA00049F7D /* ParseFilename */; };
|
||||||
|
A95395D41ED5839300E578D8 /* SBTheTVDBConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = A95395D31ED5839300E578D8 /* SBTheTVDBConnection.m */; };
|
||||||
A95B8DA91B09F37800F2F152 /* Subler_Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = A95B8DA81B09F37800F2F152 /* Subler_Tests.m */; };
|
A95B8DA91B09F37800F2F152 /* Subler_Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = A95B8DA81B09F37800F2F152 /* Subler_Tests.m */; };
|
||||||
A960E96317D4B00900FC6ED9 /* SBImageBrowserView.m in Sources */ = {isa = PBXBuildFile; fileRef = A960E96217D4B00900FC6ED9 /* SBImageBrowserView.m */; };
|
A960E96317D4B00900FC6ED9 /* SBImageBrowserView.m in Sources */ = {isa = PBXBuildFile; fileRef = A960E96217D4B00900FC6ED9 /* SBImageBrowserView.m */; };
|
||||||
A961476313F567E10068F161 /* SBQueueController.m in Sources */ = {isa = PBXBuildFile; fileRef = A961476013F567E10068F161 /* SBQueueController.m */; };
|
A961476313F567E10068F161 /* SBQueueController.m in Sources */ = {isa = PBXBuildFile; fileRef = A961476013F567E10068F161 /* SBQueueController.m */; };
|
||||||
@@ -209,6 +210,8 @@
|
|||||||
A94721901D49DC0000858DD8 /* SBMetadataPrefsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBMetadataPrefsViewController.h; path = Classes/SBMetadataPrefsViewController.h; sourceTree = "<group>"; };
|
A94721901D49DC0000858DD8 /* SBMetadataPrefsViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBMetadataPrefsViewController.h; path = Classes/SBMetadataPrefsViewController.h; sourceTree = "<group>"; };
|
||||||
A94721911D49DC0000858DD8 /* SBMetadataPrefsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBMetadataPrefsViewController.m; path = Classes/SBMetadataPrefsViewController.m; sourceTree = "<group>"; };
|
A94721911D49DC0000858DD8 /* SBMetadataPrefsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBMetadataPrefsViewController.m; path = Classes/SBMetadataPrefsViewController.m; sourceTree = "<group>"; };
|
||||||
A94A6A581ED1C5CA00049F7D /* ParseFilename */ = {isa = PBXFileReference; lastKnownFileType = folder; name = ParseFilename; path = Utilities/ParseFilename; sourceTree = "<group>"; };
|
A94A6A581ED1C5CA00049F7D /* ParseFilename */ = {isa = PBXFileReference; lastKnownFileType = folder; name = ParseFilename; path = Utilities/ParseFilename; sourceTree = "<group>"; };
|
||||||
|
A95395D21ED5839300E578D8 /* SBTheTVDBConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBTheTVDBConnection.h; path = Classes/MetadataImporters/SBTheTVDBConnection.h; sourceTree = "<group>"; };
|
||||||
|
A95395D31ED5839300E578D8 /* SBTheTVDBConnection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBTheTVDBConnection.m; path = Classes/MetadataImporters/SBTheTVDBConnection.m; sourceTree = "<group>"; };
|
||||||
A95B8DA41B09F37700F2F152 /* Subler Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Subler Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
|
A95B8DA41B09F37700F2F152 /* Subler Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Subler Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||||
A95B8DA71B09F37700F2F152 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
A95B8DA71B09F37700F2F152 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||||
A95B8DA81B09F37800F2F152 /* Subler_Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Subler_Tests.m; sourceTree = "<group>"; };
|
A95B8DA81B09F37800F2F152 /* Subler_Tests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Subler_Tests.m; sourceTree = "<group>"; };
|
||||||
@@ -654,6 +657,8 @@
|
|||||||
CD03087317576A550093A0BE /* SBTheMovieDB3.m */,
|
CD03087317576A550093A0BE /* SBTheMovieDB3.m */,
|
||||||
CD71FF1B12F1C4A900F0A98D /* SBTheTVDB.h */,
|
CD71FF1B12F1C4A900F0A98D /* SBTheTVDB.h */,
|
||||||
CD71FF1C12F1C4A900F0A98D /* SBTheTVDB.m */,
|
CD71FF1C12F1C4A900F0A98D /* SBTheTVDB.m */,
|
||||||
|
A95395D21ED5839300E578D8 /* SBTheTVDBConnection.h */,
|
||||||
|
A95395D31ED5839300E578D8 /* SBTheTVDBConnection.m */,
|
||||||
A901C51C1C04F03F00430DC3 /* SBChapterImporter.h */,
|
A901C51C1C04F03F00430DC3 /* SBChapterImporter.h */,
|
||||||
A901C51D1C04F03F00430DC3 /* SBChapterImporter.m */,
|
A901C51D1C04F03F00430DC3 /* SBChapterImporter.m */,
|
||||||
A9D4B8681C05CBEB007845C4 /* SBChapterResult.h */,
|
A9D4B8681C05CBEB007845C4 /* SBChapterResult.h */,
|
||||||
@@ -830,6 +835,7 @@
|
|||||||
A91690041DABFDAA004B9AB0 /* SBCheckBoxCellView.m in Sources */,
|
A91690041DABFDAA004B9AB0 /* SBCheckBoxCellView.m in Sources */,
|
||||||
A960E96317D4B00900FC6ED9 /* SBImageBrowserView.m in Sources */,
|
A960E96317D4B00900FC6ED9 /* SBImageBrowserView.m in Sources */,
|
||||||
A9DA3B9D1B918D5C00F1E40C /* SBExceptionAlertController.m in Sources */,
|
A9DA3B9D1B918D5C00F1E40C /* SBExceptionAlertController.m in Sources */,
|
||||||
|
A95395D41ED5839300E578D8 /* SBTheTVDBConnection.m in Sources */,
|
||||||
A94721931D49DC0000858DD8 /* SBMetadataPrefsViewController.m in Sources */,
|
A94721931D49DC0000858DD8 /* SBMetadataPrefsViewController.m in Sources */,
|
||||||
A9BC410318D9AB8700F8C92C /* SBItemViewController.m in Sources */,
|
A9BC410318D9AB8700F8C92C /* SBItemViewController.m in Sources */,
|
||||||
A9AB484917F0AEA2000AA72E /* SBAppDelegate.m in Sources */,
|
A9AB484917F0AEA2000AA72E /* SBAppDelegate.m in Sources */,
|
||||||
|
|||||||
Reference in new issue
Block a user