Update helper scripts
This commit is contained in:
parent
5065002bce
commit
dce9138640
@ -4,6 +4,8 @@ import 'dart:io';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:image/image.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:wonders/logic/data/collectible_data.dart';
|
||||
import 'package:wonders/logic/data/highlight_data.dart';
|
||||
import 'package:wonders/logic/data/wonders_data/chichen_itza_data.dart';
|
||||
import 'package:wonders/logic/data/wonders_data/christ_redeemer_data.dart';
|
||||
import 'package:wonders/logic/data/wonders_data/great_wall_data.dart';
|
||||
@ -12,9 +14,8 @@ import 'package:wonders/logic/data/wonders_data/petra_data.dart';
|
||||
import 'package:wonders/logic/data/wonders_data/pyramids_giza_data.dart';
|
||||
import 'package:wonders/logic/data/wonders_data/taj_mahal_data.dart';
|
||||
|
||||
import '../common_libs.dart';
|
||||
import '../logic/data/collectible_data.dart';
|
||||
import '../logic/data/wonders_data/colosseum_data.dart';
|
||||
import 'package:wonders/common_libs.dart';
|
||||
import 'package:wonders/logic/data/wonders_data/colosseum_data.dart';
|
||||
|
||||
class ArtifactDownloadHelper extends StatefulWidget {
|
||||
const ArtifactDownloadHelper({super.key});
|
||||
@ -54,56 +55,23 @@ class _ArtifactDownloadHelperState extends State<ArtifactDownloadHelper> {
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> downloadImage(String id, String url) async {
|
||||
//final sizes = [400, 800, 1600, 3000];
|
||||
debugPrint('Downloading $url to $imagesDir');
|
||||
final imgResponse = await get(Uri.parse(url));
|
||||
// If the image is less than a KB, it's probably a 404 image.
|
||||
if (imgResponse.bodyBytes.lengthInBytes < 2000) {
|
||||
return false;
|
||||
}
|
||||
File file = File('$imagesDir/$id.jpg');
|
||||
file.writeAsBytesSync(imgResponse.bodyBytes);
|
||||
print('img saved @ ${file.path}');
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> downloadImageAndJson(String id) async {
|
||||
File imgFile = File('$imagesDir/$id.jpg');
|
||||
if (imgFile.existsSync()) {
|
||||
print('Skipping $id');
|
||||
await resizeImage(id, 600);
|
||||
return;
|
||||
}
|
||||
Uri uri = Uri.parse('https://collectionapi.metmuseum.org/public/collection/v1/objects/$id');
|
||||
print('Downloading $id');
|
||||
final response = await http.get(uri);
|
||||
Map json = jsonDecode(response.body) as Map;
|
||||
if (!json.containsKey('primaryImage') || json['primaryImage'].isEmpty) {
|
||||
print('Missing $id');
|
||||
missingIds.add(id);
|
||||
return;
|
||||
}
|
||||
final url = json['primaryImage'] as String;
|
||||
//bool isPublicDomain = json['isPublicDomain'] as bool;
|
||||
final downloadSuccess = await downloadImage(id, url);
|
||||
if (downloadSuccess) {
|
||||
File file = File('$imagesDir/$id.json');
|
||||
file.writeAsStringSync(response.body);
|
||||
print('json saved @ ${file.path}');
|
||||
} else {
|
||||
print('Missing $id');
|
||||
missingIds.add(id);
|
||||
}
|
||||
}
|
||||
|
||||
void downloadArtifacts() async {
|
||||
/// Download collectibles
|
||||
// for (var c in collectiblesData) {
|
||||
// downloadImageAndJson(c.artifactId);
|
||||
// }
|
||||
missingIds.clear();
|
||||
|
||||
/// Download collectibles
|
||||
for (var c in collectiblesData) {
|
||||
if (await downloadImageAndJson(c.artifactId) == false) {
|
||||
missingIds.add(c.artifactId);
|
||||
}
|
||||
}
|
||||
|
||||
/// Download Highights
|
||||
for (var h in HighlightData.all) {
|
||||
if (await downloadImageAndJson(h.artifactId) == false) {
|
||||
missingIds.add(h.artifactId);
|
||||
}
|
||||
}
|
||||
|
||||
/// Download search artifacts
|
||||
final searchData = ChichenItzaData().searchData +
|
||||
ChristRedeemerData().searchData +
|
||||
@ -113,25 +81,94 @@ class _ArtifactDownloadHelperState extends State<ArtifactDownloadHelper> {
|
||||
PetraData().searchData +
|
||||
PyramidsGizaData().searchData +
|
||||
TajMahalData().searchData;
|
||||
for (var a in searchData) {
|
||||
await downloadImageAndJson(a.id.toString());
|
||||
print('${searchData.indexOf(a) + 1}/${searchData.length}');
|
||||
}
|
||||
print('Missing IDs: $missingIds');
|
||||
|
||||
// for (var a in searchData) {
|
||||
// final id = a.id.toString();
|
||||
// if (await downloadImageAndJson(id) == false) {
|
||||
// missingIds.add(id);
|
||||
// }
|
||||
// final index = searchData.indexOf(a) + 1;
|
||||
// if (index % 100 == 0) {
|
||||
// debugPrint('$index/${searchData.length}');
|
||||
// }
|
||||
// }
|
||||
debugPrint('Download complete :) Missing IDs: $missingIds');
|
||||
}
|
||||
|
||||
Future<void> resizeImage(String id, int size) async {
|
||||
final resizedFile = File('$imagesDir/${id}_$size.jpg');
|
||||
Future<bool> downloadImageAndJson(String id) async {
|
||||
File jsonFile = File('$imagesDir/$id.json');
|
||||
late Map json;
|
||||
if (jsonFile.existsSync()) {
|
||||
json = jsonDecode(jsonFile.readAsStringSync()) as Map;
|
||||
} else {
|
||||
debugPrint('Downloading $id');
|
||||
// Fetch JSON for id
|
||||
Uri uri = Uri.parse('https://collectionapi.metmuseum.org/public/collection/v1/objects/$id');
|
||||
final response = await http.get(uri);
|
||||
json = jsonDecode(response.body) as Map;
|
||||
}
|
||||
|
||||
// Check if primaryImage field is valid
|
||||
if (!json.containsKey('primaryImage') || json['primaryImage'].isEmpty) {
|
||||
return false;
|
||||
}
|
||||
// Download image
|
||||
final url = json['primaryImage'] as String;
|
||||
//bool isPublicDomain = json['isPublicDomain'] as bool;
|
||||
File imgFile = File('$imagesDir/$id.jpg');
|
||||
// If image does not already exist, download it
|
||||
if (!imgFile.existsSync()) {
|
||||
await downloadImage(id, url);
|
||||
if (!imgFile.existsSync()) return false;
|
||||
}
|
||||
// Try to resize image
|
||||
if (await resizeImage(id, [600, 2000]) == false) {
|
||||
debugPrint('Failed to resize $id');
|
||||
imgFile.deleteSync();
|
||||
return false;
|
||||
}
|
||||
// Write JSON to file
|
||||
if (!jsonFile.existsSync()) {
|
||||
jsonFile.writeAsStringSync(jsonEncode(json));
|
||||
debugPrint('json saved @ ${jsonFile.path}');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<bool> downloadImage(String id, String url) async {
|
||||
//final sizes = [400, 800, 1600, 3000];
|
||||
debugPrint('Downloading $url to $imagesDir');
|
||||
final imgResponse = await get(Uri.parse(url));
|
||||
// If the image is less than a KB, it's probably a 404 image.
|
||||
if (imgResponse.bodyBytes.lengthInBytes < 2000) {
|
||||
return false;
|
||||
}
|
||||
File file = File('$imagesDir/$id.jpg');
|
||||
file.writeAsBytesSync(imgResponse.bodyBytes);
|
||||
debugPrint('img saved @ ${file.path}');
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<bool> resizeImage(String id, List<int> sizes) async {
|
||||
final srcFile = File('$imagesDir/$id.jpg');
|
||||
print('Resizing $id...');
|
||||
if (resizedFile.existsSync() || !srcFile.existsSync()) return;
|
||||
//debugPrint('Resizing $id...');
|
||||
try {
|
||||
final img = decodeJpg(srcFile.readAsBytesSync());
|
||||
if (img != null) {
|
||||
// Write various sizes to disk
|
||||
for (var size in sizes) {
|
||||
final resizedFile = File('$imagesDir/${id}_$size.jpg');
|
||||
if (await resizedFile.exists()) continue;
|
||||
final resizedImg = copyResize(img, width: size);
|
||||
resizedFile.writeAsBytesSync(encodeJpg(resizedImg));
|
||||
print('Resized $id');
|
||||
} else {
|
||||
print('Failed to resize $id');
|
||||
await resizedFile.writeAsBytes(encodeJpg(resizedImg, quality: 90));
|
||||
debugPrint('Resized ${id}_$size');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('Failed to resize $id');
|
||||
debugPrint(e.toString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -164,12 +164,12 @@ class _ArtifactSearchHelperState extends State<ArtifactSearchHelper> {
|
||||
if (year < minYear || year > maxYear) return _logError(id, 'year is out of range');
|
||||
|
||||
String? imageUrlSmall = json['primaryImageSmall'];
|
||||
if (imageUrlSmall == null) return _logError(id, 'no small image url');
|
||||
if (!imageUrlSmall.startsWith(SearchData.baseImagePath)) {
|
||||
return _logError(id, 'unexpected image uri: "$imageUrlSmall"');
|
||||
}
|
||||
String imagePath = imageUrlSmall.substring(SearchData.baseImagePath.length);
|
||||
imagePath = imagePath.replaceFirst('/web-large/', '/mobile-large/');
|
||||
if (imageUrlSmall == null || imageUrlSmall.isEmpty) return _logError(id, 'no small image url');
|
||||
// if (!imageUrlSmall.startsWith(SearchData.baseImagePath)) {
|
||||
// return _logError(id, 'unexpected image uri: "$imageUrlSmall"');
|
||||
// }
|
||||
// String imageUrl = imageUrlSmall.substring(SearchData.baseImagePath.length);
|
||||
// imageUrl = imageUrl.replaceFirst('/web-large/', '/mobile-large/');
|
||||
|
||||
double? aspectRatio = 0;
|
||||
if (checkImages) aspectRatio = await _getAspectRatio(imageUrlSmall);
|
||||
@ -180,7 +180,6 @@ class _ArtifactSearchHelperState extends State<ArtifactSearchHelper> {
|
||||
id,
|
||||
_escape(json['title']),
|
||||
_getKeywords(json),
|
||||
imagePath,
|
||||
aspectRatio,
|
||||
);
|
||||
|
||||
@ -229,12 +228,22 @@ class _ArtifactSearchHelperState extends State<ArtifactSearchHelper> {
|
||||
|
||||
String suggestions = _getSuggestions(entries);
|
||||
|
||||
const fileNames = {
|
||||
WonderType.chichenItza: 'chichen_itza',
|
||||
WonderType.christRedeemer: 'christ_redeemer',
|
||||
WonderType.colosseum: 'colosseum',
|
||||
WonderType.greatWall: 'great_wall',
|
||||
WonderType.machuPicchu: 'machu_picchu',
|
||||
WonderType.petra: 'petra',
|
||||
WonderType.pyramidsGiza: 'pyramids_giza',
|
||||
WonderType.tajMahal: 'taj_mahal',
|
||||
};
|
||||
Directory dir = await getApplicationDocumentsDirectory();
|
||||
String type = wonder!.type.toString().split('.').last;
|
||||
String path = '${dir.path}/$type.dart';
|
||||
String name = '${fileNames[wonder!.type]}_search_data.dart';
|
||||
String path = '${dir.path}/$name';
|
||||
File file = File(path);
|
||||
await file.writeAsString('$suggestions\n\n$output');
|
||||
_log('- Wrote file: $type.dart');
|
||||
_log('- Wrote file: $name');
|
||||
debugPrint(path);
|
||||
_nextWonder();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user