|
@@ -93,12 +93,36 @@ NSString *appName = @"RedditBar";
|
93
|
93
|
}
|
94
|
94
|
|
95
|
95
|
-(NSArray *)readFrontpageLength:(NSInteger)length {
|
96
|
|
- // TODO read frontpage
|
|
96
|
+ NSHTTPURLResponse *response;
|
|
97
|
+ NSString *url = [NSString stringWithFormat:@"hot.json?limit=%ld", (long)length];
|
|
98
|
+ NSData *data = [self queryAPI:url withResponse:&response];
|
|
99
|
+ if ((data == nil) || ([response statusCode] != 200)) {
|
|
100
|
+ return nil;
|
|
101
|
+ }
|
|
102
|
+ NSError *error;
|
|
103
|
+ NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
|
104
|
+ NSDictionary *dat = [json valueForKey:@"data"];
|
|
105
|
+ if (dat == nil)
|
|
106
|
+ return nil;
|
|
107
|
+ NSArray *children = [dat valueForKey:@"children"];
|
|
108
|
+ if (children == nil)
|
|
109
|
+ return nil;
|
97
|
110
|
|
98
|
|
- RedditItem *a = [RedditItem itemWithName:@"Test 1" Link:@"http://google.de" Comments:@"http://google.de" Self:NO];
|
99
|
|
- RedditItem *b = [RedditItem itemWithName:@"Test 2" Link:@"http://reddit.com" Comments:@"http://reddit.com" Self:NO];
|
100
|
|
- RedditItem *c = [RedditItem itemWithName:@"Test 3" Link:@"http://google.de" Comments:nil Self:YES];
|
101
|
|
- NSMutableArray *array = [NSMutableArray arrayWithObjects:a, b, c, nil];
|
|
111
|
+ NSMutableArray *array = [NSMutableArray arrayWithCapacity:[children count]];
|
|
112
|
+ for (NSUInteger i = 0; i < [children count]; i++) {
|
|
113
|
+ NSDictionary *child = [children objectAtIndex:i];
|
|
114
|
+ NSDictionary *current = [child valueForKey:@"data"];
|
|
115
|
+ NSString *name = [current valueForKey:@"title"];
|
|
116
|
+ NSString *link = [current valueForKey:@"url"];
|
|
117
|
+ NSString *comments = nil;
|
|
118
|
+ NSNumber *num = [current valueForKey:@"is_self"];
|
|
119
|
+ BOOL isSelf = [num boolValue];
|
|
120
|
+ if (!isSelf) {
|
|
121
|
+ comments = [NSString stringWithFormat:@"http://www.reddit.com%@", [current valueForKey:@"permalink"]];
|
|
122
|
+ }
|
|
123
|
+ RedditItem *r = [RedditItem itemWithName:name Link:link Comments:comments Self:isSelf];
|
|
124
|
+ [array insertObject:r atIndex:i];
|
|
125
|
+ }
|
102
|
126
|
return array;
|
103
|
127
|
}
|
104
|
128
|
|