aka RedditBar, Mac OS X menu bar reddit client
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Reddit.m 6.4KB

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