jardin/jardin.py

154 lines
4.6 KiB
Python
Raw Permalink Normal View History

2023-10-19 20:00:07 +00:00
#!/usr/bin/python3
2023-10-21 14:09:23 +00:00
import re
import os
import sys
2023-10-20 11:52:58 +00:00
2023-10-20 17:32:23 +00:00
import bs4
2023-10-19 20:00:07 +00:00
from selenium import webdriver
2023-10-20 11:25:19 +00:00
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
2023-10-20 17:32:23 +00:00
from selenium.webdriver.common.action_chains import ActionChains
2023-10-19 20:00:07 +00:00
def initialize_webdriver():
return webdriver.Firefox()
2023-10-19 20:34:17 +00:00
def navigate_to_filegarden(browser):
2023-10-19 20:53:19 +00:00
browser.get("https://filegarden.com/login")
2023-10-19 20:34:17 +00:00
2023-10-20 11:32:48 +00:00
def input_email(browser, email):
2023-10-20 11:25:19 +00:00
emailInput = browser.find_element(By.ID, "email")
emailInput.send_keys(email)
emailInput.send_keys(Keys.RETURN)
2023-10-20 11:43:00 +00:00
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)
2023-10-20 11:25:19 +00:00
2023-10-20 11:32:48 +00:00
def login_with_password(browser, email, password):
input_email(browser, email)
2023-10-20 11:43:00 +00:00
click_password_button(browser)
input_password(browser, password)
2023-10-20 11:32:48 +00:00
2023-10-20 17:32:23 +00:00
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
2023-10-20 17:38:21 +00:00
def get_local_filenames(directory):
return os.listdir(directory)
2023-10-20 19:03:39 +00:00
def get_missing_filenames(localFiles, gardenFiles):
2023-10-21 14:09:23 +00:00
missingFiles = []
2023-10-20 19:03:39 +00:00
for localFile in localFiles:
if localFile not in gardenFiles:
2023-10-21 14:09:23 +00:00
missingFiles.append(localFile)
2023-10-20 19:03:39 +00:00
2023-10-21 14:09:23 +00:00
return missingFiles
2023-11-20 20:51:39 +00:00
def create_file_input(browser, userId):
javascript = """const fileInput = document.createElement("input");
fileInput.id = "upload";
fileInput.type = "file";
fileInput.multiple = true;
fileInput.addEventListener("change", () => {
for (const file of fileInput.files) {
2023-11-20 20:51:39 +00:00
const xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.responseType = "text";
xhr.open("POST", "/users/""" + userId + """/pipe", true);
const data = {
parent: null,
name: file.name
};
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.setRequestHeader("X-Data", encodeURI(JSON.stringify(data)));
xhr.send(file);
/*
Miro.request(
"POST",
"/users/""" + userId + """/pipe",
{"Content-Type": "application/octet-stream"},
file,
function(xhr) {
console.log("Started uploading " + file.name + "...");
xhr.addEventListener("readystatechange", function() {
if (xhr.readyState == XMLHttpRequest.OPENED) {
const data = {
parent: null,
name: file.name
};
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.setRequestHeader("X-Data", encodeURI(JSON.stringify(data)));
}
});
},
true)
.then(Miro.response(
function(xhr) {
console.log("Uploaded " + file.name);
}),
function(xhr, error) {
console.log("Failed to upload " + file.name + " due to error:");
console.log(error);
});
*/
}
fileInput.value = null;
});
document.body.appendChild(fileInput);
"""
browser.execute_script(javascript)
2023-10-21 14:09:23 +00:00
def get_user_id(browser):
url = browser.current_url
2023-10-21 14:09:23 +00:00
userId = re.search("[0-9a-f]{24}", url)[0]
return userId
def upload_files(browser, directory, missingFiles):
2023-10-21 14:09:23 +00:00
userId = get_user_id(browser)
2023-11-20 20:51:39 +00:00
create_file_input(browser, userId)
uploadInput = browser.find_element(By.ID, "upload")
2023-10-21 14:09:23 +00:00
for file in missingFiles:
uploadInput.send_keys(directory + file)
2023-10-20 19:03:39 +00:00
def main(email, password, directory):
2023-10-20 11:52:58 +00:00
browser = initialize_webdriver()
navigate_to_filegarden(browser)
login_with_password(browser, email, password)
2023-10-20 17:32:23 +00:00
click_go_to_your_garden(browser)
garden_filenames = get_garden_filenames(browser)
2023-10-20 19:07:23 +00:00
local_filenames = get_local_filenames(directory)
2023-10-21 14:09:23 +00:00
missing_files = get_missing_filenames(local_filenames, garden_filenames)
upload_files(browser, directory, missing_files)
2023-10-20 17:32:23 +00:00
#browser.close()
2023-10-19 10:31:24 +00:00
if __name__ == "__main__":
main(sys.argv[1], sys.argv[2], sys.argv[3])