Skip to content

Commit

Permalink
fix #175
Browse files Browse the repository at this point in the history
messing up "...(1)" names etc

now json extractor will remove those numbers only when everything else fails
  • Loading branch information
TheLastGimbus committed Feb 19, 2023
1 parent 9e00365 commit b38efb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
3 changes: 3 additions & 0 deletions bin/gpth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ void main(List<String> arguments) async {
jsonExtractor,
exifExtractor,
if (args['guess-from-name']) guessExtractor,
// this is potentially *dangerous* - see:
// https://github.com/TheLastGimbus/GooglePhotosTakeoutHelper/issues/175
(f) => jsonExtractor(f, tryhard: true),
];

/// ##### Occasional Fix mode #####
Expand Down
19 changes: 13 additions & 6 deletions lib/date_extractors/json_extractor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'dart:io';
import 'package:path/path.dart' as p;

/// Finds corresponding json file with info and gets 'photoTakenTime' from it
Future<DateTime?> jsonExtractor(File file) async {
final jsonFile = await _jsonForFile(file);
Future<DateTime?> jsonExtractor(File file, {bool tryhard = false}) async {
final jsonFile = await _jsonForFile(file, tryhard: tryhard);
if (jsonFile == null) return null;
try {
final data = jsonDecode(await jsonFile.readAsString());
Expand All @@ -25,15 +25,22 @@ Future<DateTime?> jsonExtractor(File file) async {
}
}

Future<File?> _jsonForFile(File file) async {
Future<File?> _jsonForFile(File file, {required bool tryhard}) async {
final dir = Directory(p.dirname(file.path));
var name = p.basename(file.path);
// will try all methods to strip name to find json
for (final method in [
(s) => s, // none
// none
(String s) => s,
_shortenName,
_removeExtra,
_removeDigit, // most files with '(digit)' have jsons, so it's last
// use those two only with tryhard
// look at https://github.com/TheLastGimbus/GooglePhotosTakeoutHelper/issues/175
// thanks @denouche for reporting this!
// TODO: Maybe add tests for this some day
if (tryhard) ...[
_removeExtra,
_removeDigit, // most files with '(digit)' have jsons, so it's last
]
]) {
final jsonFile = File(p.join(dir.path, '${method(name)}.json'));
if (await jsonFile.exists()) return jsonFile;
Expand Down

0 comments on commit b38efb5

Please sign in to comment.