Switch to the Swift chapter importer.
This commit is contained in:
1 parent
0ada574509
commit
c44e74d193
13 files changed
+297
-744
No files matched your search
@@ -7,16 +7,16 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
class ChapterDB : ChapterService {
|
||||
public struct ChapterDB : ChapterService {
|
||||
|
||||
func search(title: String, duration: UInt64) -> [ChapterResult] {
|
||||
public func search(title: String, duration: UInt64) -> [ChapterResult] {
|
||||
|
||||
guard let url = URL(string: "http://www.chapterdb.org/chapters/search?title=\(title.urlEncoded())"),
|
||||
let data = fetch(url: url)
|
||||
else { return [] }
|
||||
|
||||
let delta: UInt64 = 10000
|
||||
return parse(data: data).filter { $0.duration < (duration + delta) && $0.duration > (duration - delta) }
|
||||
let delta: Int64 = 10000
|
||||
return parse(data: data).filter { $0.duration < (Int64(duration) + delta) && $0.duration > (Int64(duration) - delta) }
|
||||
}
|
||||
|
||||
private func fetch(url: URL) -> Data? {
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
//
|
||||
// SBChapterDB.h
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 25/11/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SBChapterImporter.h"
|
||||
|
||||
@interface SBChapterDB : SBChapterImporter
|
||||
|
||||
@end
|
||||
@@ -1,165 +0,0 @@
|
||||
//
|
||||
// SBChapterDB.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 25/11/15.
|
||||
//
|
||||
//
|
||||
|
||||
#import "SBChapterDB.h"
|
||||
#import "SBChapterResult.h"
|
||||
|
||||
#import <MP42Foundation/MP42TextSample.h>
|
||||
#import <MP42Foundation/MP42Utilities.h>
|
||||
|
||||
#define API_KEY @"ETET7TXFJH45YNYW0I4A"
|
||||
|
||||
@interface SBChapterDB ()
|
||||
|
||||
@property (nonatomic, readwrite) NSURLSessionTask *task;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SBChapterDB
|
||||
|
||||
- (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};
|
||||
self.task = [SBMetadataHelper sessionTaskFromUrl:url HTTPMethod:@"GET" HTTPBody:nil headerOptions:headerOptions cachePolicy:NSURLRequestUseProtocolCachePolicy completionHandler:^(NSData * _Nullable data) {
|
||||
NSXMLDocument *xml = [[NSXMLDocument alloc] initWithData:data
|
||||
options:0
|
||||
error:NULL];
|
||||
|
||||
NSMutableArray<SBChapterResult *> *resultsArray = [[NSMutableArray alloc] init];
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (duration > 0) {
|
||||
NSArray<SBChapterResult *> *filteredArray = [self filterArray:resultsArray byDuration:duration delta:10000];
|
||||
if (filteredArray.count) {
|
||||
handler(filteredArray);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
handler(resultsArray);
|
||||
|
||||
}];
|
||||
|
||||
[self.task resume];
|
||||
}
|
||||
|
||||
- (void)cancel;
|
||||
{
|
||||
[self.task cancel];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 (NSUInteger)TimeFromString(durationString, 1000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
- (SBChapterResult *)parseResult:(NSXMLNode *)child
|
||||
{
|
||||
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];
|
||||
|
||||
MP42Duration currentTime = 0;
|
||||
|
||||
if (title && times && names) {
|
||||
|
||||
NSMutableArray<MP42TextSample *> *chapters = [NSMutableArray array];
|
||||
|
||||
for (NSInteger i = 0; i < times.count && i < names.count; i++) {
|
||||
|
||||
NSString *name = names[i].stringValue;
|
||||
NSString *timestamp = times[i].stringValue;
|
||||
|
||||
if (name && timestamp) {
|
||||
MP42Duration time = TimeFromString(timestamp, 1000);
|
||||
|
||||
if (time < currentTime) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
currentTime = time;
|
||||
}
|
||||
|
||||
MP42TextSample *chapter = [[MP42TextSample alloc] init];
|
||||
chapter.title = name;
|
||||
chapter.timestamp = time;
|
||||
|
||||
[chapters addObject:chapter];
|
||||
}
|
||||
}
|
||||
|
||||
if (chapters.count) {
|
||||
SBChapterResult *result = [[SBChapterResult alloc] initWithTitle:title
|
||||
duration:duration
|
||||
confirmations:confirmations
|
||||
chapters:chapters];
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -1,29 +0,0 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
+ (NSString *)defaultProvider;
|
||||
|
||||
+ (nullable instancetype)importerForProvider:(NSString *)providerName;
|
||||
|
||||
#pragma mark Methods to be overridden
|
||||
|
||||
- (void)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration completionHandler:(void(^)(NSArray<SBChapterResult *> *results))handler;
|
||||
- (void)cancel;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -1,43 +0,0 @@
|
||||
//
|
||||
// SBChapterImporter.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Michael Hueber on 21.11.15.
|
||||
//
|
||||
//
|
||||
|
||||
#import "SBChapterImporter.h"
|
||||
#import <MP42Foundation/MP42ChapterTrack.h>
|
||||
|
||||
#import "SBChapterDB.h"
|
||||
|
||||
@implementation SBChapterImporter
|
||||
|
||||
+ (NSString *)defaultProvider
|
||||
{
|
||||
return @"ChapterDB";
|
||||
}
|
||||
|
||||
+ (nullable instancetype)importerForProvider:(NSString *)aProvider
|
||||
{
|
||||
if ([aProvider isEqualToString:@"ChapterDB"]) {
|
||||
return [[SBChapterDB alloc] init];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)searchTitle:(NSString *)title language:(nullable NSString *)language duration:(NSUInteger)duration completionHandler:(void(^)(NSArray<SBChapterResult *> *results))handler
|
||||
{
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
- (void)cancel
|
||||
{
|
||||
@throw [NSException exceptionWithName:NSInternalInconsistencyException
|
||||
reason:[NSString stringWithFormat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]
|
||||
userInfo:nil];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -1,35 +0,0 @@
|
||||
//
|
||||
// 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)init NS_UNAVAILABLE;
|
||||
- (instancetype)initWithTitle:(NSString *)title duration:(NSUInteger)duration confirmations:(NSUInteger)confirmations chapters:(NSArray<MP42TextSample *> *)chapters NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
@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
|
||||
@@ -1,31 +0,0 @@
|
||||
//
|
||||
// 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
|
||||
Reference in new issue
Block a user