sbtools/handler.py
2017-09-13 18:15:53 +02:00

103 lines
2.7 KiB
Python
Executable file

#!/usr/bin/env python3
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import glob, multiprocessing, re, requests, urllib.request, urllib.error, urllib.parse, time, os
import setuptools
useragent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0"
session = None
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)
driver.get(url)
time.sleep(period)
def loginHandler(driver, user = setuptools.user(), password = setuptools.password()):
loadPage("https://scriptzbase.org/login/", driver, 3, True)
curfield = driver.find_element_by_name("login")
curfield.send_keys(user)
curfield = driver.find_element_by_name("password")
curfield.send_keys(password)
curfield.send_keys(Keys.RETURN)
time.sleep(3)
return status(driver)
class LoginError(Exception):
pass
def login(driver):
global session
if not (status(driver) or loginHandler(driver)):
raise LoginError("Login failed.")
session = requests.Session()
kekse = driver.get_cookies()
for keks in kekse:
session.cookies.set(keks["name"], keks["value"])
session.headers.update({"User-Agent": useragent})
return True
def fileHandler(url, retry = False):
global session
try:
fid = url.split("=")[1]
if not glob.glob("files/sbd%s*" % fid):
res = session.get("https://scriptzbase.org/%s" % url)
fname = re.findall("filename=(.+)", res.headers["content-disposition"])[0].split(";")[0].strip('"')
with open("files/sbd%s_%s" % (fid, fname), "wb") as out:
out.write(res.content)
except (requests.exceptions.ConnectionError, requests.exceptions.ChunkedEncodingError):
if not retry:
time.sleep(10)
fileHandler(url, True)
else:
raise
def pageHandler(driver):
juha = BeautifulSoup(driver.page_source, "html5lib")
for a in juha.findAll("a"):
try:
if "/download?version=" in a["href"]:
fileHandler(a["href"])
except KeyError:
pass
def siteHandler(driver, p = 1):
loadPage("https://scriptzbase.org/nulled_scripts/?page=" + str(p), driver)
if driver.current_url[-len(str(p)):] == str(p):
pageHandler(driver)
siteHandler(driver, p+1)
def mainHandler(driver):
os.makedirs("files", exist_ok=True)
siteHandler(driver)
if __name__ == "__main__":
caps = webdriver.DesiredCapabilities().PHANTOMJS.copy()
caps["phantoms.page.settings.userAgent"] = useragent
driver = webdriver.PhantomJS(desired_capabilities=caps)
mainHandler(driver)
driver.close()