Skip to content

Commit

Permalink
Merge pull request #3651 from osmandapp/fix_wikimedia_photos_3
Browse files Browse the repository at this point in the history
sync nearby photos code with android
  • Loading branch information
vshcherb committed Apr 30, 2024
2 parents e1a02ec + 70b58f3 commit d8b50d7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 61 deletions.
10 changes: 5 additions & 5 deletions Sources/Controllers/TargetMenu/OATargetInfoViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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]]];
Expand All @@ -543,17 +543,17 @@ - (void)addOtherCards:(NSString *)imageTagContent mapillary:(NSString *)mapillar
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:safeCharsData options:NSJSONReadingAllowFragments error:&error];
if (jsonDict)
{
NSArray<NSDictionary *> *features = jsonDict[@"features"];
if (features.count == 0)
NSArray<NSDictionary *> *images = jsonDict[@"features"];
if (images.count == 0)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self onOtherCardsReady:newCards rowInfo:nearbyImagesRowInfo];
});
}
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) {
Expand Down
9 changes: 4 additions & 5 deletions Sources/Helpers/OAWikiImageHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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/"

Expand All @@ -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;
Expand Down
130 changes: 79 additions & 51 deletions Sources/Helpers/OAWikiImageHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<OAAbstractCard *> *)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<OAAbstractCard *> *)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];
}

Expand Down Expand Up @@ -155,7 +155,7 @@ - (void)addOsmandAPIImageList:(NSString *)url
}


- (void)addWikidataCards:(NSString *)wikidataTagContent cards:(NSMutableArray<OAAbstractCard *> *)cards rowInfo:(OARowInfo *)nearbyImagesRowInfo
- (void)addWikidataCards:(NSString *)wikidataTagContent cards:(NSMutableArray<OAAbstractCard *> *)cards
{
if (wikidataTagContent)
{
Expand Down Expand Up @@ -215,7 +215,7 @@ - (void)addWikidataCards:(NSString *)wikidataTagContent cards:(NSMutableArray<OA
}
}

- (void)addWikimediaCards:(NSString *)wikiMediaTagContent cards:(NSMutableArray<OAAbstractCard *> *)cards rowInfo:(OARowInfo *)nearbyImagesRowInfo
- (void)addWikimediaCards:(NSString *)wikiMediaTagContent cards:(NSMutableArray<OAAbstractCard *> *)cards
{
if (wikiMediaTagContent && [wikiMediaTagContent hasPrefix:WIKIMEDIA_FILE])
{
Expand All @@ -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<OAAbstractCard *> *)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<OAAbstractCard *> *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<OAAbstractCard *> *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<NSString *> *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

0 comments on commit d8b50d7

Please sign in to comment.