From 70b58f39e17def0d7e8707f01d8373698ddf6285 Mon Sep 17 00:00:00 2001 From: Skalii Date: Tue, 30 Apr 2024 17:59:52 +0300 Subject: [PATCH] sync nearby photos code with android; --- .../TargetMenu/OATargetInfoViewController.mm | 10 +- Sources/Helpers/OAWikiImageHelper.h | 9 +- Sources/Helpers/OAWikiImageHelper.mm | 130 +++++++++++------- 3 files changed, 88 insertions(+), 61 deletions(-) diff --git a/Sources/Controllers/TargetMenu/OATargetInfoViewController.mm b/Sources/Controllers/TargetMenu/OATargetInfoViewController.mm index 6e37de8c5c..82a44c727b 100644 --- a/Sources/Controllers/TargetMenu/OATargetInfoViewController.mm +++ b/Sources/Controllers/TargetMenu/OATargetInfoViewController.mm @@ -527,7 +527,7 @@ - (void)addOtherCards:(NSString *)imageTagContent mapillary:(NSString *)mapillar if (imageTagContent) urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"&osm_image=%@", imageTagContent]]; if (mapillaryTagContent) - urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"&osm_mapillary_key=%@", mapillaryTagContent]]; + urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"&mapillary=%@", mapillaryTagContent]]; NSInteger cardsCount = cards.count; NSURL *urlObj = [[NSURL alloc] initWithString:[[urlString stringByReplacingOccurrencesOfString:@" " withString:@"_"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]]; @@ -543,8 +543,8 @@ - (void)addOtherCards:(NSString *)imageTagContent mapillary:(NSString *)mapillar NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:safeCharsData options:NSJSONReadingAllowFragments error:&error]; if (jsonDict) { - NSArray *features = jsonDict[@"features"]; - if (features.count == 0) + NSArray *images = jsonDict[@"features"]; + if (images.count == 0) { dispatch_async(dispatch_get_main_queue(), ^{ [self onOtherCardsReady:newCards rowInfo:nearbyImagesRowInfo]; @@ -552,8 +552,8 @@ - (void)addOtherCards:(NSString *)imageTagContent mapillary:(NSString *)mapillar } else { - NSInteger __block count = features.count; - for (NSDictionary *dict in features) + NSInteger __block count = images.count; + for (NSDictionary *dict in images) { dispatch_async(dispatch_get_main_queue(), ^{ [self getCard:dict onComplete:^(OAAbstractCard *card) { diff --git a/Sources/Helpers/OAWikiImageHelper.h b/Sources/Helpers/OAWikiImageHelper.h index 2de766f380..5d28f58814 100644 --- a/Sources/Helpers/OAWikiImageHelper.h +++ b/Sources/Helpers/OAWikiImageHelper.h @@ -12,7 +12,7 @@ #define WIKIMEDIA_API_ENDPOINT @"https://commons.wikimedia.org/w/api.php" #define WIKIDATA_ACTION @"?action=wbgetclaims&property=P18&entity=" #define WIKIMEDIA_ACTION @"?action=query&list=categorymembers&cmtitle=" -#define CM_LIMIT @"&cmlimit=500" +#define CM_LIMIT @"&cmlimit=100" #define FORMAT_JSON @"&format=json" #define IMAGE_BASE_URL @"https://commons.wikimedia.org/wiki/Special:FilePath/" @@ -21,11 +21,10 @@ #define WIKIMEDIA_CATEGORY @"Category:" #define WIKIMEDIA_WIDTH @"?width=" -#define THUMB_SIZE 500 +#define THUMB_SIZE 480 -static NSString * const OSMAND_API_ENDPOINT = @"https://osmand.net/api/"; -static NSString * const OSMAND_API_WIKIDATA_ARTICLE_ACTION = @"wiki_place?article="; -static NSString * const OSMAND_API_WIKIDATA_CATEGORY_ACTION = @"wiki_place?category="; +static NSString * const OSMAND_API_ENDPOINT = @"https://osmand.net/api/wiki_place?"; +static const NSInteger DEPT_CAT_LIMIT = 1; static const BOOL USE_OSMAND_WIKI_API = YES; @class OARowInfo; diff --git a/Sources/Helpers/OAWikiImageHelper.mm b/Sources/Helpers/OAWikiImageHelper.mm index 5faf0a4afd..74434cf35d 100644 --- a/Sources/Helpers/OAWikiImageHelper.mm +++ b/Sources/Helpers/OAWikiImageHelper.mm @@ -51,13 +51,13 @@ - (void)sendNearbyWikiImagesRequest:(OARowInfo *)nearbyImagesRowInfo targetObj:( if ([targetObj isKindOfClass:OAPOI.class]) { OAPOI *poi = targetObj; - wikimediaTagContent = poi.values[@"wikimedia_commons"]; - wikidataTagContent = poi.values[@"wikidata"]; + wikimediaTagContent = poi.values[WIKIMEDIA_COMMONS_TAG]; + wikidataTagContent = poi.values[WIKIDATA_TAG]; } _wikidataCardsReady = NO; _wikimediaCardsReady = NO; - [self addWikimediaCards:wikimediaTagContent cards:cards rowInfo:nearbyImagesRowInfo]; - [self addWikidataCards:wikidataTagContent cards:cards rowInfo:nearbyImagesRowInfo]; + [self addWikimediaCards:wikimediaTagContent cards:cards]; + [self addWikidataCards:wikidataTagContent cards:cards]; } - (OAWikiImage *)getOsmandApiWikiImage:(NSString *)imageUrl @@ -91,17 +91,17 @@ - (OAWikiImage *)getWikiImage:(NSString *)imageFileName return [[OAWikiImage alloc] initWithWikiMediaTag:urlSafeFileName imageName:imageName imageStubUrl:imageStubUrl imageHiResUrl:imageHiResUrl]; } -- (void)addOsmandAPIWikidataImageListByCategory:(NSString *)categoryName +- (void)addOsmandAPIWikimediaImageListByCategory:(NSString *)categoryName cards:(NSMutableArray *)cards { - NSString *url = [NSString stringWithFormat:@"%@%@%@", OSMAND_API_ENDPOINT, OSMAND_API_WIKIDATA_CATEGORY_ACTION, categoryName]; + NSString *url = [NSString stringWithFormat:@"%@%@%@", OSMAND_API_ENDPOINT, @"category=", categoryName]; [self addOsmandAPIImageList:url cards:cards byCategory:YES]; } - (void)addOsmandAPIWikidataImageList:(NSString *)wikidataTagContent cards:(NSMutableArray *)cards { - NSString *url = [NSString stringWithFormat:@"%@%@%@", OSMAND_API_ENDPOINT, OSMAND_API_WIKIDATA_ARTICLE_ACTION, wikidataTagContent]; + NSString *url = [NSString stringWithFormat:@"%@%@%@", OSMAND_API_ENDPOINT, @"article=", wikidataTagContent]; [self addOsmandAPIImageList:url cards:cards byCategory:NO]; } @@ -155,7 +155,7 @@ - (void)addOsmandAPIImageList:(NSString *)url } -- (void)addWikidataCards:(NSString *)wikidataTagContent cards:(NSMutableArray *)cards rowInfo:(OARowInfo *)nearbyImagesRowInfo +- (void)addWikidataCards:(NSString *)wikidataTagContent cards:(NSMutableArray *)cards { if (wikidataTagContent) { @@ -215,7 +215,7 @@ - (void)addWikidataCards:(NSString *)wikidataTagContent cards:(NSMutableArray *)cards rowInfo:(OARowInfo *)nearbyImagesRowInfo +- (void)addWikimediaCards:(NSString *)wikiMediaTagContent cards:(NSMutableArray *)cards { if (wikiMediaTagContent && [wikiMediaTagContent hasPrefix:WIKIMEDIA_FILE]) { @@ -231,60 +231,88 @@ - (void)addWikimediaCards:(NSString *)wikiMediaTagContent cards:(NSMutableArray< } else if (wikiMediaTagContent && [wikiMediaTagContent hasPrefix:WIKIMEDIA_CATEGORY]) { + NSString *categoryName = [wikiMediaTagContent stringByReplacingOccurrencesOfString:WIKIMEDIA_CATEGORY withString:@""]; if (USE_OSMAND_WIKI_API) - { - NSString *categoryName = [wikiMediaTagContent stringByReplacingOccurrencesOfString:WIKIMEDIA_CATEGORY withString:@""]; - [self addOsmandAPIWikidataImageListByCategory:categoryName cards:cards]; - } + [self addOsmandAPIWikimediaImageListByCategory:categoryName cards:cards]; else + [self addWikimediaCardsFromCategory:categoryName cards:cards depth:0 prepared:YES]; + } + else + { + _wikimediaCardsReady = YES; + if (_wikidataCardsReady) + _addOtherImagesFunction(cards); + } +} + +- (void)addWikimediaCardsFromCategory:(NSString *)categoryName + cards:(NSMutableArray *)cards + depth:(NSInteger)depth + prepared:(BOOL)prepared +{ + __block BOOL ready = prepared; + NSString *url = [NSString stringWithFormat:@"%@%@%@%@%@%@", WIKIMEDIA_API_ENDPOINT, WIKIMEDIA_ACTION, WIKIMEDIA_CATEGORY, categoryName, CM_LIMIT, FORMAT_JSON]; + url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + NSURL *urlObj = [[NSURL alloc] initWithString:url]; + NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; + [[session dataTaskWithURL:urlObj completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + NSMutableArray *resultCards = [NSMutableArray array]; + if (((NSHTTPURLResponse *)response).statusCode == 200) { - NSString *url = [NSString stringWithFormat:@"%@%@%@%@%@", WIKIMEDIA_API_ENDPOINT, WIKIMEDIA_ACTION, wikiMediaTagContent, CM_LIMIT, FORMAT_JSON]; - url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; - NSURL *urlObj = [[NSURL alloc] initWithString:url]; - NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; - [[session dataTaskWithURL:urlObj completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { - NSMutableArray *resultCards = [NSMutableArray array]; - if (((NSHTTPURLResponse *)response).statusCode == 200) + if (data) + { + NSError *error; + NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; + NSDictionary *cms = jsonDict[@"query"][@"categorymembers"]; + if (!error && cms) { - if (data) + NSMutableArray *subCategories = [NSMutableArray array]; + for (NSDictionary *cm in cms) { - NSError *error; - NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; - NSDictionary *imagesDict = jsonDict[@"query"][@"categorymembers"]; - if (!error && imagesDict) + NSString *memberTitle = cm[@"title"]; + if (memberTitle) { - for (NSDictionary *imageDict in imagesDict) + if ([memberTitle hasPrefix:WIKIMEDIA_CATEGORY]) { - NSString *imageName = imageDict[@"title"]; - if (imageName) - { - OAWikiImageCard *card = [[OAWikiImageCard alloc] initWithWikiImage:[self getWikiImage:imageName] type:@"wikimedia-photo" wikimediaCategory:YES]; - if (card) - [resultCards addObject:card]; - } + ready = NO; + [subCategories addObject:memberTitle]; + } + else + { + OAWikiImageCard *card = [[OAWikiImageCard alloc] initWithWikiImage:[self getWikiImage:memberTitle] type:@"wikimedia-photo" wikimediaCategory:YES]; + if (card) + [resultCards addObject:card]; } } } + if (depth < DEPT_CAT_LIMIT) + { + for (NSInteger i = 0; i < subCategories.count; i ++) + { + NSString *subCategory = subCategories[i]; + [self addWikimediaCardsFromCategory:subCategory + cards:cards + depth:depth + 1 + prepared:i == subCategories.count - 1]; + } + } } - else - { - NSLog(@"Error retrieving Wikimedia photos: %@", error); - } - dispatch_async(dispatch_get_main_queue(), ^{ - [cards addObjectsFromArray:resultCards]; - _wikimediaCardsReady = YES; - if (_wikidataCardsReady) - _addOtherImagesFunction(cards); - }); - }] resume]; + } } - } - else - { - _wikimediaCardsReady = YES; - if (_wikidataCardsReady) - _addOtherImagesFunction(cards); - } + else + { + NSLog(@"Error retrieving Wikimedia photos: %@", error); + } + if (ready) + { + dispatch_async(dispatch_get_main_queue(), ^{ + [cards addObjectsFromArray:resultCards]; + _wikimediaCardsReady = YES; + if (_wikidataCardsReady) + _addOtherImagesFunction(cards); + }); + } + }] resume]; } @end