Remove painful recursion, turn driver into global variable
This commit is contained in:
parent
fc14091296
commit
6ecb783e3e
1 changed files with 39 additions and 20 deletions
59
handler.py
59
handler.py
|
@ -9,21 +9,26 @@ import setuptools
|
|||
|
||||
useragent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0"
|
||||
session = None
|
||||
driver = None
|
||||
|
||||
def status():
|
||||
global driver
|
||||
|
||||
def status(driver):
|
||||
if "Benutzername oder E-Mail-Adresse:" not in driver.page_source and 'href="login/"' not in driver.page_source:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def loadPage(url, driver, period=5,init=False):
|
||||
if not (init or status(driver)):
|
||||
login(driver)
|
||||
def loadPage(url, period=5, init = False):
|
||||
global driver
|
||||
|
||||
if not (init or status()):
|
||||
login()
|
||||
driver.get(url)
|
||||
time.sleep(period)
|
||||
|
||||
def loginHandler(driver, user = setuptools.user(), password = setuptools.password()):
|
||||
loadPage("https://scriptzbase.org/login/", driver, 3, True)
|
||||
def loginHandler(user = setuptools.user(), password = setuptools.password()):
|
||||
loadPage("https://scriptzbase.org/login/", 3, True)
|
||||
|
||||
curfield = driver.find_element_by_name("login")
|
||||
curfield.send_keys(user)
|
||||
|
@ -34,15 +39,15 @@ def loginHandler(driver, user = setuptools.user(), password = setuptools.passwor
|
|||
curfield.send_keys(Keys.RETURN)
|
||||
time.sleep(3)
|
||||
|
||||
return status(driver)
|
||||
return status()
|
||||
|
||||
class LoginError(Exception):
|
||||
pass
|
||||
|
||||
def login(driver):
|
||||
global session
|
||||
def login():
|
||||
global driver, session
|
||||
|
||||
if not (status(driver) or loginHandler(driver)):
|
||||
if not (status() or loginHandler()):
|
||||
raise LoginError("Login failed.")
|
||||
|
||||
session = requests.Session()
|
||||
|
@ -74,7 +79,9 @@ def fileHandler(url, retry = False):
|
|||
else:
|
||||
raise
|
||||
|
||||
def pageHandler(driver):
|
||||
def pageHandler():
|
||||
global driver
|
||||
|
||||
juha = BeautifulSoup(driver.page_source, "html5lib")
|
||||
|
||||
for a in juha.findAll("a"):
|
||||
|
@ -84,20 +91,32 @@ def pageHandler(driver):
|
|||
except KeyError:
|
||||
pass
|
||||
|
||||
def siteHandler(driver, p = 1):
|
||||
loadPage("https://scriptzbase.org/nulled_scripts/?page=" + str(p), driver)
|
||||
def siteHandler(p):
|
||||
global driver
|
||||
|
||||
loadPage("https://scriptzbase.org/nulled_scripts/?page=" + str(p))
|
||||
|
||||
if driver.current_url[-len(str(p)):] == str(p):
|
||||
pageHandler(driver)
|
||||
siteHandler(driver, p+1)
|
||||
pageHandler()
|
||||
return True
|
||||
|
||||
def mainHandler(driver):
|
||||
return False
|
||||
|
||||
def mainHandler():
|
||||
os.makedirs("files", exist_ok=True)
|
||||
siteHandler(driver)
|
||||
p = 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
while True:
|
||||
if not siteHandler(p):
|
||||
break
|
||||
p += 1
|
||||
|
||||
def makeDriver():
|
||||
caps = webdriver.DesiredCapabilities().PHANTOMJS.copy()
|
||||
caps["phantoms.page.settings.userAgent"] = useragent
|
||||
driver = webdriver.PhantomJS(desired_capabilities=caps)
|
||||
mainHandler(driver)
|
||||
return webdriver.PhantomJS(desired_capabilities=caps)
|
||||
|
||||
if __name__ == "__main__":
|
||||
driver = makeDriver()
|
||||
mainHandler()
|
||||
driver.close()
|
||||
|
|
Loading…
Reference in a new issue