Remove painful recursion, turn driver into global variable

This commit is contained in:
Klaus-Uwe Mitterer 2017-09-14 16:23:15 +02:00
parent fc14091296
commit 6ecb783e3e

View file

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