2017-02-08 23:27:02 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from selenium import webdriver
|
|
|
|
from selenium.webdriver.common.keys import Keys
|
|
|
|
from selenium.common.exceptions import NoSuchElementException
|
|
|
|
import argparse, multiprocessing, urllib.request, urllib.error, urllib.parse, time, os
|
|
|
|
import helpers, setuptools
|
|
|
|
|
|
|
|
def status(driver):
|
|
|
|
if '<a class="btn btn-primary clickable btn-login">Login</a>' 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)
|
|
|
|
|
|
|
|
return True if driver.current_url == url else False
|
|
|
|
|
|
|
|
def loginHandler(driver, user = setuptools.riuser(), password = setuptools.ripass()):
|
|
|
|
loadPage("https://app.myreadit.com/login",driver,3,True)
|
|
|
|
|
|
|
|
curfield = driver.find_element_by_name("email")
|
|
|
|
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):
|
|
|
|
if not (status(driver) or loginHandler(driver)):
|
|
|
|
raise LoginError("Login failed.")
|
|
|
|
return True
|
|
|
|
|
|
|
|
def pageHandler(driver, id, page):
|
|
|
|
if not loadPage("https://app.myreadit.com/reader/%s#%i" % (id, int(page)), driver):
|
|
|
|
return False
|
|
|
|
|
|
|
|
img = driver.find_element_by_id("page1").get_attribute("src")
|
2017-02-09 20:11:56 +00:00
|
|
|
helpers.downloadPage(img, id, page)
|
2017-02-08 23:27:02 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
img2 = driver.find_element_by_id("page2").get_attribute("src")
|
2017-02-09 20:11:56 +00:00
|
|
|
helpers.downloadPage(img2, id, page + 1)
|
2017-02-08 23:27:02 +00:00
|
|
|
|
|
|
|
return 2
|
|
|
|
|
|
|
|
except:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def magazineHandler(driver, id, makepdf = True):
|
|
|
|
if helpers.makeDir(id):
|
|
|
|
loginHandler(driver)
|
|
|
|
page = 0
|
|
|
|
val = pageHandler(driver, id, 1)
|
|
|
|
|
|
|
|
while val:
|
|
|
|
page += val
|
|
|
|
val = pageHandler(driver, id, page + 1)
|
|
|
|
|
|
|
|
if makepdf:
|
2017-02-08 23:39:07 +00:00
|
|
|
helpers.makePDF(id, page, helpers.PNG)
|
2017-02-08 23:27:02 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
print("[NOTICE] Skipping issue %s - already exists." % id)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
2017-02-08 23:59:32 +00:00
|
|
|
parser.add_argument("id", help="ID of the magazine to be downloaded. May be a URL.", nargs="+")
|
2017-02-08 23:27:02 +00:00
|
|
|
|
2017-02-08 23:59:32 +00:00
|
|
|
ids = parser.parse_args().id
|
2017-02-08 23:27:02 +00:00
|
|
|
driver = webdriver.Firefox()
|
2017-02-08 23:59:32 +00:00
|
|
|
|
|
|
|
for id in ids:
|
|
|
|
use = id.split("/")[-1].split("#")[0]
|
|
|
|
magazineHandler(driver, use)
|
2017-02-08 23:27:02 +00:00
|
|
|
|
2017-02-08 23:40:42 +00:00
|
|
|
driver.quit()
|