Fixed style issues

- added encoding comment
- added author comment
- reduced line character count
pull/6/head
Trivernis 6 years ago
parent fe8858c3ed
commit 63847390f1

@ -37,7 +37,7 @@ image-extensions:
### Help output ### Help output
```sh ```
Usage: riddle.py [options] [subreddits] Usage: riddle.py [options] [subreddits]
Options: Options:

@ -1,3 +1,6 @@
# coding: utf-8
# author: u/Trivernis
import os import os
import shutil import shutil
import yaml import yaml
@ -46,6 +49,7 @@ class ProgressBar:
""" """
A simple progressbar. A simple progressbar.
""" """
def __init__(self, total=100, prefix='', suffix='', length=50, fill=''): def __init__(self, total=100, prefix='', suffix='', length=50, fill=''):
self.prefix = prefix self.prefix = prefix
self.suffix = suffix self.suffix = suffix
@ -55,10 +59,19 @@ class ProgressBar:
self.progress = 0 self.progress = 0
def tick(self): def tick(self):
"""
Next step of the progressbar. The stepwidth is always 1.
:return:
"""
self.progress += 1 self.progress += 1
self._print_progress() self._print_progress()
def setprogress(self, progress): def setprogress(self, progress: float):
"""
Set the progress of the bar.
:param progress: progress in percent
:return: None
"""
self.progress = progress self.progress = progress
self._print_progress() self._print_progress()
@ -72,7 +85,7 @@ class ProgressBar:
filled_length = int(self.length * iteration // total) filled_length = int(self.length * iteration // total)
bar = self.fill * filled_length + '-' * (self.length - filled_length) bar = self.fill * filled_length + '-' * (self.length - filled_length)
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end='\r') print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end='\r')
# Print New Line on Complete # Print new line on complete
if iteration == total: if iteration == total:
print() print()
@ -89,7 +102,8 @@ def parser_init():
If not set it is the maximum fetchable number.""") If not set it is the maximum fetchable number.""")
parser.add_option('-o', '--output', dest='output', parser.add_option('-o', '--output', dest='output',
type='str', default=None, type='str', default=None,
help='The name of the output folder. If none is specified, it\'s the subreddits name.') help="""The name of the output folder.
If none is specified, it\'s the subreddits name.""")
parser.add_option('-z', '--zip', dest='zip', parser.add_option('-z', '--zip', dest='zip',
action='store_true', default=False, action='store_true', default=False,
help='Stores the images in a zip file if true') help='Stores the images in a zip file if true')
@ -110,7 +124,7 @@ def get_images(reddit_client: praw.Reddit, subreddit: str, limit: int, nsfw: boo
""" """
print('[~] Fetching images for r/%s...' % subreddit) print('[~] Fetching images for r/%s...' % subreddit)
urls = [submission.url for submission in reddit_client.subreddit(subreddit).hot(limit=limit) urls = [submission.url for submission in reddit_client.subreddit(subreddit).hot(limit=limit)
if not submission.over_18 or nsfw] # fetches hot images and filters by nsfw if nsfw not set to true if not submission.over_18 or nsfw] # fetches hot images and filters nsfw if set to false
return [url for url in urls if url.split('.')[-1] in img_ext] return [url for url in urls if url.split('.')[-1] in img_ext]
@ -165,6 +179,10 @@ def compress_folder(folder: str, zip_fname: str):
def main(): def main():
"""
Main entry method. Loads the settings and iterates through subreddits and downloads all images it fetched.
If the --zip flag is set, the images will be downloaded in a .cache directory and then compressed.
"""
options, subreddits = parser_init() options, subreddits = parser_init()
with open('config.yaml', 'r') as file: # loads the config.yaml file with open('config.yaml', 'r') as file: # loads the config.yaml file
try: try:
@ -172,7 +190,7 @@ def main():
except yaml.YAMLError as err: except yaml.YAMLError as err:
print(err) print(err)
if settings: if settings:
if 'image-extensions' in settings: # uses image extensions specified in config.yaml fallback to default if 'image-extensions' in settings:
global img_ext global img_ext
img_ext = settings['image-extensions'] img_ext = settings['image-extensions']
credentials = settings['credentials'] credentials = settings['credentials']
@ -184,11 +202,12 @@ def main():
for subreddit in subreddits: for subreddit in subreddits:
dldest = subreddit dldest = subreddit
if options.output: if options.output:
dldest = options.output # uses the -o output destination instead of a folder with the subreddit name dldest = options.output # uses the -o output destination
images = get_images(client, subreddit, limit=options.count, nsfw=options.nsfw) images = get_images(client, subreddit, limit=options.count,
if options.zip: # downloads to a cache-folder first before compressing it to zip nsfw=options.nsfw)
download_images(images, '.cache') if options.zip: # download to zip instead
compress_folder('.cache', dldest+'.zip') download_images(images, '.cache') # download to cache
compress_folder('.cache', dldest + '.zip') # add to zip
shutil.rmtree('.cache') shutil.rmtree('.cache')
else: else:
download_images(images, dldest) download_images(images, dldest)

Loading…
Cancel
Save