-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuplookupcsv.py
65 lines (52 loc) · 1.9 KB
/
puplookupcsv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import time
import urllib.parse
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
# Configuration
download_dir = "C:\\vPinball\\PinUPSystem"
url = "https://virtual-pinball-spreadsheet.web.app/csvexport"
os.makedirs(download_dir, exist_ok=True)
# Backup existing puplookup.csv if it exists
puplookup_path = os.path.join(download_dir, "puplookup.csv")
backup_path = os.path.join(download_dir, "puplookup.bak")
if os.path.exists(puplookup_path):
if os.path.exists(backup_path):
os.remove(backup_path)
os.rename(puplookup_path, backup_path)
# Set up Selenium with Chrome in headless mode
options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(options=options)
driver.get(url)
# Wait for the page to load and the button to become clickable
time.sleep(5)
# Click the "Pinup Popper" button to trigger the CSV download
button = driver.find_element(By.XPATH, "//button[contains(text(), 'Pinup Popper')]")
button.click()
# Wait for the download to complete
time.sleep(10)
# Extract the CSV data URL
links = driver.find_elements(By.XPATH, "//a[@href]")
csv_data_url = None
for link in links:
href = link.get_attribute("href")
if href.startswith("data:text/csv"):
csv_data_url = href
break
if csv_data_url:
# Extract the CSV content from the data URL
csv_content = csv_data_url.split(',', 1)[1]
# Decode the percent-encoded CSV content
decoded_csv_content = urllib.parse.unquote(csv_content)
file_name = os.path.join(download_dir, "puplookup.csv")
# Save the CSV content to a file
with open(file_name, "w", encoding="utf-8") as csv_file:
csv_file.write(decoded_csv_content)
print(f"Saved {file_name}")
else:
print("CSV data URL not found.")
driver.quit()