ShareViewController.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // ShareViewController.m
  3. // ShareSupport
  4. //
  5. // Created by HonorLee on 16/1/13.
  6. // Copyright © 2016年 HonorLee. All rights reserved.
  7. //
  8. #import "ShareViewController.h"
  9. #import <MobileCoreServices/MobileCoreServices.h>
  10. #import "HTMLParser.h"
  11. #import "TFHpple.h"
  12. @interface ShareViewController ()
  13. @property NSUserDefaults *userDefaults;
  14. @property UIView *AnalyseView;
  15. @property NSString *SharedURL;
  16. @property UILabel *AnalyseViewLabel;
  17. @property NSString *CurrentStatus;
  18. @property UIActivityIndicatorView *indicator;
  19. @property UILabel *StatusLabel;
  20. @property UIButton *DoneBtn;
  21. @property NSDictionary *tumblrData;
  22. @end
  23. @implementation ShareViewController
  24. -(void)viewDidLoad{
  25. UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  26. [backgroundView setBackgroundColor:[UIColor blackColor]];
  27. [backgroundView setAlpha:0.6];
  28. [self.view sendSubviewToBack:backgroundView];
  29. [self.view addSubview:backgroundView];
  30. [self.view bringSubviewToFront:_FirstView];
  31. _FirstView.layer.cornerRadius = 5;
  32. _FirstView.layer.masksToBounds = YES;
  33. NSExtensionItem *content = self.extensionContext.inputItems[0];
  34. NSItemProvider *itemProvider = content.attachments.firstObject;
  35. if([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]){
  36. [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url,NSError *err) {
  37. _SharedURL = url.absoluteString;
  38. dispatch_async(dispatch_get_main_queue(), ^{
  39. [_FistViewURLText setText:_SharedURL];
  40. // NSLog(@"%@",_SharedURL);
  41. });
  42. if(![_SharedURL containsString:@"tumblr.com"]){
  43. [_FirstNextBtn setEnabled:NO];
  44. }
  45. }];
  46. }
  47. }
  48. // Cancel
  49. - (IBAction)Cancel:(id)sender {
  50. [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
  51. }
  52. - (IBAction)Analyse:(id)sender {
  53. _AnalyseView = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-300)/2, (self.view.frame.size.height-140)/2, 300, 140)];
  54. [_AnalyseView setBackgroundColor:[UIColor whiteColor]];
  55. [_AnalyseView.layer setCornerRadius:5];
  56. [_AnalyseView.layer setMasksToBounds:YES];
  57. //Indicator
  58. _indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(110, 0, 80, 80)];
  59. [self.view addSubview:_AnalyseView];
  60. [_indicator startAnimating];
  61. [_indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
  62. [_indicator setHidesWhenStopped:YES];
  63. [_AnalyseView addSubview:_indicator];
  64. [UIView transitionFromView:_FirstView toView:_AnalyseView duration:0.3 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) {
  65. [_FirstView removeFromSuperview];
  66. dispatch_async(dispatch_get_main_queue(), ^{
  67. });
  68. [self startAnalyse];
  69. }];
  70. }
  71. #pragma AnalyseURL
  72. -(void)startAnalyse{
  73. //StatusLabel
  74. _StatusLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 300, 60)];
  75. [_StatusLabel setFont:[UIFont systemFontOfSize:15.0f]];
  76. [_StatusLabel setTextColor:[UIColor grayColor]];
  77. [_StatusLabel setTextAlignment:NSTextAlignmentCenter];
  78. [_StatusLabel setNumberOfLines:0];
  79. [_StatusLabel setHidden:YES];
  80. [_AnalyseView addSubview:_StatusLabel];
  81. //DONE button
  82. _DoneBtn = [[UIButton alloc]initWithFrame:CGRectMake(-1,100,302, 41)];
  83. [_DoneBtn setHidden:YES];
  84. [_DoneBtn setTitleColor:[UIColor colorWithRed:0 green:191/255.0 blue:1 alpha:1] forState:UIControlStateNormal];
  85. [_DoneBtn setTitle:@"DONE" forState:UIControlStateNormal];
  86. [_DoneBtn.layer setBorderWidth:1.0f];
  87. [_DoneBtn.layer setBorderColor:[UIColor lightGrayColor].CGColor];
  88. [_DoneBtn addTarget:self action:@selector(onPress:) forControlEvents:UIControlEventTouchDown];
  89. [_AnalyseView addSubview:_DoneBtn];
  90. NSError *error = nil;
  91. NSData *htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_SharedURL]];
  92. TFHpple *doc = [[TFHpple alloc]initWithHTMLData:htmlData];
  93. NSArray *eles = [doc searchWithXPathQuery:@"//iframe[@class='embed_iframe tumblr_video_iframe']"];
  94. if([eles count]>0){
  95. TFHppleElement *ele = [eles objectAtIndex:0];
  96. NSString *secURL = [ele objectForKey:@"src"];
  97. htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:secURL]];
  98. doc = [[TFHpple alloc]initWithHTMLData:htmlData];
  99. eles = [doc searchWithXPathQuery:@"//source[@type='video/mp4']"];
  100. if([eles count]>0){
  101. ele = [eles objectAtIndex:0];
  102. secURL = [ele objectForKey:@"src"];
  103. // NSLog(@"%@",secURL);
  104. NSRegularExpression *videoRegex = [NSRegularExpression regularExpressionWithPattern:@"tumblr_[a-zA-Z0-9]+" options:NSRegularExpressionCaseInsensitive error:&error] ;
  105. if (error) {
  106. [self onError];
  107. return;
  108. }
  109. NSTextCheckingResult *r = [videoRegex firstMatchInString:secURL options:NSMatchingReportCompletion range:NSMakeRange(0, secURL.length)];
  110. NSString *videoID = nil;
  111. NSString *imgURL = nil;
  112. videoID = [secURL substringWithRange:r.range];
  113. eles = [doc searchWithXPathQuery:@"//video[@poster]"];
  114. if([eles count]>0){
  115. ele = [eles objectAtIndex:0];
  116. imgURL = [ele objectForKey:@"poster"];
  117. }
  118. _tumblrData = [[NSDictionary alloc] initWithObjectsAndKeys:videoID,@"videoID",imgURL,@"imgURL", nil];
  119. [self success];
  120. }else{
  121. [self onError];
  122. }
  123. }else{
  124. [self onError];
  125. }
  126. // NSLog(@"%@",[bodyNode rawContents]);
  127. }
  128. -(void)onPress:(id)sender{
  129. [_DoneBtn setBackgroundColor:[UIColor colorWithRed:0 green:191/255.0 blue:1 alpha:1]];
  130. [_DoneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
  131. }
  132. -(void)success{
  133. [_StatusLabel setHidden:NO];
  134. [_StatusLabel setText:@"Success!\nOpen TumblrDownloader for next move"];
  135. [_indicator stopAnimating];
  136. [_DoneBtn addTarget:self action:@selector(saveToTasks:)forControlEvents:UIControlEventTouchUpInside];
  137. [_DoneBtn setHidden:NO];
  138. }
  139. -(void)onError{
  140. [_StatusLabel setHidden:NO];
  141. [_StatusLabel setText:@"Error!\nUnavailable Tumblr Video post share"];
  142. [_indicator stopAnimating];
  143. [_DoneBtn addTarget:self action:@selector(finish:)forControlEvents:UIControlEventTouchUpInside];
  144. [_DoneBtn setHidden:NO];
  145. }
  146. -(void)finish:(id)sender{
  147. [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
  148. }
  149. -(void)saveToTasks:(id)sender{
  150. _userDefaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.honorlee.TDGroup"];
  151. [_userDefaults synchronize];
  152. NSMutableArray *tmpData = [NSMutableArray arrayWithArray:(NSMutableArray *)[_userDefaults objectForKey:@"tasks"]];
  153. if([tmpData count]==0){
  154. tmpData = [[NSMutableArray alloc]init];
  155. }
  156. [tmpData addObject:_tumblrData];
  157. [_userDefaults setObject:tmpData forKey:@"tasks"];
  158. [_userDefaults synchronize];
  159. NSLog(@"%@",_tumblrData);
  160. [self finish:nil];
  161. }
  162. @end