Skip to content

Commit

Permalink
advanced_download() Reday! -> not tested thoroughly | but seems to wo…
Browse files Browse the repository at this point in the history
…rk just fine
  • Loading branch information
0AwsD0 committed May 5, 2024
1 parent 72d988e commit 1ed4c2d
Show file tree
Hide file tree
Showing 5 changed files with 626 additions and 86 deletions.
216 changes: 187 additions & 29 deletions download.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

#BTW selenium 420 ~blaze it XD

def download(**data):
print("Download function invoked with arguments: ", data)

#below code checks if the basic option is selected - if yes -> the universal function will be invoked | if no -> the advanced functions will be invoked - tailored to the data that is for set game type
try:
if data["mode"] == "basic":
if data["mode"] == "basic":
try:
basic_download(data["url"], data["name"], data["tags"])
else:
advanced_download(data)
except Exception:
print("ERROR - download function couldn't be invoked. Is website down?")
print("Exiting in 5 seconds.")
time.sleep(5)
exit(1)

except:
print("basic_download() - failed")
print("Is website down?")
print("Did you input proper values?")
print("Exiting in 5 seconds.")
time.sleep(5)
exit(1)
else:
try:
advanced_download(**data)
except:
print("advanced_download() - failed")
print("Is website down?")
print("Did you input proper values?")
print("Exiting in 5 seconds.")
time.sleep(5)
exit(1)


def basic_download(url, name, tags):
Expand Down Expand Up @@ -56,6 +67,12 @@ def basic_download(url, name, tags):

#base url - the url2 is for adding '&page='+i and than reseting it back to normal before adding next &page
url = driver.current_url
except Exception:
print("Exception in basic_download() before loop: " + Exception)
print("Exiting in 5 seconds.")
time.sleep(5)
driver.quit()
exit(1)

while (flag == 1):
try:
Expand Down Expand Up @@ -111,30 +128,171 @@ def basic_download(url, name, tags):
i2 = 1
i += 1
except Exception:
print("The download failed. "+Exception)
print("The download failed. //loop "+Exception)
print("Exiting in 5 seconds.")
time.sleep(5)
driver.quit()
exit(1)
except Exception:
print("Exception in basic_download(): "+Exception)
print("Exiting in 5 seconds.")
time.sleep(5)
driver.quit()
exit(1)

def advanced_download(data):
print("Advanced download function invoked")
print("DATA: "+data)
#KK download(game = game, name = name, tags = tags, gender = gender, personality = personality, game_type = game_type, modded_content = modded_content, order_by = order_by, show_hidden = show_hidden, show_only_featured = show_only_featured)
#AA2 download(game = game, name = name, tags = tags, gender = gender, personality = personality, order_by = order_by, show_hidden = show_hidden, show_only_featured = show_only_featured) *
#HS download(game = game, name = name, tags = tags, gender = gender, personality = personality, game_type = game_type, order_by = order_by, show_hidden = show_hidden, show_only_featured = show_only_featured)
#PH download(game = game, name = name, tags = tags, order_by = order_by, show_hidden = show_hidden, show_only_featured = show_only_featured)
#AI_HS2 download(game = game, name = name, tags = tags, gender = gender, personality = personality, order_by = order_by, show_hidden = show_hidden, show_only_featured = show_only_featured) *
#COM3D2 download(game = game, name = name, tags = tags, preset_type = preset_type, order_by = order_by, show_hidden = show_hidden, show_only_featured = show_only_featured)
#SH download(game = game, name = name, tags = tags, order_by = order_by, show_hidden = show_hidden, show_only_featured = show_only_featured)
#HC download(game = game, name = name, tags = tags, gender = gender, order_by = order_by, show_hidden = show_hidden, show_only_featured = show_only_featured)
#AA2_&_AI_HS2
#I tried to make below shorter since the download LOOP is same as in basic download, but the driver must be initialized in same method, so I can't do it without spawning additional unnecessary browsers
#But in other end it's easier to deal with code that is simpler, even if there is more of it. ~Terry would approve
def advanced_download(**data):
print("Advanced download function invoked.")
print("DATA: ", data)
'''
ALL POSIBLE DATA:
game
name
tags
preset_type
gender
personality
game_type
modded_content
order_by
show_hidden
show_only_featured
'''
#I need to let user config wait time and provide some values or idk explain it in documentation and readme.md
#OR
#I may try using watchdog or directory scan to check if file exists -> the card and waiting until it does so there is no need for download interval - it downloads only if previous card is downloaded
print("WARNING: Some cards weigh a lot, even over 25MB - if your intewrnet is slow CHANGE WAIT TIME between card downloads to even 10 seconds!")

