image querying from within the frontend
All checks were successful
Build and release debug APK / Build APK (pull_request) Successful in 7m40s
All checks were successful
Build and release debug APK / Build APK (pull_request) Successful in 7m40s
This commit is contained in:
60
frontend/lib/utils/load_landmark_image.dart
Normal file
60
frontend/lib/utils/load_landmark_image.dart
Normal file
@@ -0,0 +1,60 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:fuzzywuzzy/fuzzywuzzy.dart';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fuzzywuzzy/model/extracted_result.dart';
|
||||
|
||||
const String baseUrl = "https://en.wikipedia.org/w/api.php";
|
||||
final Dio dio = Dio();
|
||||
|
||||
Future<int?> bestPageMatch(String title) async {
|
||||
final response = await dio.get(baseUrl, queryParameters: {
|
||||
"action": "query",
|
||||
"format": "json",
|
||||
"list": "prefixsearch",
|
||||
"pssearch": title,
|
||||
});
|
||||
|
||||
final data = jsonDecode(response.toString());
|
||||
log(data.toString());
|
||||
final List<dynamic> results = data["query"]["prefixsearch"] ?? {};
|
||||
final Map<String, int> titlesAndIds = {
|
||||
for (var d in results) d["title"]: d["pageid"]
|
||||
};
|
||||
if (titlesAndIds.isEmpty) {
|
||||
log("No pages found for $title");
|
||||
return null;
|
||||
}
|
||||
|
||||
// after the empty check, we can safely assume that there is a best match
|
||||
final ExtractedResult<String> bestMatch = extractOne(
|
||||
query: title,
|
||||
choices: titlesAndIds.keys.toList(),
|
||||
cutoff: 70,
|
||||
);
|
||||
return titlesAndIds[bestMatch.choice];
|
||||
}
|
||||
|
||||
Future<String?> getImageUrl(int pageId) async {
|
||||
final response = await dio.get(baseUrl, queryParameters: {
|
||||
"action": "query",
|
||||
"format": "json",
|
||||
"prop": "pageimages",
|
||||
"pageids": pageId,
|
||||
"pithumbsize": 500,
|
||||
});
|
||||
|
||||
final data = jsonDecode(response.toString());
|
||||
final pageData = data["query"]["pages"][pageId.toString()];
|
||||
return pageData["thumbnail"]?["source"];
|
||||
}
|
||||
|
||||
Future<String?> getImageUrlFromName(String title) async {
|
||||
int? pageId = await bestPageMatch(title);
|
||||
if (pageId == null) {
|
||||
return null;
|
||||
}
|
||||
return await getImageUrl(pageId);
|
||||
}
|
Reference in New Issue
Block a user