1
0
mirror of https://github.com/lint/TFDidThatSay synced 2025-07-01 15:36:46 +00:00

Create helper class to avoid duplicated code

This commit is contained in:
lint
2019-12-29 14:30:17 -05:00
parent 2276c36ce0
commit 70190c98bd
30 changed files with 626 additions and 994 deletions

6
tweak/assets/TFHelper.h Normal file
View File

@ -0,0 +1,6 @@
@interface TFHelper : NSObject
+(void) getUndeleteDataWithID:(NSString *) ident isComment:(BOOL) isComment timeout:(CGFloat) timeout extraData:(NSDictionary *) extra completionTarget:(id) target completionSelector:(SEL) sel;
@end

50
tweak/assets/TFHelper.m Normal file
View File

@ -0,0 +1,50 @@
#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];
}];
}
@end