Added tox to project

This commit is contained in:
JayPiKay 2018-05-12 10:54:59 +02:00
parent d91a77e115
commit ef0efe3bb9
4 changed files with 55 additions and 15 deletions

29
imgurdl.py Normal file → Executable file
View File

@ -7,13 +7,13 @@
# Distributed under terms of the MIT license.
"""
IMGUR Gallery downloader.
"""
import os
import requests
import argparse
from urllib.parse import urlparse
import argparse
import requests
from progressbar import (Bar, ETA, FileTransferSpeed, Percentage, ProgressBar)
@ -21,34 +21,36 @@ AJAX_BASEURL = 'https://imgur.com/ajaxalbums/getimages/'
IMAGE_BASEURL = 'https://i.imgur.com/'
parser = argparse.ArgumentParser(description='Imgur Album Downloader')
parser.add_argument('outdir', metavar='OUTPUT_DIR', type=str,
PARSER = argparse.ArgumentParser(description='Imgur Album Downloader')
PARSER.add_argument('outdir', metavar='OUTPUT_DIR', type=str,
help='Download the album into OUTPUT_DIR')
parser.add_argument('album', metavar='ALBUM_HASH', type=str,
PARSER.add_argument('album', metavar='ALBUM_HASH', type=str,
help='The album hash identifier to download')
args = parser.parse_args()
ARGS = PARSER.parse_args()
def download_image(filename, out_directory):
"""Download an image and save it locally."""
if not os.path.exists(out_directory):
os.makedirs(out_directory)
filepath = os.path.join(out_directory, filename)
req = requests.get(IMAGE_BASEURL + filename, stream=True)
with open(filepath, 'wb') as fd:
with open(filepath, 'wb') as imgfd:
widgets = ['Download: {} '.format(filename), Percentage(), ' ',
Bar(), ' ', ETA(), ' ', FileTransferSpeed()]
content_length = int(req.headers['content-length'])
pbar = ProgressBar(widgets=widgets, maxval=content_length).start()
filesize = 0
for chunk in req.iter_content(chunk_size=8192):
fd.write(chunk)
imgfd.write(chunk)
filesize += len(chunk)
pbar.update(filesize)
pbar.finish()
def get_ajaxalbum(album_id, out_directory):
"""Download the `hit.json` file of an album and process all images."""
req = requests.get(AJAX_BASEURL + album_id + '/hit.json')
jsonalbum = req.json()
# num_images = jsonalbum['data']['count']
@ -58,9 +60,6 @@ def get_ajaxalbum(album_id, out_directory):
if __name__ == '__main__':
parsed_url = urlparse(args.album)
album = os.path.split(parsed_url.path)[-1]
get_ajaxalbum(album, args.outdir)
# album_id = 'ThiBLHO'
# out_directory = 'album'
# get_ajaxalbum(album_id, out_directory)
PARSED_URL = urlparse(ARGS.album)
ALBUM = os.path.split(PARSED_URL.path)[-1]
get_ajaxalbum(ALBUM, ARGS.outdir)

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
flake8
pylint
progressbar2==3.37.1
requests==2.18.4

21
setup.py Normal file
View File

@ -0,0 +1,21 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 jpk <jpk+dev@goatpr0n.de>
#
# Distributed under terms of the MIT license.
"""
"""
from setuptools import setup, find_packages
setup(
name='Imgurdl',
version='1.0',
packages=find_packages(),
scripts=['imgurdl.py'],
)

16
tox.ini Normal file
View File

@ -0,0 +1,16 @@
[tox]
envlist = lint
[testenv]
deps = pytest
commands = pytest
[testenv:lint]
deps = -r{toxinidir}/requirements.txt
commands =
flake8 imgurdl.py
[testenv:pylint]
deps = -r{toxinidir}/requirements.txt
commands =
pylint imgurdl.py