aka RedditBar, Mac OS X menu bar reddit client
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // TODO read frontpage
  87. return nil;
  88. }
  89. -(NSArray *)readSubreddits:(NSArray *)source Length:(NSInteger)length {
  90. // TODO read subreddits
  91. return nil;
  92. }
  93. -(NSData *)queryAPI:(NSString *)api withResponse:(NSHTTPURLResponse **)res {
  94. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self getAPIPoint:api]];
  95. [request setTimeoutInterval:5.0];
  96. [request setCachePolicy:NSURLCacheStorageNotAllowed];
  97. [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
  98. [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
  99. if ((modhash != nil) && (![modhash isEqualToString:@""]))
  100. [request addValue:modhash forHTTPHeaderField:@"X-Modhash"];
  101. [request setHTTPMethod:@"GET"];
  102. NSError *error = nil;
  103. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:res error:&error];
  104. if (error)
  105. return nil;
  106. else
  107. return data;
  108. }
  109. -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res {
  110. NSData *requestBodyData = [string dataUsingEncoding:NSUTF8StringEncoding];
  111. NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
  112. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self getAPIPoint:api]];
  113. [request setTimeoutInterval:5.0];
  114. [request setCachePolicy:NSURLCacheStorageNotAllowed];
  115. [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
  116. [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
  117. [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
  118. if ((modhash != nil) && (![modhash isEqualToString:@""]))
  119. [request addValue:modhash forHTTPHeaderField:@"X-Modhash"];
  120. [request setHTTPMethod:@"POST"];
  121. [request setHTTPBody:requestBodyData];
  122. NSError *error = nil;
  123. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:res error:&error];
  124. if (error)
  125. return nil;
  126. else
  127. return data;
  128. }
  129. -(NSURL *)getAPIPoint:(NSString *)where {
  130. NSString *url = @"https://ssl.reddit.com";
  131. return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, where]];
  132. }
  133. -(NSString *)urlencode:(NSString *)string {
  134. NSMutableString *output = [NSMutableString string];
  135. const unsigned char *source = (const unsigned char *)[string UTF8String];
  136. long sourceLen = strlen((const char *)source);
  137. for (int i = 0; i < sourceLen; i++) {
  138. const unsigned char thisChar = source[i];
  139. if (thisChar == ' '){
  140. [output appendString:@"+"];
  141. } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
  142. (thisChar >= 'a' && thisChar <= 'z') ||
  143. (thisChar >= 'A' && thisChar <= 'Z') ||
  144. (thisChar >= '0' && thisChar <= '9')) {
  145. [output appendFormat:@"%c", thisChar];
  146. } else {
  147. [output appendFormat:@"%%%02X", thisChar];
  148. }
  149. }
  150. return output;
  151. }
  152. @end