sbtools/handler.py

85 lines
2.4 KiB
Python
Raw Normal View History

#!/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
2017-09-09 12:40:28 +00:00
import glob, multiprocessing, re, requests, urllib.request, urllib.error, urllib.parse, time, os
import setuptools
def status(driver):
2017-09-09 12:40:28 +00:00
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()):
2017-09-09 12:40:28 +00:00
loadPage("https://scriptzbase.org/login/",driver,3,True)
2017-09-09 12:40:28 +00:00
curfield = driver.find_element_by_name("login")
curfield.send_keys(user)
2017-09-09 12:40:28 +00:00
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):
if not (status(driver) or loginHandler(driver)):
raise LoginError("Login failed.")
return True
2017-09-09 12:40:28 +00:00
def pageHandler(driver):
2017-01-30 23:52:56 +00:00
juha = BeautifulSoup(driver.page_source, "html5lib")
2017-09-09 12:40:28 +00:00
session = requests.Session()
kekse = driver.get_cookies()
2017-09-09 12:40:28 +00:00
for keks in kekse:
2017-09-10 07:48:49 +00:00
session.cookies.set(keks["name"], keks["value"])
2017-09-09 12:40:28 +00:00
for a in juha.findAll("a"):
2017-09-10 08:32:25 +00:00
try:
if "/download?version=" in a["href"]:
fid = a["href"].split("=")[1]
if not glob.glob("files/sbd%s*" % fid):
res = session.get("https://scriptzbase.org/%s" % a["href"])
fname = re.findall("filename=(.+)", res.headers["content-disposition"])[0].strip('"')
2017-09-10 08:32:25 +00:00
with open("files/sbd%s_%s" % (fid, fname), "wb") as out:
out.write(res.content)
except KeyError:
pass
2017-09-09 12:40:28 +00:00
def siteHandler(driver, p = 1):
2017-09-10 08:32:25 +00:00
loadPage("https://scriptzbase.org/nulled_scripts/?page=" + str(p), driver)
2017-09-09 12:40:28 +00:00
if driver.current_url[-len(str(p)):] == str(p):
if pageHandler(driver):
siteHandler(driver, p+1)
2017-01-30 23:52:56 +00:00
2017-09-09 12:40:28 +00:00
def mainHandler(driver):
2017-09-10 08:32:25 +00:00
os.makedirs("files", exist_ok=True)
loginHandler(driver)
2017-09-09 12:40:28 +00:00
siteHandler(driver)
if __name__ == "__main__":
caps = webdriver.DesiredCapabilities().PHANTOMJS.copy()
caps["phantoms.page.settings.userAgent"] = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:54.0) Gecko/20100101 Firefox/54.0"
driver = webdriver.PhantomJS(desired_capabilities=caps)
2017-09-09 12:40:28 +00:00
mainHandler(driver)
driver.close()