fread/readit.py

92 lines
2.3 KiB
Python
Raw Normal View History

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")
f = urllib.request.urlopen(img)
2017-02-08 23:39:07 +00:00
with open("%s/%i.png" % (id, page), "b+w") as o:
2017-02-08 23:27:02 +00:00
o.write(f.read())
try:
img2 = driver.find_element_by_id("page2").get_attribute("src")
f2 = urllib.request.urlopen(img2)
2017-02-08 23:39:07 +00:00
with open("%s/%i.png" % (id, page + 1), "b+w") as o2:
2017-02-08 23:27:02 +00:00
o2.write(f2.read())
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()
parser.add_argument("id", help="ID of the magazine to be downloaded. May be a URL.")
id = parser.parse_args().id.split("/")[-1].split("#")[0]
driver = webdriver.Firefox()
magazineHandler(driver, id)
driver.quit()