Fixed some deprecated api warnings.
This commit is contained in:
1 parent
7dcf857660
commit
5083abe3ed
12 files changed
+74
-88
No files matched your search
@@ -14,41 +14,55 @@
|
||||
|
||||
#define API_KEY @"ETET7TXFJH45YNYW0I4A"
|
||||
|
||||
@interface SBChapterDB ()
|
||||
|
||||
@property (nonatomic, readwrite) NSURLSessionTask *task;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SBChapterDB
|
||||
|
||||
- (NSArray<SBChapterResult *> *)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration
|
||||
- (void)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration completionHandler:(void(^)(NSArray<SBChapterResult *> *results))handler
|
||||
{
|
||||
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.chapterdb.org/chapters/search?title=%@", [SBMetadataHelper urlEncoded:title]]];
|
||||
|
||||
NSDictionary *headerOptions = @{@"ApiKey" : API_KEY};
|
||||
NSData *xmlData = xmlData = [SBMetadataHelper dataFromUrl:url withHTTPMethod:@"GET" headerOptions:headerOptions error:nil];
|
||||
self.task = [SBMetadataHelper sessionTaskFromUrl:url withHTTPMethod:@"GET" headerOptions:headerOptions completionHandler:^(NSData * _Nullable data) {
|
||||
NSXMLDocument *xml = [[NSXMLDocument alloc] initWithData:data
|
||||
options:0
|
||||
error:NULL];
|
||||
|
||||
NSXMLDocument *xml = [[NSXMLDocument alloc] initWithData:xmlData
|
||||
options:0
|
||||
error:NULL];
|
||||
NSMutableArray<SBChapterResult *> *resultsArray = [[NSMutableArray alloc] init];
|
||||
|
||||
NSMutableArray<SBChapterResult *> *resultsArray = [[NSMutableArray alloc] init];
|
||||
if (xml) {
|
||||
NSArray<NSXMLNode *> *children = [xml nodesForXPath:@"//*:chapterInfo" error:nil];
|
||||
|
||||
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];
|
||||
for (NSXMLNode *child in children) {
|
||||
SBChapterResult *result = [self parseResult:child];
|
||||
if (result) {
|
||||
[resultsArray addObject:result];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (duration > 0) {
|
||||
NSArray<SBChapterResult *> *filteredArray = [self filterArray:resultsArray byDuration:duration delta:10000];
|
||||
if (filteredArray.count) {
|
||||
return filteredArray;
|
||||
if (duration > 0) {
|
||||
NSArray<SBChapterResult *> *filteredArray = [self filterArray:resultsArray byDuration:duration delta:10000];
|
||||
if (filteredArray.count) {
|
||||
handler(filteredArray);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handler(resultsArray);
|
||||
|
||||
return resultsArray;
|
||||
}];
|
||||
|
||||
[self.task resume];
|
||||
}
|
||||
|
||||
- (void)cancel;
|
||||
{
|
||||
[self.task cancel];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,20 +13,16 @@
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface SBChapterImporter : NSObject {
|
||||
@private
|
||||
BOOL _isCancelled;
|
||||
}
|
||||
@interface SBChapterImporter : NSObject
|
||||
|
||||
+ (NSString *)defaultProvider;
|
||||
|
||||
+ (nullable instancetype)importerForProvider:(NSString *)providerName;
|
||||
- (void) searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration completionHandler:(void(^)(NSArray<SBChapterResult *> *results))handler;
|
||||
- (void) cancel;
|
||||
|
||||
#pragma mark Methods to be overridden
|
||||
|
||||
- (NSArray<SBChapterResult *> *)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration;
|
||||
- (void)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration completionHandler:(void(^)(NSArray<SBChapterResult *> *results))handler;
|
||||
- (void)cancel;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -11,16 +11,8 @@
|
||||
|
||||
#import "SBChapterDB.h"
|
||||
|
||||
@interface SBChapterImporter ()
|
||||
|
||||
@property (atomic, readwrite) BOOL isCancelled;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SBChapterImporter
|
||||
|
||||
@synthesize isCancelled = _isCancelled;
|
||||
|
||||
+ (NSString *)defaultProvider
|
||||
{
|
||||
return @"ChapterDB";
|
||||
@@ -36,22 +28,12 @@
|
||||
|
||||
- (void)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration completionHandler:(void(^)(NSArray<SBChapterResult *> *results))handler
|
||||
{
|
||||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||||
NSArray *results = [self searchTitle:title language:language duration:duration];
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (!self.isCancelled) {
|
||||
handler(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
- (void)cancel
|
||||
{
|
||||
self.isCancelled = YES;
|
||||
}
|
||||
|
||||
- (NSArray<SBChapterResult *> *)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration
|
||||
{
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
|
||||
|
||||
@@ -23,7 +23,7 @@ typedef NS_ENUM(NSUInteger, SBCachePolicy) {
|
||||
+ (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;
|
||||
+ (NSURLSessionTask *)sessionTaskFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header completionHandler:(void(^)(NSData * _Nullable data))completionHandler;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -121,14 +121,7 @@
|
||||
}
|
||||
|
||||
+ (NSString *)urlEncoded:(NSString *)string {
|
||||
string = string.precomposedStringWithCompatibilityMapping;
|
||||
CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes(
|
||||
NULL,
|
||||
(CFStringRef) string,
|
||||
NULL,
|
||||
(CFStringRef) @"!*'\"();:@&=+$,/?%#[]% ",
|
||||
kCFStringEncodingUTF8);
|
||||
return CFAutorelease(urlString);
|
||||
return [string.precomposedStringWithCompatibilityMapping stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
|
||||
}
|
||||
|
||||
+ (NSString *)md5String:(NSString *)s {
|
||||
@@ -189,7 +182,7 @@
|
||||
}
|
||||
|
||||
#pragma mark NSURLRequest
|
||||
+ (nullable NSData *)dataFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header error:(NSError * __autoreleasing *)outError
|
||||
+ (NSURLSessionTask *)sessionTaskFromUrl:(NSURL *)url withHTTPMethod:(NSString *)method headerOptions:(nullable NSDictionary *)header completionHandler:(void(^)(NSData * _Nullable data))completionHandler
|
||||
{
|
||||
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
|
||||
cachePolicy:NSURLRequestUseProtocolCachePolicy
|
||||
@@ -202,24 +195,24 @@
|
||||
}
|
||||
}
|
||||
|
||||
NSURLResponse *response = nil;
|
||||
NSError *error = nil;
|
||||
NSData *data = [NSURLConnection sendSynchronousRequest:request
|
||||
returningResponse:&response
|
||||
error:&error];
|
||||
NSURLSession *defaultSession = [NSURLSession sharedSession];
|
||||
NSURLSessionTask *task = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
||||
if (data) {
|
||||
NSUInteger statusCode = 0;
|
||||
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
||||
statusCode = (long)((NSHTTPURLResponse *)response).statusCode;
|
||||
}
|
||||
|
||||
if (data) {
|
||||
NSUInteger statusCode = 0;
|
||||
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
||||
statusCode = (long)((NSHTTPURLResponse *)response).statusCode;
|
||||
if (statusCode == 200) {
|
||||
completionHandler(data);
|
||||
}
|
||||
else {
|
||||
completionHandler(nil);
|
||||
}
|
||||
}
|
||||
|
||||
if (statusCode == 200) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}];
|
||||
return task;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user