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.

Reddit.m 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. -(NSString *)queryModhash {
  33. NSMutableString *stringData = [NSMutableString stringWithString:@"api_type=json"];
  34. [stringData appendFormat:@"&user=%@", [self urlencode: username]];
  35. [stringData appendFormat:@"&passwd=%@", [self urlencode: password]];
  36. [stringData appendString:@"&rem=True"];
  37. NSHTTPURLResponse *response;
  38. NSData *data = [self queryAPI:@"api/login" withData:stringData andResponse:&response];
  39. if (data == nil) {
  40. return nil;
  41. } else {
  42. long code = [response statusCode];
  43. if (code == 200) {
  44. NSError *error;
  45. id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
  46. if (error)
  47. return nil;
  48. if([object isKindOfClass:[NSDictionary class]]) {
  49. NSDictionary *results = object;
  50. NSDictionary *json = [results valueForKey:@"json"];
  51. if (json == nil)
  52. return nil;
  53. NSDictionary *data = [json valueForKey:@"data"];
  54. if (data == nil)
  55. return nil;
  56. return [data valueForKey:@"modhash"];
  57. } else {
  58. return nil;
  59. }
  60. } else {
  61. return nil;
  62. }
  63. }
  64. }
  65. -(NSData *)queryAPI:(NSString *)api withData:(NSString *)string andResponse:(NSHTTPURLResponse **)res {
  66. NSData *requestBodyData = [string dataUsingEncoding:NSUTF8StringEncoding];
  67. NSString *requestBodyLength = [NSString stringWithFormat:@"%lu", (unsigned long)[requestBodyData length]];
  68. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self getAPIPoint:api]];
  69. [request setTimeoutInterval:5.0];
  70. [request setCachePolicy:NSURLCacheStorageNotAllowed];
  71. [request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
  72. [request setValue:requestBodyLength forHTTPHeaderField:@"Content-Length"];
  73. [request setValue:[NSString stringWithFormat:@"%@/%@ by %@", appName, version, author] forHTTPHeaderField:@"User-Agent"];
  74. [request setHTTPMethod:@"POST"];
  75. [request setHTTPBody:requestBodyData];
  76. NSError *error = nil;
  77. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:res error:&error];
  78. if (error)
  79. return nil;
  80. else
  81. return data;
  82. }
  83. -(BOOL)isAuthenticated {
  84. return FALSE;
  85. }
  86. -(NSURL *)getAPIPoint:(NSString *)where {
  87. NSString *url = @"https://ssl.reddit.com";
  88. return [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", url, where]];
  89. }
  90. -(NSString *)urlencode:(NSString *)string {
  91. NSMutableString *output = [NSMutableString string];
  92. const unsigned char *source = (const unsigned char *)[string UTF8String];
  93. long sourceLen = strlen((const char *)source);
  94. for (int i = 0; i < sourceLen; i++) {
  95. const unsigned char thisChar = source[i];
  96. if (thisChar == ' '){
  97. [output appendString:@"+"];
  98. } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
  99. (thisChar >= 'a' && thisChar <= 'z') ||
  100. (thisChar >= 'A' && thisChar <= 'Z') ||
  101. (thisChar >= '0' && thisChar <= '9')) {
  102. [output appendFormat:@"%c", thisChar];
  103. } else {
  104. [output appendFormat:@"%%%02X", thisChar];
  105. }
  106. }
  107. return output;
  108. }
  109. @end