Last active
August 29, 2015 13:56
-
-
Save utsengar/9062169 to your computer and use it in GitHub Desktop.
wiki client in objc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Wikipedia.m | |
// yoda | |
// | |
// Created by Utkarsh Sengar on 2/9/14. | |
// Copyright (c) 2014 area42. All rights reserved. | |
// | |
#import "Wikipedia.h" | |
#import "AFNetworking.h" | |
#import "Question.h" | |
@implementation Wikipedia | |
NSString *WIKIPEDIA_BASE_URL = @"https://en.wikipedia.org/w/api.php"; | |
-(void)getImagesAndTitleForCategory:(NSString *)categoryName | |
limitCount:(NSInteger)count | |
success:(void (^)(AFHTTPRequestOperation *operation, id response))success | |
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{ | |
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; | |
//Get list of articles by category | |
NSMutableDictionary *getPagesQueryParams = [[NSMutableDictionary alloc] init]; | |
getPagesQueryParams[@"action"] = @"query"; | |
getPagesQueryParams[@"prop"] = @"images"; | |
getPagesQueryParams[@"format"] = @"json"; | |
getPagesQueryParams[@"imlimit"] = @"10"; | |
getPagesQueryParams[@"generator"] = @"categorymembers"; | |
getPagesQueryParams[@"gcmtitle"] = [@"Category:" stringByAppendingString:categoryName]; | |
getPagesQueryParams[@"gcmnamespace"] = @"0"; | |
getPagesQueryParams[@"gcmlimit"] = @"50"; | |
[manager GET:WIKIPEDIA_BASE_URL | |
parameters:getPagesQueryParams | |
success:^(AFHTTPRequestOperation *task, id responseObject) { | |
NSLog(@"JSON in 1: %@", responseObject); | |
NSMutableArray *testInput = [[NSMutableArray alloc] init]; | |
[testInput addObject:@"Trip_Adler"]; | |
[self getGetImageForTitles:testInput manager:manager success:success failure:failure]; | |
} | |
failure:^(AFHTTPRequestOperation *task, NSError *error) { | |
NSLog(@"Error in 1: %@", error); | |
}]; | |
} | |
-(void)getGetImageForTitles:(NSArray *)articleTitles | |
manager:(AFHTTPRequestOperationManager *)manager | |
success:(void (^)(AFHTTPRequestOperation *operation, id response))success | |
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{ | |
//Get article title from response | |
NSMutableDictionary *getImageTitlesParams = [[NSMutableDictionary alloc] init]; | |
getImageTitlesParams[@"action"] = @"query"; | |
for (NSString *title in articleTitles) { | |
//TODO: Fix it | |
getImageTitlesParams[@"titles"] = title; | |
} | |
getImageTitlesParams[@"format"] = @"json"; | |
getImageTitlesParams[@"prop"] = @"images"; | |
[manager GET:WIKIPEDIA_BASE_URL | |
parameters:getImageTitlesParams | |
success:^(AFHTTPRequestOperation *task, id responseObject) { | |
NSLog(@"JSON in 2: %@", responseObject); | |
Question *question = [[Question alloc] init]; | |
[question setValue:@"pageUrl" forKey:@""]; | |
[question setValue:@"title" forKey:@""]; | |
[question setValue:@"imgUrl" forKey:@""]; | |
NSMutableArray *testInput = [[NSMutableArray alloc] init]; | |
[testInput addObject:question]; | |
[self getGetImageUrlsFromImageTitle:testInput articleTitles:articleTitles manager:manager success:success failure:failure]; | |
} | |
failure:^(AFHTTPRequestOperation *task, NSError *error) { | |
NSLog(@"Error in 2: %@", error); | |
}]; | |
} | |
-(void)getGetImageUrlsFromImageTitle:(NSArray *)imageTitles | |
articleTitles:(NSArray *)articleTitles | |
manager:(AFHTTPRequestOperationManager *)manager | |
success:(void (^)(AFHTTPRequestOperation *operation, id response))success | |
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure{ | |
//Get Image title from response | |
NSMutableDictionary *getImageUrlParams = [[NSMutableDictionary alloc] init]; | |
getImageUrlParams[@"action"] = @"query"; | |
for (Question *question in imageTitles) { | |
//TODO: Fix it | |
getImageUrlParams[@"titles"] = [@"Image:" stringByAppendingString:question.imgUrl]; //@"Image:Next Avengers - Heroes of Tomorrow Coverart.png"; | |
} | |
getImageUrlParams[@"format"] = @"json"; | |
getImageUrlParams[@"prop"] = @"imageinfo"; | |
getImageUrlParams[@"iiprop"] = @"url"; | |
[manager GET:WIKIPEDIA_BASE_URL | |
parameters:getImageUrlParams | |
success:success | |
failure:failure]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment