First commit for ChapterDB support.
This commit is contained in:
1 parent
fd2a6118df
commit
be1a335591
24 files changed
+1394
-230
No files matched your search
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SBMetadataHelper.h"
|
||||
|
||||
@class MP42Metadata;
|
||||
|
||||
@@ -17,17 +18,6 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
BOOL _isCancelled;
|
||||
}
|
||||
|
||||
typedef NS_ENUM(NSUInteger, SBCachePolicy) {
|
||||
SBDefaultPolicy,
|
||||
SBReturnCacheElseLoad,
|
||||
SBReloadIgnoringLocalCacheData,
|
||||
};
|
||||
|
||||
#pragma mark Helper routines
|
||||
+ (NSDictionary<NSString *, NSString *> *)parseFilename:(NSString *)filename;
|
||||
+ (NSString *)urlEncoded:(NSString *)string;
|
||||
+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy;
|
||||
|
||||
#pragma mark Class methods
|
||||
+ (NSArray<NSString *> *) movieProviders;
|
||||
+ (NSArray<NSString *> *) tvProviders;
|
||||
|
||||
@@ -13,13 +13,10 @@
|
||||
|
||||
#import "SBMetadataSearchController.h"
|
||||
|
||||
#import <CommonCrypto/CommonDigest.h>
|
||||
|
||||
#import "iTunesStore.h"
|
||||
#import "TheMovieDB3.h"
|
||||
#import "TheTVDB.h"
|
||||
|
||||
|
||||
@interface MetadataImporter ()
|
||||
|
||||
@property (atomic, readwrite) BOOL isCancelled;
|
||||
@@ -30,190 +27,6 @@
|
||||
|
||||
@synthesize isCancelled = _isCancelled;
|
||||
|
||||
#pragma mark Helper routines
|
||||
|
||||
+ (NSDictionary<NSString *, NSString *> *)parseFilename:(NSString *)filename
|
||||
{
|
||||
NSParameterAssert(filename);
|
||||
|
||||
NSMutableDictionary<NSString *, NSString *> *results = nil;
|
||||
|
||||
// Try with the usual anime filename
|
||||
__block NSDictionary<NSString *, NSString *> *resultDictionary = nil;
|
||||
|
||||
NSString *pattern = @"^\\[(.+)\\](?:(?:\\s|_)+)?([^()]+)(?:(?:\\s|_)+)(?:(?:-\\s|-_|Ep)+)([0-9]+)";
|
||||
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
|
||||
options:NSRegularExpressionCaseInsensitive
|
||||
error:nil];
|
||||
|
||||
[regex enumerateMatchesInString:filename
|
||||
options:0
|
||||
range:NSMakeRange(0, filename.length)
|
||||
usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
|
||||
|
||||
resultDictionary = @{ @"fanSubGroup": [filename substringWithRange:[match rangeAtIndex:1]],
|
||||
@"seriesName": [filename substringWithRange:[match rangeAtIndex:2]],
|
||||
@"episodeNumber": [filename substringWithRange:[match rangeAtIndex:3]] };
|
||||
|
||||
}];
|
||||
|
||||
if (resultDictionary.count) {
|
||||
results = [[NSMutableDictionary alloc] init];
|
||||
NSString *seriesName = [resultDictionary[@"seriesName"] stringByReplacingOccurrencesOfString:@"_" withString:@" "];
|
||||
NSInteger episodeNumber = [resultDictionary[@"episodeNumber"] integerValue];
|
||||
|
||||
results[@"type"] = @"tv";
|
||||
results[@"seriesName"] = seriesName;
|
||||
results[@"seasonNum"] = @"1";
|
||||
results[@"episodeNum"] = [NSString stringWithFormat:@"%ld", (long)episodeNumber];
|
||||
|
||||
return [results autorelease];
|
||||
}
|
||||
|
||||
// Else use the ParseFilename perl script
|
||||
NSTask *task = [[NSTask alloc] init];
|
||||
[task setLaunchPath:@"/usr/bin/perl"];
|
||||
|
||||
NSMutableArray<NSString *> *args = [[NSMutableArray alloc] initWithCapacity:3];
|
||||
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"ParseFilename" ofType:@""];
|
||||
[args addObject:[NSString stringWithFormat:@"-I%@/lib", path]];
|
||||
[args addObject:[NSString stringWithFormat:@"%@/ParseFilename.pl", path]];
|
||||
[args addObject:filename];
|
||||
[task setArguments:args];
|
||||
|
||||
NSPipe *stdOut = [[NSPipe alloc] init];
|
||||
NSFileHandle *stdOutWrite = [stdOut fileHandleForWriting];
|
||||
[task setStandardOutput:stdOutWrite];
|
||||
|
||||
[task launch];
|
||||
[task waitUntilExit];
|
||||
[stdOutWrite closeFile];
|
||||
|
||||
NSData *outputData = [[stdOut fileHandleForReading] readDataToEndOfFile];
|
||||
NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
|
||||
NSArray<NSString *> *lines = [outputString componentsSeparatedByString:@"\n"];
|
||||
|
||||
if (lines.count) {
|
||||
|
||||
if ([lines.firstObject isEqualToString:@"tv"]) {
|
||||
|
||||
if (lines.count >= 4) {
|
||||
|
||||
results = [[NSMutableDictionary alloc] initWithCapacity:4];
|
||||
results[@"type"] = @"tv";
|
||||
|
||||
NSString *newSeriesName=[lines[1] stringByReplacingOccurrencesOfString:@"."
|
||||
withString:@" "];
|
||||
results[@"seriesName"] = newSeriesName;
|
||||
|
||||
if ([lines[2] integerValue]) {
|
||||
results[@"seasonNum"] = lines[2];
|
||||
}
|
||||
else {
|
||||
results[@"seasonNum"] = @"0";
|
||||
}
|
||||
|
||||
results[@"episodeNum"] = lines[3];
|
||||
}
|
||||
}
|
||||
else if ([lines.firstObject isEqualToString:@"movie"]) {
|
||||
|
||||
if (lines.count >= 2) {
|
||||
|
||||
results = [[NSMutableDictionary alloc] initWithCapacity:2];
|
||||
results[@"type"] = @"movie";
|
||||
|
||||
NSString *newTitle=[lines[1] stringByReplacingOccurrencesOfString:@"."
|
||||
withString:@" "];
|
||||
|
||||
newTitle = [newTitle stringByReplacingOccurrencesOfString:@"(" withString:@" "];
|
||||
newTitle = [newTitle stringByReplacingOccurrencesOfString:@")" withString:@" "];
|
||||
newTitle = [newTitle stringByReplacingOccurrencesOfString:@"[" withString:@" "];
|
||||
newTitle = [newTitle stringByReplacingOccurrencesOfString:@"]" withString:@" "];
|
||||
newTitle = [newTitle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
|
||||
results[@"title"] = newTitle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[outputString release];
|
||||
[stdOut release];
|
||||
[args release];
|
||||
[task release];
|
||||
|
||||
return [results autorelease];
|
||||
}
|
||||
|
||||
+ (NSString *)urlEncoded:(NSString *)string {
|
||||
string = [string precomposedStringWithCompatibilityMapping];
|
||||
CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes(
|
||||
NULL,
|
||||
(CFStringRef) string,
|
||||
NULL,
|
||||
(CFStringRef) @"!*'\"();:@&=+$,/?%#[]% ",
|
||||
kCFStringEncodingUTF8);
|
||||
return [(NSString *)urlString autorelease];
|
||||
}
|
||||
|
||||
+ (NSString *)md5String:(NSString *)s {
|
||||
const char *cStr = s.UTF8String;
|
||||
unsigned char result[CC_MD5_DIGEST_LENGTH];
|
||||
CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
|
||||
NSMutableString *r = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
|
||||
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; ++i) {
|
||||
[r appendFormat:@"%02x", result[i]];
|
||||
}
|
||||
return [NSString stringWithString:r];
|
||||
}
|
||||
|
||||
+ (NSString *)sha256String:(NSString *)s {
|
||||
const char *cStr = s.UTF8String;
|
||||
unsigned char result[CC_SHA256_DIGEST_LENGTH];
|
||||
CC_SHA256(cStr, (CC_LONG)strlen(cStr), result);
|
||||
NSMutableString *r = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
|
||||
for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; ++i) {
|
||||
[r appendFormat:@"%02x", result[i]];
|
||||
}
|
||||
return [NSString stringWithString:r];
|
||||
}
|
||||
|
||||
+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy error:(NSError **)error {
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
NSString *bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
|
||||
NSURL *cacheURL = [[[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask]
|
||||
firstObject] URLByAppendingPathComponent:bundleName];
|
||||
|
||||
|
||||
NSURL *fileURL = [cacheURL URLByAppendingPathComponent:[MetadataImporter sha256String:url.absoluteString]];
|
||||
fileURL = [fileURL URLByAppendingPathExtension:url.pathExtension];
|
||||
|
||||
if (policy != SBReloadIgnoringLocalCacheData) {
|
||||
NSDictionary<NSString *, id> *attrs = [fileURL resourceValuesForKeys:@[NSURLCreationDateKey] error:NULL];
|
||||
NSDate *creationDate = attrs[NSURLCreationDateKey];
|
||||
|
||||
if (creationDate) {
|
||||
NSTimeInterval oldness = creationDate.timeIntervalSinceNow;
|
||||
|
||||
// if less than 2 hours old or jpg
|
||||
if ([url.pathExtension caseInsensitiveCompare:@"jpg"] == NSOrderedSame ||
|
||||
(oldness > -60 * 60 * 2) ||
|
||||
policy == SBReturnCacheElseLoad) {
|
||||
return [NSData dataWithContentsOfURL:fileURL options:0 error:error];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NSData *downloadedData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:error];
|
||||
[downloadedData writeToURL:fileURL atomically:YES];
|
||||
|
||||
return downloadedData;
|
||||
}
|
||||
|
||||
+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy {
|
||||
return [MetadataImporter downloadDataFromURL:url withCachePolicy:policy error:NULL];
|
||||
}
|
||||
|
||||
#pragma mark Class methods
|
||||
|
||||
+ (NSArray<NSString *> *)movieProviders {
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
//
|
||||
// SBChapterDB.h
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 25/11/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SBChapterImporter.h"
|
||||
|
||||
@interface SBChapterDB : SBChapterImporter
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,141 @@
|
||||
//
|
||||
// SBChapterDB.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 25/11/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import "SBChapterDB.h"
|
||||
#import "SBChapterResult.h"
|
||||
|
||||
#import <MP42Foundation/MP42ChapterTrack.h>
|
||||
|
||||
#define API_KEY @"ETET7TXFJH45YNYW0I4A"
|
||||
|
||||
@implementation SBChapterDB
|
||||
|
||||
- (NSArray<SBChapterResult *> *)searchTitle:(NSString *)aTitle duration:(NSUInteger)duration
|
||||
{
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.chapterdb.org/chapters/search?title=%@", [SBMetadataHelper urlEncoded:aTitle]]];
|
||||
|
||||
NSDictionary *headerOptions = @{@"ApiKey" : API_KEY};
|
||||
NSData *xmlData = xmlData = [SBMetadataHelper dataFromUrl:url withHTTPMethod:@"GET" headerOptions:headerOptions error:nil];
|
||||
|
||||
NSXMLDocument *xml = [[NSXMLDocument alloc] initWithData:xmlData
|
||||
options:0
|
||||
error:nil];
|
||||
|
||||
NSMutableArray<SBChapterResult *> *resultsArray = [[[NSMutableArray alloc] init] autorelease];
|
||||
|
||||
if (xml) {
|
||||
NSArray<NSXMLNode *> *children = [xml nodesForXPath:@"//*:chapterInfo" error:nil];
|
||||
|
||||
for (NSXMLNode *child in children) {
|
||||
SBChapterResult *result = [self parseResult:child];
|
||||
if (result) {
|
||||
[resultsArray addObject:result];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[xml release];
|
||||
|
||||
if (duration > 0) {
|
||||
NSArray<SBChapterResult *> *filteredArray = [self filterArray:resultsArray byDuration:duration delta:10000];
|
||||
if (filteredArray.count) {
|
||||
return filteredArray;
|
||||
}
|
||||
}
|
||||
|
||||
return resultsArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the results by the duration plus/minus a delta time
|
||||
*
|
||||
* @param array the array of SBChapterResult
|
||||
* @param duration the movie duration
|
||||
* @param delta the delta in ms
|
||||
*
|
||||
* @return the filtered array.
|
||||
*/
|
||||
- (NSArray<SBChapterResult *> *)filterArray:(NSArray *)array byDuration:(NSUInteger)duration delta:(NSUInteger)delta
|
||||
{
|
||||
NSMutableArray<SBChapterResult *> *filteredArray = [NSMutableArray array];
|
||||
|
||||
for (SBChapterResult *result in array) {
|
||||
if (result.duration < (duration + delta) && result.duration > (duration - delta)) {
|
||||
[filteredArray addObject:result];
|
||||
}
|
||||
}
|
||||
|
||||
return filteredArray;
|
||||
}
|
||||
|
||||
#pragma mark - Result convertion
|
||||
|
||||
- (NSString *)titleForNode:(NSXMLNode *)node
|
||||
{
|
||||
NSArray<NSXMLNode *> *tag = [node nodesForXPath:@"./*:title" error:NULL];
|
||||
return tag.firstObject.stringValue;
|
||||
}
|
||||
|
||||
- (NSUInteger)confirmationsForNode:(NSXMLNode *)node
|
||||
{
|
||||
NSArray<NSXMLNode *> *tag = [node nodesForXPath:@"./@confirmations" error:NULL];
|
||||
return tag.firstObject.stringValue.integerValue;
|
||||
}
|
||||
|
||||
- (NSUInteger)durationForNode:(NSXMLNode *)node
|
||||
{
|
||||
NSArray<NSXMLNode *> *tag = [node nodesForXPath:@"./*:source/*:duration" error:NULL];
|
||||
NSString *durationString = tag.firstObject.stringValue;
|
||||
if (durationString) {
|
||||
return ParseSubTime(durationString.UTF8String, 1000, NO);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
- (SBChapterResult *)parseResult:(NSXMLNode *)child
|
||||
{
|
||||
NSMutableArray<MP42TextSample *> *chapters = [NSMutableArray array];
|
||||
|
||||
NSString *title = [self titleForNode:child];
|
||||
NSUInteger confirmations = [self confirmationsForNode:child];
|
||||
NSUInteger duration = [self durationForNode:child];
|
||||
|
||||
NSArray<NSXMLNode *> *times = [child nodesForXPath:@"./*:chapters/*:chapter/@time" error:NULL];
|
||||
NSArray<NSXMLNode *> *names = [child nodesForXPath:@"./*:chapters/*:chapter/@name" error:NULL];
|
||||
|
||||
if (times && names) {
|
||||
|
||||
for (NSInteger i = 0; i < times.count && i < names.count; i++) {
|
||||
|
||||
NSString *name = names[i].stringValue;
|
||||
NSString *timestamp = times[i].stringValue;
|
||||
|
||||
if (name && timestamp) {
|
||||
unsigned time = ParseSubTime(timestamp.UTF8String, 1000, NO);
|
||||
|
||||
MP42TextSample *chapter = [[MP42TextSample alloc] init];
|
||||
chapter.title = name;
|
||||
chapter.timestamp = time;
|
||||
|
||||
[chapters addObject:chapter];
|
||||
[chapter release];
|
||||
}
|
||||
}
|
||||
|
||||
SBChapterResult *result = [[SBChapterResult alloc] initWithTitle:title
|
||||
duration:duration
|
||||
confirmations:confirmations
|
||||
chapters:chapters];
|
||||
|
||||
return [result autorelease];
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// SBChapterImporter.h
|
||||
// Subler
|
||||
//
|
||||
// Created by Michael Hueber on 21.11.15.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SBMetadataHelper.h"
|
||||
|
||||
@class SBChapterResult;
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface SBChapterImporter : NSObject {
|
||||
@private
|
||||
BOOL _isCancelled;
|
||||
}
|
||||
|
||||
+ (NSString *)defaultProvider;
|
||||
|
||||
+ (instancetype)importerForProvider:(NSString *)aProviderName;
|
||||
- (void) searchTitle:(NSString *)title duration:(NSUInteger)duration completionHandler:(void(^)(NSArray<SBChapterResult *> * _Nullable results))handler;
|
||||
- (void) cancel;
|
||||
|
||||
#pragma mark Methods to be overridden
|
||||
|
||||
- (NSArray<SBChapterResult *> *)searchTitle:(NSString *)title duration:(NSUInteger)duration;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// SBChapterImporter.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Michael Hueber on 21.11.15.
|
||||
//
|
||||
//
|
||||
|
||||
#import "SBChapterImporter.h"
|
||||
#import <MP42Foundation/MP42ChapterTrack.h>
|
||||
|
||||
#import "SBChapterDB.h"
|
||||
|
||||
@interface SBChapterImporter ()
|
||||
|
||||
@property (atomic, readwrite) BOOL isCancelled;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SBChapterImporter
|
||||
|
||||
@synthesize isCancelled = _isCancelled;
|
||||
|
||||
+ (NSString *)defaultProvider
|
||||
{
|
||||
return @"ChapterDB";
|
||||
}
|
||||
|
||||
+ (instancetype)importerForProvider:(NSString *)aProvider
|
||||
{
|
||||
if ([aProvider isEqualToString:@"ChapterDB"]) {
|
||||
return [[[SBChapterDB alloc] init] autorelease];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)searchTitle:(NSString *)title duration:(NSUInteger)duration completionHandler:(void(^)(NSArray<SBChapterResult *> * _Nullable results))handler
|
||||
{
|
||||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||||
NSArray *results = [self searchTitle:title duration:duration];
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (!self.isCancelled) {
|
||||
handler(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
- (void)cancel
|
||||
{
|
||||
self.isCancelled = YES;
|
||||
}
|
||||
|
||||
- (NSArray<SBChapterResult *> *)searchTitle:(NSString *)title duration:(NSUInteger)duration
|
||||
{
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// SBChapterResult.h
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 25/11/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class MP42TextSample;
|
||||
|
||||
@interface SBChapterResult : NSObject {
|
||||
@private
|
||||
NSString *_title;
|
||||
NSUInteger _duration;
|
||||
NSUInteger _confirmations;
|
||||
|
||||
NSArray<MP42TextSample *> *_chapters;
|
||||
}
|
||||
|
||||
- (instancetype)initWithTitle:(NSString *)title duration:(NSUInteger)duration confirmations:(NSUInteger)confirmations chapters:(NSArray<MP42TextSample *> *)chapters;
|
||||
|
||||
@property (nonatomic, readonly) NSString *title;
|
||||
@property (nonatomic, readonly) NSUInteger duration;
|
||||
@property (nonatomic, readonly) NSUInteger confirmations;
|
||||
|
||||
@property (nonatomic, readonly) NSArray<MP42TextSample *> *chapters;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// SBChapterResult.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 25/11/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import "SBChapterResult.h"
|
||||
|
||||
@implementation SBChapterResult
|
||||
|
||||
@synthesize title = _title;
|
||||
@synthesize duration = _duration;
|
||||
@synthesize confirmations = _confirmations;
|
||||
|
||||
@synthesize chapters = _chapters;
|
||||
|
||||
- (instancetype)initWithTitle:(NSString *)title duration:(NSUInteger)duration confirmations:(NSUInteger)confirmations chapters:(NSArray<MP42TextSample *> *)chapters
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
_title = [title copy];
|
||||
_duration = duration;
|
||||
_confirmations = confirmations;
|
||||
_chapters = [chapters copy];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// SBMetadataHelper.h
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 25/11/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface SBMetadataHelper : NSObject
|
||||
|
||||
typedef NS_ENUM(NSUInteger, SBCachePolicy) {
|
||||
SBDefaultPolicy,
|
||||
SBReturnCacheElseLoad,
|
||||
SBReloadIgnoringLocalCacheData,
|
||||
};
|
||||
|
||||
#pragma mark Helper routines
|
||||
+ (NSDictionary<NSString *, NSString *> *)parseFilename:(NSString *)filename;
|
||||
+ (NSString *)urlEncoded:(NSString *)string;
|
||||
|
||||
+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy;
|
||||
+ (nullable NSData *)dataFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header error:(NSError **)outError;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -0,0 +1,230 @@
|
||||
//
|
||||
// SBMetadataHelper.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 25/11/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import "SBMetadataHelper.h"
|
||||
#import <CommonCrypto/CommonDigest.h>
|
||||
|
||||
@implementation SBMetadataHelper
|
||||
|
||||
+ (NSDictionary<NSString *, NSString *> *)parseFilename:(NSString *)filename
|
||||
{
|
||||
NSParameterAssert(filename);
|
||||
|
||||
NSMutableDictionary<NSString *, NSString *> *results = nil;
|
||||
|
||||
// Try with the usual anime filename
|
||||
__block NSDictionary<NSString *, NSString *> *resultDictionary = nil;
|
||||
|
||||
NSString *pattern = @"^\\[(.+)\\](?:(?:\\s|_)+)?([^()]+)(?:(?:\\s|_)+)(?:(?:-\\s|-_|Ep)+)([0-9]+)";
|
||||
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
|
||||
options:NSRegularExpressionCaseInsensitive
|
||||
error:nil];
|
||||
|
||||
[regex enumerateMatchesInString:filename
|
||||
options:0
|
||||
range:NSMakeRange(0, filename.length)
|
||||
usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
|
||||
|
||||
resultDictionary = @{ @"fanSubGroup": [filename substringWithRange:[match rangeAtIndex:1]],
|
||||
@"seriesName": [filename substringWithRange:[match rangeAtIndex:2]],
|
||||
@"episodeNumber": [filename substringWithRange:[match rangeAtIndex:3]] };
|
||||
|
||||
}];
|
||||
|
||||
if (resultDictionary.count) {
|
||||
results = [[NSMutableDictionary alloc] init];
|
||||
NSString *seriesName = [resultDictionary[@"seriesName"] stringByReplacingOccurrencesOfString:@"_" withString:@" "];
|
||||
NSInteger episodeNumber = [resultDictionary[@"episodeNumber"] integerValue];
|
||||
|
||||
results[@"type"] = @"tv";
|
||||
results[@"seriesName"] = seriesName;
|
||||
results[@"seasonNum"] = @"1";
|
||||
results[@"episodeNum"] = [NSString stringWithFormat:@"%ld", (long)episodeNumber];
|
||||
|
||||
return [results autorelease];
|
||||
}
|
||||
|
||||
// Else use the ParseFilename perl script
|
||||
NSTask *task = [[NSTask alloc] init];
|
||||
[task setLaunchPath:@"/usr/bin/perl"];
|
||||
|
||||
NSMutableArray<NSString *> *args = [[NSMutableArray alloc] initWithCapacity:3];
|
||||
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"ParseFilename" ofType:@""];
|
||||
[args addObject:[NSString stringWithFormat:@"-I%@/lib", path]];
|
||||
[args addObject:[NSString stringWithFormat:@"%@/ParseFilename.pl", path]];
|
||||
[args addObject:filename];
|
||||
[task setArguments:args];
|
||||
|
||||
NSPipe *stdOut = [[NSPipe alloc] init];
|
||||
NSFileHandle *stdOutWrite = [stdOut fileHandleForWriting];
|
||||
[task setStandardOutput:stdOutWrite];
|
||||
|
||||
[task launch];
|
||||
[task waitUntilExit];
|
||||
[stdOutWrite closeFile];
|
||||
|
||||
NSData *outputData = [[stdOut fileHandleForReading] readDataToEndOfFile];
|
||||
NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
|
||||
NSArray<NSString *> *lines = [outputString componentsSeparatedByString:@"\n"];
|
||||
|
||||
if (lines.count) {
|
||||
|
||||
if ([lines.firstObject isEqualToString:@"tv"]) {
|
||||
|
||||
if (lines.count >= 4) {
|
||||
|
||||
results = [[NSMutableDictionary alloc] initWithCapacity:4];
|
||||
results[@"type"] = @"tv";
|
||||
|
||||
NSString *newSeriesName=[lines[1] stringByReplacingOccurrencesOfString:@"."
|
||||
withString:@" "];
|
||||
results[@"seriesName"] = newSeriesName;
|
||||
|
||||
if ([lines[2] integerValue]) {
|
||||
results[@"seasonNum"] = lines[2];
|
||||
}
|
||||
else {
|
||||
results[@"seasonNum"] = @"0";
|
||||
}
|
||||
|
||||
results[@"episodeNum"] = lines[3];
|
||||
}
|
||||
}
|
||||
else if ([lines.firstObject isEqualToString:@"movie"]) {
|
||||
|
||||
if (lines.count >= 2) {
|
||||
|
||||
results = [[NSMutableDictionary alloc] initWithCapacity:2];
|
||||
results[@"type"] = @"movie";
|
||||
|
||||
NSString *newTitle=[lines[1] stringByReplacingOccurrencesOfString:@"."
|
||||
withString:@" "];
|
||||
|
||||
newTitle = [newTitle stringByReplacingOccurrencesOfString:@"(" withString:@" "];
|
||||
newTitle = [newTitle stringByReplacingOccurrencesOfString:@")" withString:@" "];
|
||||
newTitle = [newTitle stringByReplacingOccurrencesOfString:@"[" withString:@" "];
|
||||
newTitle = [newTitle stringByReplacingOccurrencesOfString:@"]" withString:@" "];
|
||||
newTitle = [newTitle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||
|
||||
results[@"title"] = newTitle;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[outputString release];
|
||||
[stdOut release];
|
||||
[args release];
|
||||
[task release];
|
||||
|
||||
return [results autorelease];
|
||||
}
|
||||
|
||||
+ (NSString *)urlEncoded:(NSString *)string {
|
||||
string = [string precomposedStringWithCompatibilityMapping];
|
||||
CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes(
|
||||
NULL,
|
||||
(CFStringRef) string,
|
||||
NULL,
|
||||
(CFStringRef) @"!*'\"();:@&=+$,/?%#[]% ",
|
||||
kCFStringEncodingUTF8);
|
||||
return [(NSString *)urlString autorelease];
|
||||
}
|
||||
|
||||
+ (NSString *)md5String:(NSString *)s {
|
||||
const char *cStr = s.UTF8String;
|
||||
unsigned char result[CC_MD5_DIGEST_LENGTH];
|
||||
CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
|
||||
NSMutableString *r = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
|
||||
for (int i = 0; i < CC_MD5_DIGEST_LENGTH; ++i) {
|
||||
[r appendFormat:@"%02x", result[i]];
|
||||
}
|
||||
return [NSString stringWithString:r];
|
||||
}
|
||||
|
||||
+ (NSString *)sha256String:(NSString *)s {
|
||||
const char *cStr = s.UTF8String;
|
||||
unsigned char result[CC_SHA256_DIGEST_LENGTH];
|
||||
CC_SHA256(cStr, (CC_LONG)strlen(cStr), result);
|
||||
NSMutableString *r = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
|
||||
for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; ++i) {
|
||||
[r appendFormat:@"%02x", result[i]];
|
||||
}
|
||||
return [NSString stringWithString:r];
|
||||
}
|
||||
|
||||
+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy error:(NSError **)error {
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
NSString *bundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
|
||||
NSURL *cacheURL = [[[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask]
|
||||
firstObject] URLByAppendingPathComponent:bundleName];
|
||||
|
||||
|
||||
NSURL *fileURL = [cacheURL URLByAppendingPathComponent:[SBMetadataHelper sha256String:url.absoluteString]];
|
||||
fileURL = [fileURL URLByAppendingPathExtension:url.pathExtension];
|
||||
|
||||
if (policy != SBReloadIgnoringLocalCacheData) {
|
||||
NSDictionary<NSString *, id> *attrs = [fileURL resourceValuesForKeys:@[NSURLCreationDateKey] error:NULL];
|
||||
NSDate *creationDate = attrs[NSURLCreationDateKey];
|
||||
|
||||
if (creationDate) {
|
||||
NSTimeInterval oldness = creationDate.timeIntervalSinceNow;
|
||||
|
||||
// if less than 2 hours old or jpg
|
||||
if ([url.pathExtension caseInsensitiveCompare:@"jpg"] == NSOrderedSame ||
|
||||
(oldness > -60 * 60 * 2) ||
|
||||
policy == SBReturnCacheElseLoad) {
|
||||
return [NSData dataWithContentsOfURL:fileURL options:0 error:error];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NSData *downloadedData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:error];
|
||||
[downloadedData writeToURL:fileURL atomically:YES];
|
||||
|
||||
return downloadedData;
|
||||
}
|
||||
|
||||
+ (nullable NSData *)downloadDataFromURL:(NSURL *)url withCachePolicy:(SBCachePolicy)policy {
|
||||
return [SBMetadataHelper downloadDataFromURL:url withCachePolicy:policy error:NULL];
|
||||
}
|
||||
|
||||
#pragma mark NSURLRequest
|
||||
+ (nullable NSData *)dataFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header error:(NSError **)outError
|
||||
{
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
|
||||
cachePolicy:NSURLRequestUseProtocolCachePolicy
|
||||
timeoutInterval:30.0];
|
||||
[request setHTTPMethod:method];
|
||||
|
||||
if (header) {
|
||||
for (NSString *key in header.allKeys) {
|
||||
[request addValue:header[key] forHTTPHeaderField:key];
|
||||
}
|
||||
}
|
||||
|
||||
NSURLResponse *response = nil;
|
||||
NSError *error = nil;
|
||||
NSData *data = [NSURLConnection sendSynchronousRequest:request
|
||||
returningResponse:&response
|
||||
error:&error];
|
||||
|
||||
if (data) {
|
||||
NSUInteger statusCode = 0;
|
||||
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
||||
statusCode = (long)[(NSHTTPURLResponse *)response statusCode];
|
||||
}
|
||||
|
||||
if (statusCode == 200) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -25,8 +25,8 @@
|
||||
|
||||
- (NSArray *) searchMovie:(NSString *)aMovieTitle language:(NSString *)aLanguage {
|
||||
NSString *lang = [MP42Languages iso6391CodeFor:aLanguage];
|
||||
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];
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.themoviedb.org/3/search/movie?api_key=%@&query=%@&language=%@", API_KEY, [SBMetadataHelper urlEncoded:aMovieTitle], lang]];
|
||||
NSData *jsonData = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
if (jsonData) {
|
||||
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
||||
if ([d isKindOfClass:[NSDictionary class]]) {
|
||||
@@ -52,7 +52,7 @@
|
||||
|
||||
// load image variables from configuration
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.themoviedb.org/3/configuration?api_key=%@", API_KEY]];
|
||||
NSData *jsonData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
NSData *jsonData = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
NSArray *posters = [r valueForKeyPath:@"images.posters"];
|
||||
|
||||
if (jsonData && posters && [posters isKindOfClass:[NSArray class]]) {
|
||||
@@ -99,7 +99,7 @@
|
||||
NSString *lang = [MP42Languages iso6391CodeFor:aLanguage];
|
||||
NSNumber *theMovieDBID = [[aMetadata tagsDict] valueForKey:@"TheMovieDB ID"];
|
||||
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];
|
||||
NSData *jsonData = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
if (jsonData) {
|
||||
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
||||
if ([d isKindOfClass:[NSDictionary class]]) {
|
||||
|
||||
@@ -24,7 +24,7 @@ static NSArray *TVDBlanguages;
|
||||
+ (void)initialize {
|
||||
if (self == [TheTVDB class]) {
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://thetvdb.com/api/%@/languages.xml", API_KEY]];
|
||||
NSData *languagesXML = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBReturnCacheElseLoad];
|
||||
NSData *languagesXML = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBReturnCacheElseLoad];
|
||||
NSDictionary *languages = [XMLReader dictionaryForXMLData:languagesXML error:NULL];
|
||||
|
||||
if (languages) {
|
||||
@@ -53,8 +53,8 @@ static NSArray *TVDBlanguages;
|
||||
- (NSArray *) searchTVSeries:(NSString *)aSeriesName language:(NSString *)aLanguage {
|
||||
NSURL *url;
|
||||
// search for series
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"http://thetvdb.com/api/GetSeries.php?seriesname=%@&language=all", [MetadataImporter urlEncoded:aSeriesName]]];
|
||||
NSData *seriesXML = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"http://thetvdb.com/api/GetSeries.php?seriesname=%@&language=all", [SBMetadataHelper urlEncoded:aSeriesName]]];
|
||||
NSData *seriesXML = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
NSDictionary *series = [XMLReader dictionaryForXMLData:seriesXML error:NULL];
|
||||
if (!series) return nil;
|
||||
NSArray *seriesArray = [series retrieveArrayForPath:@"Data.Series"];
|
||||
@@ -71,8 +71,8 @@ static NSArray *TVDBlanguages;
|
||||
NSURL *url;
|
||||
|
||||
// search for series
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"http://thetvdb.com/api/GetSeries.php?seriesname=%@&language=all", [MetadataImporter urlEncoded:aSeriesName]]];
|
||||
NSData *seriesXML = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"http://thetvdb.com/api/GetSeries.php?seriesname=%@&language=all", [SBMetadataHelper urlEncoded:aSeriesName]]];
|
||||
NSData *seriesXML = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
NSDictionary *series = [XMLReader dictionaryForXMLData:seriesXML error:NULL];
|
||||
|
||||
if (!series) return nil;
|
||||
@@ -102,7 +102,7 @@ static NSArray *TVDBlanguages;
|
||||
continue;
|
||||
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"http://thetvdb.com/api/%@/series/%@/all/%@.xml", API_KEY, seriesID, lang]];
|
||||
NSData *episodesXML = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
NSData *episodesXML = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
NSDictionary *episodes = [XMLReader dictionaryForXMLData:episodesXML error:NULL];
|
||||
|
||||
if (!episodes)
|
||||
@@ -150,7 +150,7 @@ static NSArray *TVDBlanguages;
|
||||
[newArtworkProviderNames addObjectsFromArray:[aMetadata artworkProviderNames]];
|
||||
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://thetvdb.com/api/%@/series/%@/banners.xml", API_KEY, [[aMetadata tagsDict] valueForKey:@"TheTVDB Series ID"]]];
|
||||
NSData *bannersXML = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
NSData *bannersXML = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
NSDictionary *banners = [XMLReader dictionaryForXMLData:bannersXML error:NULL];
|
||||
if (!banners) return nil;
|
||||
NSArray *bannersArray = [banners retrieveArrayForPath:@"Banners.Banner"];
|
||||
|
||||
@@ -127,11 +127,11 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context)
|
||||
|
||||
NSURL *url;
|
||||
if (aSeasonNum && [aSeasonNum length]) {
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/search?country=%@&lang=%@&term=%@&attribute=tvSeasonTerm&entity=tvEpisode&limit=200", country, [language lowercaseString], [MetadataImporter urlEncoded:[NSString stringWithFormat:@"%@ %@ %@", aSeriesName, season, aSeasonNum]]]];
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/search?country=%@&lang=%@&term=%@&attribute=tvSeasonTerm&entity=tvEpisode&limit=200", country, [language lowercaseString], [SBMetadataHelper urlEncoded:[NSString stringWithFormat:@"%@ %@ %@", aSeriesName, season, aSeasonNum]]]];
|
||||
} else {
|
||||
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]]];
|
||||
url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/search?country=%@&lang=%@&term=%@&attribute=showTerm&entity=tvEpisode&limit=200", country, [language lowercaseString], [SBMetadataHelper urlEncoded:aSeriesName]]];
|
||||
}
|
||||
NSData *jsonData = [MetadataImporter downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
NSData *jsonData = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
|
||||
if (jsonData) {
|
||||
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
||||
@@ -169,8 +169,8 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context)
|
||||
}
|
||||
NSString *country = [store valueForKey:@"country2"];
|
||||
NSString *language = [store valueForKey:@"language2"];
|
||||
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];
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/search?country=%@&lang=%@&term=%@&entity=tvEpisode", country, [language lowercaseString], [SBMetadataHelper urlEncoded:[NSString stringWithFormat:@"%@ %@", aSeriesName, aEpisodeTitle]]]];
|
||||
NSData *jsonData = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
if (jsonData) {
|
||||
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
||||
if ([d isKindOfClass:[NSDictionary class]]) {
|
||||
@@ -188,8 +188,8 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context)
|
||||
}
|
||||
NSString *country = [store valueForKey:@"country2"];
|
||||
NSString *language = [store valueForKey:@"language2"];
|
||||
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];
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/search?country=%@&lang=%@&term=%@&entity=movie", country, language, [SBMetadataHelper urlEncoded:aMovieName]]];
|
||||
NSData *jsonData = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
if (jsonData) {
|
||||
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
||||
if ([d isKindOfClass:[NSDictionary class]]) {
|
||||
@@ -212,8 +212,8 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context)
|
||||
language = [store valueForKey:@"language2"];
|
||||
}
|
||||
|
||||
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];
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/search?country=%@&lang=%@&term=%@&entity=movie&limit=150", country, language, [SBMetadataHelper urlEncoded:aMovieTitle]]];
|
||||
NSData *jsonData = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
if (jsonData) {
|
||||
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
||||
if ([d isKindOfClass:[NSDictionary class]]) {
|
||||
@@ -233,7 +233,7 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context)
|
||||
NSString *country = [store valueForKey:@"country2"];
|
||||
NSString *language = [store valueForKey:@"language2"];
|
||||
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];
|
||||
NSData *jsonData = [SBMetadataHelper downloadDataFromURL:url withCachePolicy:SBDefaultPolicy];
|
||||
if (jsonData) {
|
||||
NSDictionary *d = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
|
||||
if ([d isKindOfClass:[NSDictionary class]]) {
|
||||
@@ -248,7 +248,7 @@ NSInteger sortMP42Metadata(id ep1, id ep2, void *context)
|
||||
}
|
||||
|
||||
- (MP42Metadata *) loadMovieMetadata:(MP42Metadata *)aMetadata language:(NSString *)aLanguage {
|
||||
NSData *xmlData = [MetadataImporter downloadDataFromURL:[NSURL URLWithString:[[aMetadata tagsDict] valueForKey:@"iTunes URL"]] withCachePolicy:SBDefaultPolicy];
|
||||
NSData *xmlData = [SBMetadataHelper downloadDataFromURL:[NSURL URLWithString:[[aMetadata tagsDict] valueForKey:@"iTunes URL"]] withCachePolicy:SBDefaultPolicy];
|
||||
if (xmlData) {
|
||||
NSDictionary *store = [iTunesStore getStoreFor:aLanguage];
|
||||
NSXMLDocument *xml = [[NSXMLDocument alloc] initWithData:xmlData options:NSXMLDocumentTidyHTML error:NULL];
|
||||
|
||||
Reference in new issue
Block a user