【Python】Google Photo と ローカルフォルダの並びを同じにする

以前、Mac の Automater の機能を使って、同じフォルダの中のファイル群に、1分ずつずらした「変更日」を設定しました。

www.dev-dev.net

これは、Google Photo のアルバムの中の「並び替え」を確実に行うため。

www.nanigoto.net

前回は、Google Photo にアップロードする前に、「変更日」を設定しましたが、

今回は、アップロード後に、ブラウザから日時を変更する手法で、

1つ1つ手作業だと面倒くさいので、Python + Selenium を使いました。

これで、アップロード前のローカルパソコンのフォルダ一覧の並びと同じにすることができます。


# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

from selenium.webdriver.chrome.options import Options

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.action_chains import ActionChains

import time
import glob
import math

WAIT_TIME = 60

# -----------------
google_acount_email = "【Google アカウントの ID】"
google_acount_pass = "【Google アカウントの パスワード】"
google_photo_url = "https://photos.google.com/album/【Google Photo のアルバムのURL】"
local_file_path = "/Volumes/【ローカルフォルダのパス】"
# -----------------

local_file_list = glob.glob(local_file_path + "/*")
local_file_list = sorted(local_file_list)

save_href_list = []
save_ele = None

print(local_file_list)

options = Options()
# options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
# options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

driver.set_window_position(0, 0)
driver.set_window_size(1000, 1000)

driver.get("https://www.google.com")

detail_open_flg = False

def get_hour(count):
    if count >= 60:
        h = math.floor(count / 60)
    else:
        h = 0

    s = "0" + str(h)

    return s[-2:]

def get_minute(count):
    if count >= 60:
        m = count % 60
    else:
        m = count

    s = "0" + str(m)

    return s[-2:]

def get_index(name):
    global local_file_list

    count = -1

    for filename in local_file_list:
        count += 1
        if name in filename:
            return count + 1

    return -1

def main():

    driver.find_element_by_link_text("ログイン").click()

    driver.find_element_by_id("identifierId").send_keys(google_acount_email)

    ele = WebDriverWait(driver, WAIT_TIME).until(
        EC.presence_of_element_located((By.ID, "identifierNext"))
    )

    driver.find_element_by_id("identifierNext").click()

    ele = WebDriverWait(driver, WAIT_TIME).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input'))
    )

    driver.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input').send_keys(google_acount_pass)

    ele = WebDriverWait(driver, WAIT_TIME).until(
        EC.presence_of_element_located((By.ID, "passwordNext"))
    )

    driver.execute_script("arguments[0].click();", ele)

    ele = WebDriverWait(driver, WAIT_TIME).until(
        EC.presence_of_element_located((By.XPATH, "//*/div/div/div[2]/div[2]/div[1]/a"))
    )

    driver.get(google_photo_url)

    ele = WebDriverWait(driver, WAIT_TIME).until(
        EC.presence_of_element_located((By.XPATH, "//*/div/div[1]/div/div[1]/div/textarea"))
    )

    load_base_tab()

def set_scroll():

    global save_ele
    global save_href_list

    start_flg = False

    eles = driver.find_elements_by_tag_name("a")
    print("len(eles) = " + str(len(eles)))
    for ele in eles:
        if save_ele is not None:
            if save_ele == ele:
                start_flg = True

            if start_flg == False:
                continue

        label = ele.get_attribute("aria-label")
        href = ele.get_attribute("href")
        if label is not None:
            if "写真 -" in label:
                actions = ActionChains(driver)
                actions.move_to_element(ele)
                actions.perform()
                print(label)
                print(href)
                if (href in save_href_list) is False:
                    save_href_list.append(href)

    if save_ele == ele:
        return

    save_ele = ele

    set_scroll()

def load_base_tab():
    global save_href_list

    set_scroll()

    for href in save_href_list:
        print(href)

        next_media(href)

    driver.get(google_photo_url)

def next_media(href):
    global detail_open_flg

    driver.get(href)

    if detail_open_flg is False:
        detail_open_flg = True

        actions = ActionChains(driver)

        actions.move_to_element(driver.find_element_by_tag_name("body")).perform()

        eles = driver.find_elements_by_tag_name("button")

        for ele in eles:
            label = ele.get_attribute("aria-label")
            if label is not None:
                if "情報を開く" in label:
                    ele.click()
                    break

    ele = WebDriverWait(driver, WAIT_TIME).until(
        EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'この写真の撮影場所を設定'))
    )

    eles = driver.find_elements_by_tag_name("div")

    file_index = -1

    for ele in eles:
        label = ele.get_attribute("aria-label")
        if label is not None:
            if "ファイル名" in label:
                filename = ele.text

                if filename == "":
                    print("filename again!")
                    driver.refresh()
                    next_media(href)
                    return

                print("filename = " + filename)
                file_index = get_index(filename)
                print("file_index = " + str(file_index))
                break

    for ele in eles:
        label = ele.get_attribute("aria-label")
        if label is not None:
            if "日付" in label:
                driver.execute_script("arguments[0].click();", ele)
                break

    ele = WebDriverWait(driver, WAIT_TIME).until(
        EC.presence_of_element_located((By.XPATH, '//*/div[2]/div/div[2]/span/div/div[1]/div[1]/div/div[1]/input'))
    )

    eles = driver.find_elements_by_tag_name("input")

    for ele in eles:
        label = ele.get_attribute("aria-label")
        if label is not None:
            if "年" in label:
                ele.send_keys("2000")

            if "月" in label:
                ele.send_keys("01")

            if "日" in label:
                ele.send_keys("01")

            if "時間" in label:
                h = get_hour(file_index)
                print("h = " + h)
                ele.send_keys(h)

            if "分" in label:
                m = get_minute(file_index)
                print("m = " + m)
                ele.send_keys(m)

    driver.find_element_by_xpath('/html/body/div[1]/div/div[2]/div/div[2]/div[3]/button[2]').send_keys(Keys.ENTER)

main()

返信を残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA