|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
-
- #import "TFHelper.h"
-
- @implementation TFHelper
-
- + (void)getUndeleteDataWithID:(NSString *)ident isComment:(BOOL)isComment timeout:(CGFloat)timeout extraData:(NSDictionary *)extra completionTarget:(id)target completionSelector:(SEL)sel {
-
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
-
- if (isComment){
- [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/comment/?ids=%@&fields=author,body", ident]]];
- } else {
- [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.pushshift.io/reddit/search/submission/?ids=%@&fields=author,selftext", ident]]];
- }
-
- [request setHTTPMethod:@"GET"];
- [request setTimeoutInterval:timeout];
-
- [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
-
- NSString *author = @"[author]";
- NSString *body = @"[body]";
-
- if (data != nil && error == nil){
- id jsonData = [[NSJSONSerialization JSONObjectWithData:data options:0 error:&error] objectForKey:@"data"];
- if ([jsonData count] != 0){
- author = [jsonData[0] objectForKey:@"author"];
- body = isComment ? [jsonData[0] objectForKey:@"body"] : [jsonData[0] objectForKey:@"selftext"];
- if ([body isEqualToString:@"[deleted]"] || [body isEqualToString:@"[removed]"]){
- body = @"[pushshift was unable to archive this]";
- }
- } else {
- body = @"[pushshift has not archived this yet]";
- }
- } else if (error != nil || data == nil){
- body = [NSString stringWithFormat:@"[an error occured while attempting to contact pushshift api (%@)]", [error localizedDescription]];
- }
-
- NSMutableDictionary *result = [@{@"author" : author, @"body" : body} mutableCopy];
-
- if (extra){
- [result addEntriesFromDictionary:extra];
- }
-
- [target performSelectorOnMainThread:sel withObject:result waitUntilDone:NO];
- }];
- }
-
- +(BOOL) shouldShowUndeleteButtonWithInfo:(NSString *) content isDeletedOnly:(BOOL) isDeletedOnly{
-
- if (!isDeletedOnly){
- return YES;
- } else {
- if ([content isEqualToString:@"[deleted]"] || [content isEqualToString:@"[removed]"]){
- return YES;
- } else if ([content hasPrefix:@"[pushshift"] || [content hasPrefix:@"[an error occured"]){
- return YES;
- }
- }
-
- return NO;
- }
-
- @end
|