123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- //
- // ShareViewController.m
- // ShareSupport
- //
- // Created by HonorLee on 16/1/13.
- // Copyright © 2016年 HonorLee. All rights reserved.
- //
- #import "ShareViewController.h"
- #import <MobileCoreServices/MobileCoreServices.h>
- #import "HTMLParser.h"
- #import "TFHpple.h"
- @interface ShareViewController ()
- @property NSUserDefaults *userDefaults;
- @property UIView *AnalyseView;
- @property NSString *SharedURL;
- @property UILabel *AnalyseViewLabel;
- @property NSString *CurrentStatus;
- @property UIActivityIndicatorView *indicator;
- @property UILabel *StatusLabel;
- @property UIButton *DoneBtn;
- @property NSDictionary *tumblrData;
- @end
- @implementation ShareViewController
- -(void)viewDidLoad{
- UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
- [backgroundView setBackgroundColor:[UIColor blackColor]];
- [backgroundView setAlpha:0.6];
- [self.view sendSubviewToBack:backgroundView];
- [self.view addSubview:backgroundView];
- [self.view bringSubviewToFront:_FirstView];
- _FirstView.layer.cornerRadius = 5;
- _FirstView.layer.masksToBounds = YES;
- 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) {
- _SharedURL = url.absoluteString;
- dispatch_async(dispatch_get_main_queue(), ^{
- [_FistViewURLText setText:_SharedURL];
- // NSLog(@"%@",_SharedURL);
- });
- if(![_SharedURL containsString:@"tumblr.com"]){
- [_FirstNextBtn setEnabled:NO];
- }
- }];
- }
- }
- // Cancel
- - (IBAction)Cancel:(id)sender {
- [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
- }
- - (IBAction)Analyse:(id)sender {
- _AnalyseView = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-300)/2, (self.view.frame.size.height-140)/2, 300, 140)];
- [_AnalyseView setBackgroundColor:[UIColor whiteColor]];
- [_AnalyseView.layer setCornerRadius:5];
- [_AnalyseView.layer setMasksToBounds:YES];
- //Indicator
- _indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(110, 0, 80, 80)];
- [self.view addSubview:_AnalyseView];
- [_indicator startAnimating];
- [_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)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] ;
- if (error) {
- [self onError];
- return;
- }
- NSTextCheckingResult *r = [videoRegex firstMatchInString:secURL options:NSMatchingReportCompletion range:NSMakeRange(0, secURL.length)];
-
- NSString *videoID = nil;
- NSString *imgURL = nil;
-
- videoID = [secURL substringWithRange:r.range];
- eles = [doc searchWithXPathQuery:@"//video[@poster]"];
- if([eles count]>0){
- ele = [eles objectAtIndex:0];
- imgURL = [ele objectForKey:@"poster"];
- }
- _tumblrData = [[NSDictionary alloc] initWithObjectsAndKeys:videoID,@"videoID",imgURL,@"imgURL", nil];
- [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"]];
- if([tmpData count]==0){
- tmpData = [[NSMutableArray alloc]init];
- }
- [tmpData addObject:_tumblrData];
- [_userDefaults setObject:tmpData forKey:@"tasks"];
- [_userDefaults synchronize];
- NSLog(@"%@",_tumblrData);
- [self finish:nil];
- }
- @end
|