-
Notifications
You must be signed in to change notification settings - Fork 8
/
SubtitlesDownloader.m
60 lines (50 loc) · 1.68 KB
/
SubtitlesDownloader.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// SubtitlesDownloader.m
// WhatSub
//
// Created by Marcin Grabda on 5/3/11.
// Copyright 2011 Marcin Grabda. All rights reserved.
//
#import "SubtitlesDownloader.h"
#import "NapiProjektEngine.h"
@implementation SubtitlesDownloader
- (id)initWithEngine:(NapiProjektEngine*)napiProjektEngine
{
self = [super init];
if (self)
{
engine = napiProjektEngine;
}
return self;
}
- (NSString*)download:(NSString*)pathToFile
{
NSString* hash = nil;
NSData* fileData = [engine retrieveSubtitlesForMovieInPath:pathToFile hash:&hash];
if (fileData != nil)
{
NSString* temporaryDirectory = NSTemporaryDirectory();
NSString* p7zipFileName = [hash stringByAppendingPathExtension:@"7z"];
NSString* p7zipOutputFilePath = [temporaryDirectory stringByAppendingPathComponent:p7zipFileName];
NSLog(@"Writing 7z file to %@", p7zipOutputFilePath);
[fileData writeToFile:p7zipOutputFilePath atomically:YES];
NSString* p7zip = [[NSBundle mainBundle] pathForResource:@"7za" ofType:nil];
NSArray* args = [NSArray arrayWithObjects:@"e", @"-y", @"-piBlm8NTigvru0Jr0", p7zipFileName, nil];
NSTask* task = [[NSTask alloc] init];
[task setCurrentDirectoryPath:temporaryDirectory];
[task setLaunchPath:p7zip];
[task setArguments:args];
[task launch];
[task waitUntilExit];
NSString* subtitlesFileName = [p7zipOutputFilePath stringByDeletingPathExtension];
NSString* filePath = [subtitlesFileName stringByAppendingPathExtension:@"txt"];
return filePath;
}
else
{
NSString* reason = @"Corrupted subtitles file";
NSException* e = [NSException exceptionWithName:@"SubtitlesException" reason:reason userInfo:nil];
@throw e;
}
}
@end