You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
modpack/details/update-mods.py

102 lines
3.5 KiB

from time import sleep
import random
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
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
# These, absolute PSYCHOPATHS, are using cloudflare to identify bot traffic.
# random sleep has been introduced to reduce the potential for being flagged.
base_url = "https://www.curseforge.com"
file_path = 'SteveOnlineModpack_List.txt'
mod_version = '1.20.1'
# Important page elements
download_row_xpath = '/html/body/div[2]/div/main/div[1]/div[2]/div/section/div[3]/div/div[2]/a'
download_button_xpath = '/html/body/div[2]/div/main/div[1]/div[2]/div/section/section/h2/div/div/a[1]'
# Options
user_agent = 'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
chrome_options = Options()
chrome_options.add_argument(user_agent)
chrome_options.add_argument('--allow-running-insecure-content')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--safebrowsing-disable-download-protection')
chrome_options.add_argument("--disable-features=InsecureDownloadWarnings")
chrome_options.add_experimental_option("prefs", {
"safebrowsing.enabled": False,
"download.default_directory": "/home/spaulding/Downloads/mods/",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
})
driver = webdriver.Chrome(service=Service(), options=chrome_options)
driver.maximize_window()
def do_sleep(start, finish, action):
print(action)
sleep(float(random.randrange(start, finish)))
return
def do_cf(url):
files_query_string = f"/files/all?page=1&pageSize=10&version={mod_version}&gameVersionTypeId=1&showAlphaFiles=hide"
files_url = f'{url}{files_query_string}'
try:
do_sleep(3, 6, 'Try URL..')
driver.get(files_url)
do_sleep(3, 6, 'Find file rows..')
WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.XPATH, download_row_xpath))
)
do_sleep(3, 6, 'Find first download row..')
download_list_link = driver.find_element(By.XPATH, download_row_xpath)
do_sleep(1, 3, 'Setup action chain..')
actions = ActionChains(driver)
do_sleep(1, 3, 'Click on first download element')
actions.move_to_element(download_list_link).click().perform()
do_sleep(3, 6, 'Wait for download page..')
WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.XPATH, download_button_xpath))
)
do_sleep(1, 3, 'Find download button..')
download_btn = driver.find_element(By.XPATH, download_button_xpath)
do_sleep(3, 6, 'Click download button..')
actions.move_to_element(download_btn).click().perform()
do_sleep(12, 24, 'Wait for download to start..')
except Exception as e:
print('!!!!!!!!!!!!!!!!!!!')
print('Could not download file from URL: %s' % url)
print('Error: %s' % e)
print('!!!!!!!!!!!!!!!!!!!')
count = 0
with open(file_path, 'r') as f:
for line in f:
count += 1
print('Processing line %s' % count)
print(line)
do_cf(line)
print('============================================')
# End the session
driver.quit()