Skip to content

Add support for config file #31

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 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
129 changes: 129 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@

# Created by https://www.gitignore.io/api/python

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

### Python Patch ###
.venv/

# VSCode config
.vscode

### Python.VirtualEnv Stack ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
pip-selfcheck.json


# End of https://www.gitignore.io/api/python
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ Automatically download all your eBooks and videos. (See: [PacktPub Free Daily Bo


## Usage:
python downloader.py -e <email> -p <password> [-d <directory> -b <book assets> -v <video assets>]
python downloader.py -e <email> -p <password> [-d <directory> -b <book assets> -v <video assets> -C <configuration file>]

##### Example: Download books in PDF and EPUB formats and accompanying source code
python downloader.py -e hello@world.com -p p@ssw0rd -d ~/Desktop/packt -b pdf,epub,code
python downloader.py -C config.json -d ~/Desktop/packt -b pdf,epub,code

##### Example: Download videos, their cover image, and accompanying source code
python downloader.py -e hello@world.com -p p@ssw0rd -d ~/Desktop/packt -v video,cover,code
python downloader.py -C config.json -d ~/Desktop/packt -v video,cover,code

##### Example: Download Integrated Courses (Interactive-Ebooks), their cover image, and accompanying source code
python downloader.py -e hello@world.com -p p@ssw0rd -d ~/Desktop/packt -c course,cover,code
python downloader.py -C config.json -d ~/Desktop/packt -c course,cover,code

##### Example: Download everything
python downloader.py -e hello@world.com -p p@ssw0rd -d ~/Desktop/packt -b pdf,epub,mobi,cover,code,info -v video,cover,code -c course,cover,code
python downloader.py -C config.json -d ~/Desktop/packt -b pdf,epub,mobi,cover,code,info -v video,cover,code -c course,cover,code


## Commandline Options
Expand All @@ -26,6 +30,7 @@ Automatically download all your eBooks and videos. (See: [PacktPub Free Daily Bo
- *-v*, *--videos* = Assets to download. Options are: *video,cover,code*
- *-b*, *--books* = Assets to download. Options are: *pdf,mobi,epub,cover,code,info*
- *-c*, *--courses* = Assets to download. Options are: *course,cover,code*
- *-C*, *--config* = A configuration file holding your authentication details (see config.sample.json)

**Video Assets**

Expand Down Expand Up @@ -60,4 +65,7 @@ Automatically download all your eBooks and videos. (See: [PacktPub Free Daily Bo

pip install lxml

Tested working on Python 2.7.11 and Python 3.6.0 :: Anaconda 4.3.0 (64-bit)
### Install dependencies:
pip install -r requirements.txt

Tested working on Python 2.7.11 and Python 3.6.0 :: Anaconda 4.3.0 (64-bit)
4 changes: 4 additions & 0 deletions config.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"username": "youremail@here.com",
"password": "MyFabulousPassword"
}
25 changes: 18 additions & 7 deletions downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def download_book(book, directory, assets, session, headers):
# cover image
if len(image) > 0 and 'cover' in assets:
filename = os.path.join(book_directory, title + ".jpg")
image_url = "https:" + image[0].replace("/imagecache/thumbview", "", 1)
image_url = image[0].replace("/imagecache/thumbview", "", 1)
print("Downloading IMAGE")
download_to_file(filename, image_url, session, headers, False)

Expand Down Expand Up @@ -247,12 +247,13 @@ def main(argv):
book_assets = None # 'pdf,mobi,epub,cover,code'
video_assets = None # 'video,cover,code'
course_assets = None # 'course,cover,code'
errorMessage = 'Usage: downloader.py -e <email> -p <password> [-d <directory> -b <book assets> -v <video assets> -c <course assets>]'
config_file = None # '~/packtpub_config.json'
errorMessage = 'Usage: downloader.py -e <email> -p <password> [-d <directory> -b <book assets> -v <video assets> -c <course assets>] -C <config file>'

# get the command line arguments/options
try:
opts, args = getopt.getopt(argv,"e:p:d:b:v:c:",["email=","pass=","directory=","books=","videos=","courses="])
except getopt.GetoptError:
opts, args = getopt.getopt(argv,"e:p:d:b:v:c:C:",["email=","pass=","directory=","books=","videos=","courses=","config="])
except getopt.GetoptError as error:
print(errorMessage)
sys.exit(2)

Expand All @@ -270,12 +271,16 @@ def main(argv):
video_assets = arg
elif opt in ('-c','--courses'):
course_assets = arg
elif opt in ('-C','--config'):
config_file = arg


# do we have the minimum required info?
if not email or not password:
print(errorMessage)
sys.exit(2)
if not config_file:
if not email or not password:
print("Missing authentication info, please provide username and password or a config file with this information.")
print(errorMessage)
sys.exit(2)

# create an http session
session = requests.Session()
Expand All @@ -291,6 +296,12 @@ def main(argv):
form_build_id = tree.xpath('//form[@id="packt-user-login-form"]//input[@name="form_build_id"]/@id')[0]

# payload for login
if config_file:
with open(config_file, 'r') as f:
data = json.load(f)
email = data['username']
password = data['password']

login_data = dict(
email=email,
password=password,
Expand Down
18 changes: 18 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
astroid==1.6.5
backports.functools-lru-cache==1.5
certifi==2018.8.24
chardet==3.0.4
configparser==3.5.0
enum34==1.1.6
futures==3.2.0
idna==2.7
isort==4.3.4
lazy-object-proxy==1.3.1
lxml==4.2.4
mccabe==0.6.1
pylint==1.9.3
requests==2.19.1
singledispatch==3.4.0.3
six==1.11.0
urllib3==1.23
wrapt==1.10.11