|
@@ -8,8 +8,8 @@
|
|
|
|
|
|
#import "ShareViewController.h"
|
|
|
#import <MobileCoreServices/MobileCoreServices.h>
|
|
|
-#import <libxml/HTMLparser.h>
|
|
|
-
|
|
|
+#import "HTMLParser.h"
|
|
|
+#import "TFHpple.h"
|
|
|
|
|
|
@interface ShareViewController ()
|
|
|
@property NSUserDefaults *userDefaults;
|
|
@@ -18,42 +18,14 @@
|
|
|
@property UILabel *AnalyseViewLabel;
|
|
|
@property NSString *CurrentStatus;
|
|
|
@property UIActivityIndicatorView *indicator;
|
|
|
+@property UILabel *StatusLabel;
|
|
|
+@property UIButton *DoneBtn;
|
|
|
+@property NSString *RealVideoID;
|
|
|
+
|
|
|
@end
|
|
|
|
|
|
@implementation ShareViewController
|
|
|
-- (BOOL)isContentValid {
|
|
|
- // Do validation of contentText and/or NSExtensionContext attachments here
|
|
|
- return YES;
|
|
|
-}
|
|
|
|
|
|
-- (void)didSelectPost {
|
|
|
- // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
|
|
|
- _userDefaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.honorlee.TDGroup"];
|
|
|
- [_userDefaults synchronize];
|
|
|
- NSMutableArray *tmpData = [NSMutableArray arrayWithArray:(NSMutableArray *)[_userDefaults objectForKey:@"tasks"]];
|
|
|
- NSLog(@"%lu",(unsigned long)[tmpData count]);
|
|
|
-// NSArray *inputItems = self.extensionContext.inputItems;
|
|
|
-// NSLog(@"%lu",(unsigned long)[inputItems count]);
|
|
|
- NSExtensionItem *content = self.extensionContext.inputItems[0];
|
|
|
- NSItemProvider *itemProvider = content.attachments.firstObject;
|
|
|
- if([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]){
|
|
|
- [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url,NSError *err) {
|
|
|
- NSString *sharedURL = url.absoluteString;
|
|
|
- if([sharedURL containsString:@"tumblr.com"]){
|
|
|
- [tmpData addObject:sharedURL];
|
|
|
- [_userDefaults setObject:tmpData forKey:@"tasks"];
|
|
|
- [_userDefaults synchronize];
|
|
|
- }else{
|
|
|
- NSLog(@"Not tumblr");
|
|
|
- }
|
|
|
- }];
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-// NSString *type = (NSString *)
|
|
|
- // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
|
|
|
-
|
|
|
-}
|
|
|
-(void)viewDidLoad{
|
|
|
UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
|
|
|
[backgroundView setBackgroundColor:[UIColor blackColor]];
|
|
@@ -70,17 +42,14 @@
|
|
|
_SharedURL = url.absoluteString;
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
[_FistViewURLText setText:_SharedURL];
|
|
|
+ NSLog(@"%@",_SharedURL);
|
|
|
});
|
|
|
if(![_SharedURL containsString:@"tumblr.com"]){
|
|
|
- //[_FirstNextBtn setEnabled:NO];
|
|
|
+ [_FirstNextBtn setEnabled:NO];
|
|
|
}
|
|
|
}];
|
|
|
}
|
|
|
}
|
|
|
-- (NSArray *)configurationItems {
|
|
|
- // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
|
|
|
- return @[];
|
|
|
-}
|
|
|
|
|
|
// Cancel
|
|
|
- (IBAction)Cancel:(id)sender {
|
|
@@ -92,16 +61,100 @@
|
|
|
[_AnalyseView setBackgroundColor:[UIColor whiteColor]];
|
|
|
[_AnalyseView.layer setCornerRadius:5];
|
|
|
[_AnalyseView.layer setMasksToBounds:YES];
|
|
|
- _indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
|
|
|
+ //Indicator
|
|
|
+ _indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(110, 0, 80, 80)];
|
|
|
[self.view addSubview:_AnalyseView];
|
|
|
- [_AnalyseView addSubview:_indicator];
|
|
|
[_indicator startAnimating];
|
|
|
- [UIView transitionFromView:_FirstView toView:_AnalyseView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) {
|
|
|
+ [_indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
|
|
|
+ [_indicator setHidesWhenStopped:YES];
|
|
|
+ [_AnalyseView addSubview:_indicator];
|
|
|
+ [UIView transitionFromView:_FirstView toView:_AnalyseView duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) {
|
|
|
[_FirstView removeFromSuperview];
|
|
|
+ dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
+ });
|
|
|
+ [self startAnalyse];
|
|
|
+
|
|
|
}];
|
|
|
}
|
|
|
#pragma AnalyseURL
|
|
|
--(void)ChangeStatus{
|
|
|
+-(void)startAnalyse{
|
|
|
+ //StatusLabel
|
|
|
+ _StatusLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 300, 60)];
|
|
|
+ [_StatusLabel setFont:[UIFont systemFontOfSize:15.0f]];
|
|
|
+ [_StatusLabel setTextColor:[UIColor grayColor]];
|
|
|
+ [_StatusLabel setTextAlignment:NSTextAlignmentCenter];
|
|
|
+ [_StatusLabel setNumberOfLines:0];
|
|
|
+ [_StatusLabel setHidden:YES];
|
|
|
+ [_AnalyseView addSubview:_StatusLabel];
|
|
|
+
|
|
|
+ //DONE button
|
|
|
+ _DoneBtn = [[UIButton alloc]initWithFrame:CGRectMake(-1,100,302, 41)];
|
|
|
+ [_DoneBtn setHidden:YES];
|
|
|
+ [_DoneBtn setTitleColor:[UIColor colorWithRed:0 green:191/255.0 blue:1 alpha:1] forState:UIControlStateNormal];
|
|
|
+ [_DoneBtn setTitle:@"DONE" forState:UIControlStateNormal];
|
|
|
+ [_DoneBtn.layer setBorderWidth:1.0f];
|
|
|
+ [_DoneBtn.layer setBorderColor:[UIColor lightGrayColor].CGColor];
|
|
|
+ [_DoneBtn addTarget:self action:@selector(onPress:) forControlEvents:UIControlEventTouchDown];
|
|
|
+ [_AnalyseView addSubview:_DoneBtn];
|
|
|
+
|
|
|
+ NSError *error = nil;
|
|
|
+ NSData *htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_SharedURL]];
|
|
|
+ TFHpple *doc = [[TFHpple alloc]initWithHTMLData:htmlData];
|
|
|
+ NSArray *eles = [doc searchWithXPathQuery:@"//iframe[@class='embed_iframe tumblr_video_iframe']"];
|
|
|
+ if([eles count]>0){
|
|
|
+ TFHppleElement *ele = [eles objectAtIndex:0];
|
|
|
+ NSString *secURL = [ele objectForKey:@"src"];
|
|
|
+ htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:secURL]];
|
|
|
+ doc = [[TFHpple alloc]initWithHTMLData:htmlData];
|
|
|
+ eles = [doc searchWithXPathQuery:@"//source[@type='video/mp4']"];
|
|
|
+ if([eles count]>0){
|
|
|
+ ele = [eles objectAtIndex:0];
|
|
|
+ secURL = [ele objectForKey:@"src"];
|
|
|
+ NSLog(@"%@",secURL);
|
|
|
+
|
|
|
+ NSRegularExpression *videoRegex = [NSRegularExpression regularExpressionWithPattern:@"tumblr_[a-zA-Z0-9]+" options:NSRegularExpressionCaseInsensitive error:&error] ;
|
|
|
+ NSTextCheckingResult *r = [videoRegex firstMatchInString:secURL options:NSMatchingReportCompletion range:NSMakeRange(0, secURL.length)];
|
|
|
|
|
|
+ _RealVideoID = [secURL substringWithRange:r.range];
|
|
|
+ [self success];
|
|
|
+ }else{
|
|
|
+ [self onError];
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ [self onError];
|
|
|
+ }
|
|
|
+// NSLog(@"%@",[bodyNode rawContents]);
|
|
|
+
|
|
|
+}
|
|
|
+-(void)onPress:(id)sender{
|
|
|
+ [_DoneBtn setBackgroundColor:[UIColor colorWithRed:0 green:191/255.0 blue:1 alpha:1]];
|
|
|
+ [_DoneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
|
|
|
+}
|
|
|
+-(void)success{
|
|
|
+ [_StatusLabel setHidden:NO];
|
|
|
+ [_StatusLabel setText:@"Success!\nOpen TumblrDownloader for next move"];
|
|
|
+ [_indicator stopAnimating];
|
|
|
+ [_DoneBtn addTarget:self action:@selector(saveToTasks:)forControlEvents:UIControlEventTouchUpInside];
|
|
|
+ [_DoneBtn setHidden:NO];
|
|
|
+}
|
|
|
+-(void)onError{
|
|
|
+ [_StatusLabel setHidden:NO];
|
|
|
+ [_StatusLabel setText:@"Error!\nUnavailable Tumblr Video post share"];
|
|
|
+ [_indicator stopAnimating];
|
|
|
+ [_DoneBtn addTarget:self action:@selector(finish:)forControlEvents:UIControlEventTouchUpInside];
|
|
|
+ [_DoneBtn setHidden:NO];
|
|
|
+}
|
|
|
+-(void)finish:(id)sender{
|
|
|
+ [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
|
|
|
+}
|
|
|
+-(void)saveToTasks:(id)sender{
|
|
|
+ _userDefaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.honorlee.TDGroup"];
|
|
|
+ [_userDefaults synchronize];
|
|
|
+ NSMutableArray *tmpData = [NSMutableArray arrayWithArray:(NSMutableArray *)[_userDefaults objectForKey:@"tasks"]];
|
|
|
+ [tmpData addObject:_RealVideoID];
|
|
|
+ [_userDefaults setObject:tmpData forKey:@"tasks"];
|
|
|
+ [_userDefaults synchronize];
|
|
|
+
|
|
|
+ [self finish:nil];
|
|
|
}
|
|
|
@end
|