#!/usr/bin/python3 import re import os import sys import bs4 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.action_chains import ActionChains def initialize_webdriver(): return webdriver.Firefox() def navigate_to_filegarden(browser): browser.get("https://filegarden.com/login") def input_email(browser, email): emailInput = browser.find_element(By.ID, "email") emailInput.send_keys(email) emailInput.send_keys(Keys.RETURN) def click_password_button(browser): passwordButton = browser.find_element(By.XPATH, "/html/body/div[2]/div[3]/div[1]/form/div[1]/span/button[3]") passwordButton.click() def input_password(browser, password): passwordInput = browser.find_element(By.ID, "password") passwordInput.send_keys(password) passwordInput.send_keys(Keys.RETURN) def login_with_password(browser, email, password): input_email(browser, email) click_password_button(browser) input_password(browser, password) def click_go_to_your_garden(browser): browser.implicitly_wait(100) goToYourGardenButton = browser.find_element(By.LINK_TEXT, "GO TO YOUR GARDEN") goToYourGardenButton.click() def get_garden_filenames(browser): titles = [] soup = bs4.BeautifulSoup(browser.page_source, "lxml") for item in soup.find_all(attrs={"class": "name"}): titles.append(item.text) return titles def get_local_filenames(directory): return os.listdir(directory) def get_missing_filenames(localFiles, gardenFiles): missingFiles = [] for localFile in localFiles: if localFile not in gardenFiles: missingFiles.append(localFile) return missingFiles def create_file_input(browser): javascript = """const fileInput = document.createElement("input"); fileInput.id = "upload"; fileInput.type = "file"; fileInput.multiple = true; fileInput.addEventListener("change", () => { for (const file of fileInput.files) { console.log(file); // Debug // Miro.request(file); // What I'll probably actually use (or wrapper method) //addFile(file); // Original (token not accessible from minified code) } fileInput.value = null; }); document.body.appendChild(fileInput); """ browser.execute_script(javascript) def get_user_id(browser): url = browser.current_url userId = re.search("[0-9a-f]{24}", url)[0] return userId def upload_files(browser, directory, missingFiles): create_file_input(browser) uploadInput = browser.find_element(By.ID, "upload") userId = get_user_id(browser) for file in missingFiles: uploadInput.send_keys(directory + file) def main(email, password, directory): browser = initialize_webdriver() navigate_to_filegarden(browser) login_with_password(browser, email, password) click_go_to_your_garden(browser) garden_filenames = get_garden_filenames(browser) local_filenames = get_local_filenames(directory) missing_files = get_missing_filenames(local_filenames, garden_filenames) upload_files(browser, directory, missing_files) #browser.close() if __name__ == "__main__": main(sys.argv[1], sys.argv[2], sys.argv[3])