TheLastGimbus/GooglePhotosTakeoutHelper

Exception when time data in exif has 24 as midnight instead of 00

cosandr opened this issue · 9 comments

Traceback

.\photos/2016-01-25 #2/IMG_20160126_004214.jpg
Error setting creation date from string:
time data '2016:01:26 24:48:30' does not match format '%Y:%m:%d %H:%M:%S'
Traceback (most recent call last):
  File ".\GooglePhotosTakeoutHelper\google_photos_takeout_helper\__main__.py", line 387, in <module>
    main()
  File ".\GooglePhotosTakeoutHelper\google_photos_takeout_helper\__main__.py", line 353, in main
    for_all_files_recursive(
  File ".\GooglePhotosTakeoutHelper\google_photos_takeout_helper\__main__.py", line 99, in for_all_files_recursive
    for_all_files_recursive(file, file_function, folder_function, filter_fun)
  File ".\GooglePhotosTakeoutHelper\google_photos_takeout_helper\__main__.py", line 102, in for_all_files_recursive
    file_function(dir, file)
  File ".\GooglePhotosTakeoutHelper\google_photos_takeout_helper\__main__.py", line 274, in fix_metadata
    set_creation_date_from_exif(file)
  File ".\GooglePhotosTakeoutHelper\google_photos_takeout_helper\__main__.py", line 241, in set_creation_date_from_exif
    set_creation_date_from_str(file, datetime_str)
  File ".\GooglePhotosTakeoutHelper\google_photos_takeout_helper\__main__.py", line 226, in set_creation_date_from_str
    _os.utime(file, (timestamp, timestamp))
UnboundLocalError: local variable 'timestamp' referenced before assignment

Proposed patch

diff --git a/google_photos_takeout_helper/__main__.py b/google_photos_takeout_helper/__main__.py
index 34efe73..d366560 100644
--- a/google_photos_takeout_helper/__main__.py
+++ b/google_photos_takeout_helper/__main__.py
@@ -220,10 +220,10 @@ def main():
                 str_datetime,
                 '%Y:%m:%d %H:%M:%S'
             ).timestamp()
+            _os.utime(file, (timestamp, timestamp))
         except Exception as e:
             print('Error setting creation date from string:')
             print(e)
-        _os.utime(file, (timestamp, timestamp))


     def set_creation_date_from_exif(file):

I'm not sure why the parsing doesn't work but after the above change at least the program doesn't crash. As expected, the error is still printed out.

.\photos/2016-01-25 #2/IMG_20160126_004214.jpg
Error setting creation date from string:
time data '2016:01:26 24:48:30' does not match format '%Y:%m:%d %H:%M:%S'
.\photos/2016-02-04/IMG_0082.PNG

I should note that the picture was copied to the output directory, even with the error.

It doesn't parse, because you have 24:48:30 as hour. 24:48. It should be 00:48

I don't really know whether to write some complicated parser that will replace those wrong hours with 00, or just put it in try so it doesn't crash...

The file name is correct though, it's 004214. The 48 minutes and 30 seconds seem to come from the JSON.

{
  "title": "IMG_20160126_004214.jpg",
  "description": "",
  "imageViews": "0",
  "creationTime": {
    "timestamp": "1453765709",
    "formatted": "Jan 25, 2016, 11:48:29 PM UTC"
  },
  "modificationTime": {
    "timestamp": "1600085152",
    "formatted": "Sep 14, 2020, 12:05:52 PM UTC"
  },
  "geoData": {
    "latitude": 0.0,
    "longitude": 0.0,
    "altitude": 0.0,
    "latitudeSpan": 0.0,
    "longitudeSpan": 0.0
  },
  "geoDataExif": {
    "latitude": 0.0,
    "longitude": 0.0,
    "altitude": 0.0,
    "latitudeSpan": 0.0,
    "longitudeSpan": 0.0
  },
  "photoTakenTime": {
    "timestamp": "1453765334",
    "formatted": "Jan 25, 2016, 11:42:14 PM UTC"
  },
  "googlePhotosOrigin": {
    "mobileUpload": {
      "deviceType": "ANDROID_PHONE"
    }
  }
}

My current time zone is UTC+2 so maybe something's going wrong with that?

OoOhhhh - so - maybe try to set the date from json if exif date is wrongly formatted?

This should be default behavior

First of all, thanks for creating this script. It is a huge help!

I get a similar error as well:

