Order Photos on Timeline

Books
Purpose:

This program takes a directory of images and dates them with incrementally earlier times in a given year to ensure they appear in order on the Google Photos timeline.

Dependencies

Python (with os module), ExifTool

# REQUIRES EXIFTOOL INSTALLED ON MACHINE
import os

# These Varables will likely change
directory = "#DIRECTORY OF NUMBERED IMAGES#"
year = "#YEAR TO START DATING#"                 #The program will use this as the starting date for the photos.


files = os.listdir(directory)
files.sort()

# This retrieves the image's number in order to sort, and later date, the images correctly.
def getint(name):
   if year in str(name) and "COVER" not in str(name) and "BACK" not in str(name):
       num_start = str(name).find("+") + len("+")
       temp_num = name[num_start:]
       num_end = temp_num.find(".")
       num = (temp_num[:num_end])
   elif "COVER" in str(name):
       num = -100000
   elif "BACK" in str(name):
       num = 100000
   else:
       num = 0
   return int(num)

files = sorted(files, key=getint)

photo_num = 0
hour = 24
minute = 59
second = 58
base_date = str(year)+":01:01 "
print(base_date)

# Uses ExifTool to write a given date to a file_path's EXIF data.
def write_date (file_path, date):
   bashCommand = "Exiftool \"-datetimeoriginal=" + date + "\" \"-CreateDate=" + date + "\" \"-ModifyDate=" + date + "\" \"-filemodifydate=" + date + "\" -overwrite_original " + file_path
   print('\x1b[6;30;42m' + "CORRECTING DATE" + '\x1b[0m')
   os.system(bashCommand)

# Iterates through each file in the directory and dates them in order.
for file in files:
   try:
       if ".jpeg" in str(file):
           photo_num += 1
           minute -= 1
           if hour ==0:
               sys.exit()
           elif minute == 0:
               hour -= 1
               minute = 58
               date = base_date+str(hour)+":"+str(minute)+":"+str(second)
               file_path = directory + "/" + str(file)
               print("Changing "+ str(file) + "'s date to", date)
               write_date(file_path, date)
           else:
               date = base_date+str(hour)+":"+str(minute)+":"+str(second)
               file_path = directory + "/" + str(file)
               # print(str(file_path) + " - " + date)
               print("Changing "+ str(file) + "'s date to", date)
               write_date(file_path, date)
   except:
       print(file_path)
       print('\33[101m' + "*****ERROR*****" + '\x1b[0m')

# Prints the number of photos the program edited.
print('\x1b[6;30;42m' + str(photo_num) + " photos updated!" + '\x1b[0m')