You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 line
3.0KB

  1. #import "TFHelper.h"
  2. @implementation TFHelper
  3. + (void)getUndeleteDataWithID:(NSString *)ident isComment:(BOOL)isComment timeout:(CGFloat)timeout extraData:(NSDictionary *)extra completionTarget:(id)target completionSelector:(SEL)sel {
  4. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  5. //NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  6. if (isComment){
  7. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body", ident]]];
  8. } else {
  9. [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext", ident]]];
  10. }
  11. [request setHTTPMethod:@"GET"];
  12. [request setTimeoutInterval:timeout];
  13. NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  14. //[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
  15. NSString *author = @"[author]";
  16. NSString *body = @"[body]";
  17. NSError *jsonError;
  18. if (data) {
  19. id jsonData = [[NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError] objectForKey:@"data"];
  20. if (jsonData && [jsonData count] != 0) {
  21. author = [jsonData[0] objectForKey:@"author"];
  22. body = isComment ? [jsonData[0] objectForKey:@"body"] : [jsonData[0] objectForKey:@"selftext"];
  23. if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
  24. body = [NSString stringWithFormat:@"[pushshift was unable to archive this %@]", isComment ? @"comment" : @"post"];
  25. }
  26. } else {
  27. body = [NSString stringWithFormat:@"[no data for this %@ was returned by pushshift]", isComment ? @"comment" : @"post"];
  28. }
  29. }
  30. if (error || jsonError) {
  31. body = [NSString stringWithFormat:@"[an error occurred while attempting retrieve data from the pushshift api]\n\nHTTP Status Code: %li", (long)((NSHTTPURLResponse *)response).statusCode];
  32. }
  33. if (error) {
  34. body = [body stringByAppendingFormat:@"\n\nRequest Error: %@", [error localizedDescription]];
  35. }
  36. if (jsonError) {
  37. body = [body stringByAppendingFormat:@"\n\nJSON Error: %@", [jsonError localizedDescription]];
  38. }
  39. NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:@{@"author" : author, @"body" : body}];
  40. if (extra){
  41. [result addEntriesFromDictionary:extra];
  42. }
  43. [target performSelectorOnMainThread:sel withObject:result waitUntilDone:NO];
  44. }];
  45. [dataTask resume];
  46. }
  47. + (BOOL)shouldShowUndeleteButtonWithInfo:(NSString *) content isDeletedOnly:(BOOL) isDeletedOnly{
  48. if (!isDeletedOnly){
  49. return YES;
  50. } else {
  51. if ([content isEqualToString:@"[deleted]"] || [content isEqualToString:@"[removed]"]){
  52. return YES;
  53. } else if ([content hasPrefix:@"[pushshift"] || [content hasPrefix:@"[an error occured"]){
  54. return YES;
  55. }
  56. }
  57. return NO;
  58. }
  59. @end