D:\Backups\GoogleFotosTakeoutLine301120\Takeout\GoogleFotos/1980-01-01/1988 Ferie 09.jpg
Error setting creation date from string:
time data '2007:12:28 14:31: 2' does not match format '%Y:%m:%d %H:%M:%S'
Traceback (most recent call last):
File "c:\users\emil\anaconda3\lib\runpy.py", line 193, in run_module_as_main
"main", mod_spec)
File "c:\users\emil\anaconda3\lib\runpy.py", line 85, in run_code
exec(code, run_globals)
File "C:\Users\Emil\anaconda3\Scripts\google-photos-takeout-helper.exe_main.py", line 7, in
File "c:\users\emil\anaconda3\lib\site-packages\google_photos_takeout_helper_main.py", line 356, in main
filter_fun=lambda f: (is_photo(f) or is_video(f))
File "c:\users\emil\anaconda3\lib\site-packages\google_photos_takeout_helper_main_.py", line 99, in for_all_files_recursive
for_all_files_recursive(file, file_function, folder_function, filter_fun)
File "c:\users\emil\anaconda3\lib\site-packages\google_photos_takeout_helper_main_.py", line 102, in for_all_files_recursive
file_function(dir, file)
File "c:\users\emil\anaconda3\lib\site-packages\google_photos_takeout_helper_main_.py", line 274, in fix_metadata
set_creation_date_from_exif(file)
File "c:\users\emil\anaconda3\lib\site-packages\google_photos_takeout_helper_main_.py", line 241, in set_creation_date_from_exif
set_creation_date_from_str(file, datetime_str)
File "c:\users\emil\anaconda3\lib\site-packages\google_photos_takeout_helper_main_.py", line 226, in set_creation_date_from_str
_os.utime(file, (timestamp, timestamp))
UnboundLocalError: local variable 'timestamp' referenced before assignment

I seem to get this error when the script tries to process photos that I have manually uploaded to Gphotos, rather than those uploaded by my phone automatically. I should mention that i use the --divide-to-dates tag.

Is it possible to include an ignore-errors flag that simply skips sorting photos with incorrect date strings, and instead puts these into a folder named 'unsorted' or something? I do not have specific suggestions as for how to code it, sorry

Nothing to add except this error is occurring for me as well.

albums/2011-09-04-05/IMG_0010.JPG
Error setting creation date from string:
time data '' does not match format '%Y:%m:%d %H:%M:%S'
Traceback (most recent call last):
  File "/home/rich/.platformio/penv/bin/google-photos-takeout-helper", line 8, in <module>
    sys.exit(main())
  File "/home/rich/.platformio/penv/lib/python3.8/site-packages/google_photos_takeout_helper/__main__.py", line 353, in main
    for_all_files_recursive(
  File "/home/rich/.platformio/penv/lib/python3.8/site-packages/google_photos_takeout_helper/__main__.py", line 99, in for_all_files_recursive
    for_all_files_recursive(file, file_function, folder_function, filter_fun)
  File "/home/rich/.platformio/penv/lib/python3.8/site-packages/google_photos_takeout_helper/__main__.py", line 102, in for_all_files_recursive
    file_function(dir, file)
  File "/home/rich/.platformio/penv/lib/python3.8/site-packages/google_photos_takeout_helper/__main__.py", line 274, in fix_metadata
    set_creation_date_from_exif(file)
  File "/home/rich/.platformio/penv/lib/python3.8/site-packages/google_photos_takeout_helper/__main__.py", line 241, in set_creation_date_from_exif
    set_creation_date_from_str(file, datetime_str)
  File "/home/rich/.platformio/penv/lib/python3.8/site-packages/google_photos_takeout_helper/__main__.py", line 226, in set_creation_date_from_str
    _os.utime(file, (timestamp, timestamp))
UnboundLocalError: local variable 'timestamp' referenced before assignment

Guys, please edit comments to wrap any logs inside code blocks, thanks

I don't have a way to fix it in the script, but you can delete or remove the file Google Photos/2011-12-31/20120101_055014_001.jpg and restart the script. I am running this against 187,000 photos and have had to go through the remove process for about 30 photos so far, restarting the script each time.

Guys, please edit comments to wrap any logs inside code blocks, thanks

@godishalagoutham, please

'2016: 6:29 18: 1:36'

Oh, this is marvelous 😍

I'm just waiting to see text-formatted timestamp - "Friday night, year two thousand and six"

Just rolled out new version, you all can update and see how it's going!