Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Modified Subscene service downloading method

  • Loading branch information...
commit 329075535879d9747d8bb8fa615d242dd658630a 1 parent 159df0d
@SebCorbin authored
Showing with 52 additions and 15 deletions.
  1. +50 −15 Services/SubsceneService.m
  2. +2 −0  SubSheetController.m
View
65 Services/SubsceneService.m
@@ -77,29 +77,64 @@ - (NSMutableArray *)searchSubtitlesForSubFile:(id)file {
+ (void)downloadSubtitleForSource:(SubSource *)source {
NSString *content = [NSString stringWithContentsOfURL:[source link] encoding:NSUTF8StringEncoding error:nil];
- // Parse the content of the movie list
+ // Parse the content of the subtitle page
HTMLNode *form = [[[[HTMLParser alloc] initWithString:content error:nil] body] findChildTag:@"form"];
NSURL *srtUrl = [NSURL URLWithString:[[SubsceneService serviceHost] stringByAppendingString:
[[[[form findChildOfClass:@"downloadLink rating100"]
getAttributeNamed:@"href"] componentsSeparatedByString:@"\""] objectAtIndex:7]]];
// Set up a request with the current link as 'Referer'
- NSMutableURLRequest *query = [NSMutableURLRequest requestWithURL:srtUrl];
- [query setValue:[[source link] absoluteString] forHTTPHeaderField:@"Referer"];
+ NSString *variables = [NSString stringWithFormat:@"subtitleId=%@&filmId=%@&typeId=%@",
+ [[form findChildWithAttribute:@"name" matchingName:@"subtitleId" allowPartial:NO] getAttributeNamed:@"value"],
+ [[form findChildWithAttribute:@"name" matchingName:@"filmId" allowPartial:NO] getAttributeNamed:@"value"],
+ [[form findChildWithAttribute:@"name" matchingName:@"typeId" allowPartial:NO] getAttributeNamed:@"value"]
+ ];
+ NSData *postVariables = [variables dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:srtUrl];
+ [request setHTTPMethod:@"POST"];
+ [request setValue:[[source link] absoluteString] forHTTPHeaderField:@"Referer"];
+ [request setHTTPBody:postVariables];
NSURLResponse *response = nil;
- NSData *data = [NSURLConnection sendSynchronousRequest:query returningResponse:&response error:NULL];
- // Storing .srt
- if ([[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"RenameFile"]) {
- srtUrl = [[[[source originalFile] localUrl] URLByDeletingPathExtension] URLByAppendingPathExtension:@"srt"];
- }
- else {
- srtUrl = [[[[source originalFile] localUrl] URLByDeletingLastPathComponent]
- URLByAppendingPathComponent:[response suggestedFilename]];
+ NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
+ if ([[response MIMEType] isEqualToString:@"application/zip"]) {
+ NSURL *zipUrl = [[[[source originalFile] localUrl] URLByDeletingPathExtension] URLByAppendingPathExtension:@"zip"];;
+ //NSURL *zipUrl = [NSURL fileURLWithPath:[[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:[response suggestedFilename]]];
+ NSError *writeError;
+ if (![data writeToURL:zipUrl atomically:YES]) {
+ // Error creating zip
+ }
+ NSTask *unzip = [[NSTask alloc] init];
+ NSPipe *zipPipe = [NSPipe pipe];
+ [unzip setLaunchPath:@"/usr/bin/unzip"];
+ [unzip setStandardOutput:zipPipe];
+ [unzip setArguments:[NSArray arrayWithObjects:@"-p", [NSString stringWithFormat:@"%@", [zipUrl relativePath]], nil]];
+ [unzip launch];
+ NSData *zipData = [[zipPipe fileHandleForReading] readDataToEndOfFile];
+ NSURL *srtUrl = [[[[source originalFile] localUrl] URLByDeletingPathExtension] URLByAppendingPathExtension:@"srt"];
+ [zipData writeToURL:srtUrl options:YES error:&writeError];
+ [unzip waitUntilExit];
+ [unzip release];
+ NSFileManager *fm = [NSFileManager defaultManager];
+ if (![fm removeItemAtURL:zipUrl error:NULL]) {
+ // error deleting
+ }
+ else {
+ // successfully deleted
+ }
+ [fm release];
}
- NSError *writeError = nil;
- if (![data writeToURL:srtUrl options:NSDataWritingAtomic error:&writeError]) {
- [[NSAlert alertWithError:writeError] runModal];
+ else { // Storing .srt
+ if ([[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"RenameFile"]) {
+ srtUrl = [[[[source originalFile] localUrl] URLByDeletingPathExtension] URLByAppendingPathExtension:@"srt"];
+ }
+ else {
+ srtUrl = [[[[source originalFile] localUrl] URLByDeletingLastPathComponent]
+ URLByAppendingPathComponent:[response suggestedFilename]];
+ }
+ NSError *writeError = nil;
+ if (![data writeToURL:srtUrl options:NSDataWritingAtomic error:&writeError]) {
+ [[NSAlert alertWithError:writeError] runModal];
+ }
}
-
}
+ (BOOL)handlesMovies {
View
2  SubSheetController.m
@@ -7,6 +7,7 @@
//
#import "SubSheetController.h";
+#import "Logger.h"
@implementation SubSheetController
@@ -14,6 +15,7 @@ @implementation SubSheetController
- (IBAction)downloadSubtitle:(id)sender {
// Get current selected subtitle
SubSource *subSource = [subtitles objectAtIndex:[table selectedRow]];
+ [Logger log:@"Downloading from link %@", [subSource link]];
[[subSource sourceClass] downloadSubtitleForSource:subSource];
[NSApp endSheet:subPanel];
[subPanel orderOut:nil];
Please sign in to comment.
Something went wrong with that request. Please try again.