aka RedditBar, Mac OS X menu bar reddit client
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Reddit.m
  3. // RedditBar
  4. //
  5. // Created by Thomas Buck on 01.12.13.
  6. // Copyright (c) 2013 xythobuz. All rights reserved.
  7. //
  8. #import "Reddit.h"
  9. @implementation Reddit
  10. NSString *version = @"1.0.0";
  11. NSString *author = @"xythobuz";
  12. NSString *appName = @"RedditBar";
  13. @synthesize username, modhash, password;
  14. -(id)initWithUsername:(NSString *)name Modhash:(NSString *)hash {
  15. self = [super init];
  16. if (self) {
  17. username = name;
  18. modhash = hash;
  19. password = nil;
  20. }
  21. return self;
  22. }
  23. -(id)initWithUsername:(NSString *)name Password:(NSString *)pass {
  24. self = [super init];
  25. if (self) {
  26. username = name;
  27. modhash = nil;
  28. password = pass;
  29. }
  30. return self;
  31. }
  32. -(BOOL)isAuthenticatedNewModhash:(NSString **)newModHash {
  33. NSHTTPURLResponse *response;
  34. NSData *data = [self queryAPI:@"api/me.json" withResponse:&response];
  35. if ((data != nil) && ([response statusCode] == 200)) {
  36. NSError *error;
  37. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  38. NSDictionary *data = [json valueForKey:@"data"];
  39. if (data == nil)
  40. return FALSE;
  41. NSString *newHash = [data valueForKey:@"modhash"];
  42. if ((newHash == nil) || ([newHash isEqualToString:@""]))
  43. return FALSE;
  44. if (![newHash isEqualToString:modhash]) {
  45. modhash = newHash;
  46. *newModHash = newHash;
  47. }
  48. return TRUE;
  49. }
  50. return FALSE;
  51. }
  52. -(NSString *)queryModhash {
  53. NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
  54. [stringData appendFormat:@"&user=%@", [self urlencode: username]];
  55. [stringData appendFormat:@"&passwd=%@", [self urlencode: password]];
  56. [stringData appendString:@"&rem=True"];
  57. NSHTTPURLResponse *response;
  58. NSData *data = [self queryAPI:@"api/login" withData:stringData andResponse:&response];
  59. if (data == nil) {
  60. return nil;
  61. } else {
  62. long code = [response statusCode];
  63. if (code == 200) {
  64. NSError *error;
  65. id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  66. if (error)
  67. return nil;
  68. if([object isKindOfClass:[NSDictionary class]]) {
  69. NSDictionary *results = object;
  70. NSDictionary *json = [results valueForKey:@"json"];
  71. if (json == nil)
  72. return nil;
  73. NSDictionary *data = [json valueForKey:@"data"];
  74. if (data == nil)
  75. return nil;
  76. return [data valueForKey:@"modhash"];
  77. } else {
  78. return nil;
  79. }
  80. } else {
  81. return nil;
  82. }
  83. }
  84. }
  85. -(NSArray *)readFrontpageLength:(NSInteger)length {
  86. NSHTTPURLResponse *response;
  87. NSString *url = [NSString stringWithFormat:@"hot.json?limit=%ld", (long)length];
  88. NSData *data = [self queryAPI:url withResponse:&response];
  89. if ((data == nil) || ([response statusCode] != 200)) {
  90. return nil;
  91. }
  92. NSError *error;
  93. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  94. NSDictionary *dat = [json valueForKey:@"data"];
  95. if (dat == nil)
  96. return nil;
  97. NSArray *children = [dat valueForKey:@"children"];
  98. if (children == nil)
  99. return nil;
  100. NSMutableArray *array = [NSMutableArray arrayWithCapacity:[children count]];
  101. for (NSUInteger i = 0; i < [children count]; i++) {
  102. NSDictionary *child = [children objectAtIndex:i];
  103. NSDictionary *current = [child valueForKey:@"data"];
  104. NSString *name = [current valueForKey:@"title"];
  105. NSString *link = [current valueForKey:@"url"];
  106. NSString *comments = nil;
  107. NSNumber *num = [current valueForKey:@"is_self"];
  108. BOOL isSelf = [num boolValue];
  109. if (!isSelf) {
  110. comments = [NSString stringWithFormat:@"http://www.reddit.com%@", [current valueForKey:@"permalink"]];
  111. }
  112. RedditItem *r = [RedditItem itemWithName:name Link:link Comments:comments Self:isSelf];
  113. [array insertObject:r atIndex:i];
  114. }
  115. return array;
  116. }
  117. -(NSArray *)readSubreddits:(NSArray *)source Length:(NSInteger)length {
  118. // TODO read subreddits
  119. return nil;
  120. }
  121. -(NSData *)queryAPI:(NSString *)api withResponse:(NSHTTPURLResponse **)res {
  122. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self getAPIPoint:api]];
  123. [request setTimeoutInterval:5.0];
  124. [request setCachePolicy:NSURLCacheStorageNotAllowed];
  125. [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
  126. [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
  127. if ((modhash != nil) && (![modhash isEqualToString:@""]))
  128. [request addValue:modhash forHTTPHeaderField:@"X-Modhash"];
  129. [request setHTTPMethod:@"GET"];
  130. NSError *error = nil;
  131. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:res error:&error];
  132. if (error)
  133. return nil;
  134. else
  135. return data;
  136. }
  137. -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res {
  138. NSData *requestBodyData = [string dataUsingEncoding:NSUTF8StringEncoding];
  139. NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
  140. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self getAPIPoint:api]];
  141. [request setTimeoutInterval:5.0];
  142. [request setCachePolicy:NSURLCacheStorageNotAllowed];
  143. [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
  144. [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
  145. [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
  146. if ((modhash != nil) && (![modhash isEqualToString:@""]))
  147. [request addValue:modhash forHTTPHeaderField:@"X-Modhash"];
  148. [request setHTTPMethod:@"POST"];
  149. [request setHTTPBody:requestBodyData];
  150. NSError *error = nil;
  151. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:res error:&error];
  152. if (error)
  153. return nil;
  154. else
  155. return data;
  156. }
  157. -(NSURL *)getAPIPoint:(NSString *)where {
  158. NSString *url = @"https://ssl.reddit.com";
  159. return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, where]];
  160. }
  161. -(NSString *)urlencode:(NSString *)string {
  162. NSMutableString *output = [NSMutableString string];
  163. const unsigned char *source = (const unsigned char *)[string UTF8String];
  164. long sourceLen = strlen((const char *)source);
  165. for (int i = 0; i < sourceLen; i++) {
  166. const unsigned char thisChar = source[i];
  167. if (thisChar == ' '){
  168. [output appendString:@"+"];
  169. } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
  170. (thisChar >= 'a' && thisChar <= 'z') ||
  171. (thisChar >= 'A' && thisChar <= 'Z') ||
  172. (thisChar >= '0' && thisChar <= '9')) {
  173. [output appendFormat:@"%c", thisChar];
  174. } else {
  175. [output appendFormat:@"%%%02X", thisChar];
  176. }
  177. }
  178. return output;
  179. }
  180. @end