Skip to content

Instantly share code, notes, and snippets.

@josemariagarcia95
Created April 5, 2020 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josemariagarcia95/1daf128e3e024ca57f66239cbd3ac958 to your computer and use it in GitHub Desktop.
Save josemariagarcia95/1daf128e3e024ca57f66239cbd3ac958 to your computer and use it in GitHub Desktop.
# Function to update a picture date
def date_update():
# Open the side bar with the info, including the name file and the date the photo was made
chrome_driver.find_element_by_xpath("//button[@class='info']").click()
time.sleep(2)
# Grab the image name
image_name = chrome_driver.find_element_by_xpath("//div[@class='detail-item fileInfo']//span[@class='label']").text
print(image_name)
# Start the date parsing
image_date = ""
# If the file name is something like "IMG-20200510-21045.jpg", "IMG_20150228_151447.png" or "VID-20200402-100512.mp4"
if image_name[0:3] == "IMG" or image_name[0:3] == "VID":
if image_name[3] == "-" or image_name[3] == "_":
# Split the name by "-" or "_", grab the second item (the "20200510" part) and parse it into a date
image_date = datetime.strptime(image_name.split("-")[1], "%Y%m%d")
# If the file name is something like "Whatsapp Image 2014-11-10.jpg"
elif image_name[0:3] == "Wha":
image_date = datetime.strptime(image_name.split(' ')[2], "%Y-%m-%d")
# If the file name is something like "Screenshot_20200401-121523.jpg"
elif "Screen" in image_name:
image_date = datetime.strptime(image_name.split('_')[1].split('-')[0], "%Y%m%d")
# A default date to manually classify the pictures in the Amazon Photos web later
elif image_date == "":
image_date = datetime.strptime("31-12-1999", "%d-%m-%Y")
if image_date != "":
#Click in the Edit date button and fill in the different input fields, one per date part (day, month, year)
edit_date_button = chrome_driver.find_element_by_xpath("//button[@class='edit-btn']")
edit_date_button.click()
year_input = chrome_driver.find_element_by_xpath("//input[@name='year']")
year_input.clear()
year_input.send_keys(image_date.year)
month_input = chrome_driver.find_element_by_xpath("//input[@name='month']")
month_input.clear()
month_input.send_keys(image_date.month)
day_input = chrome_driver.find_element_by_xpath("//input[@name='day']")
day_input.clear()
day_input.send_keys(image_date.day)
print(image_date)
# IMPORTANT SLEEP. Since the web reads the date from the label on top of the input fields, if we fill in a date but we don't
# wait for that label to update, submitting the date would save the current date as the date the photo was taken
time.sleep(1)
chrome_driver.find_element_by_xpath("//button[@type='submit']").click()
# Wait until the success toast appears, no changes will be made if we leave the page earlier
element = WebDriverWait(chrome_driver, 10).until(
EC.presence_of_element_located((By.NAME, "EditDateToastSuccess"))
)
# Close the detail page
chrome_driver.find_element_by_xpath("//button[@class='close-details']").click()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment