Skip to content

Add page detection and paging to the code and improved Python 3 support #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import requests
import sys, getopt
from importlib import reload
import json
from lxml import html

Expand Down Expand Up @@ -314,22 +315,34 @@ def main(argv):
print("Logged in successfully!")

if book_assets:

# get the list of books
books_page = session.get("https://www.packtpub.com/account/my-ebooks", verify=True, headers=headers)
books_tree = html.fromstring(books_page.content)
book_nodes = books_tree.xpath("//div[@id='product-account-list']/div[contains(@class,'product-line unseen')]")

print('###########################################################################')
print("FOUND {0} BOOKS: STARTING DOWNLOADS".format(len(book_nodes)))
print('###########################################################################')

# loop through the books
for book in book_nodes:

# download the book
books_directory = os.path.join(root_directory, "books")
download_book(book, books_directory, book_assets, session, headers)
current_page = 1
total_pages = 1
checked_pages = False

while (current_page <= total_pages):
# get the list of books
books_page = session.get("https://www.packtpub.com/account/my-ebooks?page={}".format(current_page), verify=True, headers=headers)
books_tree = html.fromstring(books_page.content)
book_nodes = books_tree.xpath("//div[@id='product-account-list']/div[contains(@class,'product-line unseen')]")

if not checked_pages:
page_nodes = books_tree.xpath("//div[contains(@class,'solr-pager-page-selector')]")
if len(page_nodes) > 0:
total_pages = len(page_nodes[0].getchildren())
checked_pages = True

print('###########################################################################')
print("FOUND {0} BOOKS ON PAGE {1}/{2}: STARTING DOWNLOADS".format(len(book_nodes), current_page, total_pages))
print('###########################################################################')

# loop through the books
for book in book_nodes:

# download the book
books_directory = os.path.join(root_directory, "books")
download_book(book, books_directory, book_assets, session, headers)

current_page += 1

if video_assets:

Expand Down Expand Up @@ -369,6 +382,7 @@ def main(argv):


if __name__ == "__main__":
reload(sys)
sys.setdefaultencoding('utf8')
if sys.version_info[0] < 3:
reload(sys)
sys.setdefaultencoding('utf8')
main(sys.argv[1:])