url = data["url"]
name = data["name"]
tags = data["tags"]

show_hidden = data["show_hidden"]
show_only_featured = data["show_only_featured"]
order_by = data["order_by"]

#selenium code below

options = webdriver.FirefoxOptions()
#options.add_argument("-headless")
#driver = webdriver.Firefox(options=options)
driver = webdriver.Firefox()
driver.get(url)

# I know I can just make up the link to the website since it's uses GET method like "https://db.bepis.moe/koikatsu?name=aaaa&tag=bbbb" but I want to try use the way below
name_input = driver.find_element(By.ID, "name")
name_input.send_keys(name)
tag_input = driver.find_element(By.ID, "tag")
tag_input.send_keys(tags)

#below mess to detect if "varible" (key) exists, if yes -> create the varible -> than use it to fill the form field
try:
preset_type = data["preset_type"]
preset_type_input = Select(driver.find_element(By.ID, "type"))
if (preset_type != ""):
preset_type_input.select_by_value(preset_type)
except:
print("preset_type not found in datased - skipping")

try:
gender = data["gender"]
gender_input = Select(driver.find_element(By.ID, "gender"))
if (gender != ""):
gender_input.select_by_value(gender)
else:#for test
print("gender > empty")#for test
except:
print("gender not found in datased - skipping")

try:
personality = data["personality"]
personality_input = Select(driver.find_element(By.ID, "personality"))
if (personality != ""):
personality_input.select_by_value(personality)
except:
print("personality not found in datased - skipping")

try:
game_type = data["game_type"]
game_type_input = Select(driver.find_element(By.ID, "type"))
if (game_type != ""):
game_type_input.select_by_value(game_type)
except:
print("game_type not found in datased - skipping")

try:
modded_content = data["modded_content"]
except:
print("modded_content not found in datased - skipping")

#submit whole form
tag_input.submit()

print("Waiting 5 seconds for page to load...")
time.sleep(5)

#just set the loop to be true until the "Next" button is "disabled" than flip the flag to exit loop // if Next button DISABLED flag = 0
i = 2
i2 = 1
flag = 1

#base url - the url2 is for adding '&page='+i and than reseting it back to normal before adding next &page
url = driver.current_url

while (flag == 1):
try:
try:
while (i2 <= 24):
print(f"Downloading card number: {i2}")
download_selector = "document.querySelector('#inner-card-body > div:nth-child(" + str(i2) + ") > div > div > a.btn.btn-primary.btn-sm').click();"
time.sleep(4)
driver.execute_script(download_selector)
i2 += 1
except Exception:
print("The download FINISHED!")
print("There were less than 24 cards on last page.")
print(">>OR There is slight chance, that website got down.")
# flag == 0 + go back to main() // or leave like that to exit program
print("Exiting in 5 seconds.")
time.sleep(5)
driver.quit()
exit(0)
next_button_script = """
xpath = "//a[contains(text(),'Next')]";
var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
parentDiv = matchingElement.parentNode;
if(parentDiv.classList.contains('disabled') == true){
var state = "Disabled";
return state;
}
else{
var state = "Enabled";
return state;
}
"""
print("Getting 'Next' button state...")
button_state = driver.execute_script(next_button_script)
#sleep for script and mainly for last card/cards to download
time.sleep(5)
print("Button state = "+button_state)
if(button_state == "Disabled"):
print("Download FINISHED!")
# flag == 0 + go back to main() // or leave like that to exit program
print("-------------------")
print("Exiting in 5 seconds.")
driver.quit()
time.sleep(5)
exit(0)
print("Getting url...")
url2 = url
print("Got url - adding page number...")
url2 += "&page=" + str(i)
print("Waiting 5 seconds for page to load...")
driver.get(url2)
time.sleep(5)
i2 = 1
i += 1
except Exception:
print("The download failed. ", Exception)
print("Exiting in 5 seconds.")
time.sleep(5)
driver.quit()
exit(1)

def main():
print("download.py")
Expand Down
Loading

0 comments on commit 1ed4c2d

Please sign in to comment.