123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- //
- // Reddit.m
- // RedditBar
- //
- // Created by Thomas Buck on 01.12.13.
- // Copyright (c) 2013 xythobuz. All rights reserved.
- //
-
- #import "Reddit.h"
-
- @implementation Reddit
-
- NSString *version = @"1.0.0";
- NSString *author = @"xythobuz";
- NSString *appName = @"RedditBar";
-
- @synthesize username, modhash, password;
-
- -(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash {
- self = [super init];
- if (self) {
- username = name;
- modhash = hash;
- password = nil;
- }
- return self;
- }
-
- -(id)initWithUsername:(NSString *)name Password:(NSString *)pass {
- self = [super init];
- if (self) {
- username = name;
- modhash = nil;
- password = pass;
- }
- return self;
- }
-
- -(NSString *)queryModhash {
- NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
- [stringData appendFormat:@"&user=%@", [self urlencode: username]];
- [stringData appendFormat:@"&passwd=%@", [self urlencode: password]];
- [stringData appendString:@"&rem=True"];
- NSHTTPURLResponse *response;
- NSData *data = [self queryAPI:@"api/login" withData:stringData andResponse:&response];
- if (data == nil) {
- return nil;
- } else {
- long code = [response statusCode];
- if (code == 200) {
- NSError *error;
- id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
- if (error)
- return nil;
- if([object isKindOfClass:[NSDictionary class]]) {
- NSDictionary *results = object;
- NSDictionary *json = [results valueForKey:@"json"];
- if (json == nil)
- return nil;
- NSDictionary *data = [json valueForKey:@"data"];
- if (data == nil)
- return nil;
- return [data valueForKey:@"modhash"];
- } else {
- return nil;
- }
- } else {
- return nil;
- }
- }
- }
-
- -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res {
- NSData *requestBodyData = [string dataUsingEncoding:NSUTF8StringEncoding];
- NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self getAPIPoint:api]];
- [request setTimeoutInterval:5.0];
- [request setCachePolicy:NSURLCacheStorageNotAllowed];
- [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
- [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
- [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
- [request setHTTPMethod:@"POST"];
- [request setHTTPBody:requestBodyData];
- NSError *error = nil;
- NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:res error:&error];
- if (error)
- return nil;
- else
- return data;
- }
-
- -(BOOL)isAuthenticated {
- return FALSE;
- }
-
- -(NSURL *)getAPIPoint:(NSString *)where {
- NSString *url = @"https://ssl.reddit.com";
- return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, where]];
- }
-
- -(NSString *)urlencode:(NSString *)string {
- NSMutableString *output = [NSMutableString string];
- const unsigned char *source = (const unsigned char *)[string UTF8String];
- long sourceLen = strlen((const char *)source);
- for (int i = 0; i < sourceLen; i++) {
- const unsigned char thisChar = source[i];
- if (thisChar == ' '){
- [output appendString:@"+"];
- } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
- (thisChar >= 'a' && thisChar <= 'z') ||
- (thisChar >= 'A' && thisChar <= 'Z') ||
- (thisChar >= '0' && thisChar <= '9')) {
- [output appendFormat:@"%c", thisChar];
- } else {
- [output appendFormat:@"%%%02X", thisChar];
- }
- }
- return output;
- }
-
- @end
|