Skip to content

Instantly share code, notes, and snippets.

@josemariagarcia95
Last active November 8, 2021 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josemariagarcia95/d28f6ad5e7b9128d0cb370329e8d5807 to your computer and use it in GitHub Desktop.
Save josemariagarcia95/d28f6ad5e7b9128d0cb370329e8d5807 to your computer and use it in GitHub Desktop.
Selenium script with Python to update photos with no date in Amazon Photos
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# 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()
# Just to measure the time and number of photos updated
start = time.time()
n_photos = 0
try:
chrome_driver = webdriver.Chrome()
# Open the amazon photos login page
chrome_driver.get(
"https://www.amazon.es/photos?sf=1/ref=s9_acss_bw_h1_EEEESSS_bn_w?ref_=BN_ES_C_U_E_M_CD_CG_417_ADAPSI&pf_rd_m=A1AT7YVPFBWXBL&pf_rd_s=merchandised-search-1&pf_rd_r=CKMHBDSC7YCPSMXR24KE&pf_rd_t=101&pf_rd_p=d284dade-6826-459b-9042-bf7f3d7341d1&pf_rd_i=12364776031"
)
# Set a longer implicit wait in order to make selenium wait at least five seconds for loading things
chrome_driver.implicitly_wait(5)
#Log in using your credentials
name_field = chrome_driver.find_element_by_id("ap_email")
password_field = chrome_driver.find_element_by_id("ap_password")
name_field.clear()
name_field.send_keys("**********************")
password_field.clear()
password_field.send_keys("************************")
chrome_driver.find_element_by_id("signInSubmit").click()
header_inline = chrome_driver.find_element_by_id("primary-header-inline")
header = chrome_driver.find_element_by_id("primary-header")
# Remove the upper bar, but it is not really necessary, it was just a test using execute_script
chrome_driver.execute_script(
"var a = arguments[0]; var b = arguments[1]; a.parentElement.removeChild(a); b.parentElement.removeChild(b)", header, header_inline)
# Spread "Year" drop-drown list to make the "Undated" category visible
for see_more_button in chrome_driver.find_elements_by_css_selector("button.show-more-button"):
chrome_driver.execute_script("arguments[0].click();", see_more_button)
# Click the "Undated" button (it has to be found like this since there is no id and the class name is not valid, '1000,' and it's not
# detected by others methods like find_element_by_class_name or find_element_by_css_selector)
more_photos_button = chrome_driver.find_element_by_xpath(
"//button[@class='1000']")
chrome_driver.execute_script("arguments[0].click();", more_photos_button)
# Loop through the images in the grid
for image_mosaic in chrome_driver.find_elements_by_class_name("mosaic-item"):
time.sleep(1)
# Open an image
chrome_driver.find_element_by_xpath("//a[@class='node-link']").click()
# Update its date
date_update()
n_photos += 1
# Go back to the grid
chrome_driver.find_element_by_xpath("//a[@class='back']").click()
except Exception as e:
print(e)
finally:
print("--- %s seconds ---" % (time.time() - start))
print("--- %s fotos ---" % n_photos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment