TheLastGimbus / GooglePhotosTakeoutHelper

Script that organizes the Google Takeout archive into one big chronological folder
https://aur.archlinux.org/packages/gpth-bin
Apache License 2.0
3.96k stars 196 forks source link

Google Photos Takeout now adds a suffix to json file (and not properly either) #353

Open AalianKhan opened 4 days ago

AalianKhan commented 4 days ago

Hello, Thanks a lot for this tool but unfortunately it is not working correctly for me. I noticed either other people's json file was just the filename with .json at the end.

But I noticed mine have a suffix at the end of each json file. Most of my files have this extension .supplemental-metadata.json after the file name. but with some of the longer files, the extensions are cut off at the end such as PXL_20240817_202602411.mp4 has the json file named PXL_20240817_202602411.mp4.supplemental-metada.json. and there are many others like that with .supplemental-me, .supplementa, etc

Guessing works with some but fails terribly with others. I have some that are dated to 1868 and 2068 😂

AalianKhan commented 4 days ago

Added this to json_extractor.dart and it works now (FYI AI generated)

Future<File?> _jsonForFile(File file, {required bool tryhard}) async {
  final dir = Directory(p.dirname(file.path));
  var name = p.basename(file.path);

  // Use methods to generate potential JSON file names
  for (final method in [
    (String s) => s,
    _shortenName,
    _bracketSwap,
    _removeExtra,
    if (tryhard) ...[
      _removeExtraRegex,
      _removeDigit,
    ]
  ]) {
    final baseJsonFile = File(p.join(dir.path, '${method(name)}.json'));
    if (await baseJsonFile.exists()) return baseJsonFile;

    // Check for JSON file with suffix and truncated suffix variations
    final supplementalJsonFile =
        await _matchSupplementalSuffix(dir, method(name));
    if (supplementalJsonFile != null) return supplementalJsonFile;
  }
  return null;
}

/// Attempts to find a JSON file with `.supplemental-metadata.json` suffix or a truncated version
Future<File?> _matchSupplementalSuffix(Directory dir, String baseName) async {
  const suffix = '.supplemental-metadata.json';
  const maxLength = 51;

  // Try with full suffix
  final fullFile = File(p.join(dir.path, '$baseName$suffix'));
  if (await fullFile.exists()) return fullFile;

  // Try truncated suffixes if file length exceeds max length
  for (int i = suffix.length; i > 0; i--) {
    final truncatedSuffix = suffix.substring(0, i);
    final truncatedFile =
        File(p.join(dir.path, '$baseName$truncatedSuffix.json'));
    if (await truncatedFile.exists()) return truncatedFile;
  }
  return null;
}