diff --git a/packtdl.py b/packtdl.py new file mode 100755 index 0000000..e9ead67 --- /dev/null +++ b/packtdl.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# Needs grab, pip3 install -U grab +# (also needs: libcurl4-*-dev for curl-config, libxslt1-dev and libxml2-dev) + +import configparser +import logging +import selection +import sys +from grab import Grab +from io import StringIO + +config_str = '[DEFAULT]\n' + open(sys.path[0] + '/CONFIG', 'r').read() +config_fp = StringIO(config_str) + +c = configparser.RawConfigParser() +c.readfp(config_fp) + +logging.basicConfig(level=logging.DEBUG) + +class LoggedOutException(Exception): + '''Raise when attempting an action needing login without being logged in.''' + +class PacktBook(): + def __init__(self): + self.title = "" + self.isbn = "" + self.nid = "" + self.cover_img = "" + self.dl_pdf = "" + self.dl_epub = "" + self.dl_mobi = "" + self.dl_code = "" + +class PacktPub(): + def __init__(self): + self.g = Grab() + self.g.setup(follow_location=True) + self.g.setup(follow_refresh=True) + self.g.setup(timeout=120) + self.g.setup(connect_timeout=10) + #self.g.setup(body_maxsize=512000) + self.logged_in = False + + def login(self, email, password): + self.g.go('https://www.packtpub.com/') + self.g.doc.save('/tmp/packtpub-home.html') + self.g.doc.choose_form(id='packt-user-login-form') + print("Logging in with account: {}".format(email)) + self.g.doc.set_input('email', email) + self.g.doc.set_input('password', password) + self.g.doc.submit() + self.g.doc.save('/tmp/packpub-home-after-login.html') + self.g.doc.text_assert('"sid":') + self.logged_in = True + + def get_ebooks_list(self, url="https://www.packtpub.com/account/my-ebooks"): + '''Loads the list of purchased ebooks and returns a Selection object with all books.''' + if url.startswith("http") and not self.logged_in: + raise LoggedOutException("Must be logged in before getting ebooks list!") + self.g.go(url) + self.g.doc.save('/tmp/packtpub-my-ebooks.html') + self.g.doc.text_assert('