- improved readme
- added comments to riddle.py
pull/1/head
Trivernis 6 years ago
parent 9cb07ea4a1
commit 03cd4ccd99

@ -53,19 +53,19 @@ Options:
### Example ### Example
Download all images from r/EarthPorn: Download all images from [r/EarthPorn](https://EarthPorn.reddit.com):
```sh ```sh
python3 riddle.py EarthPorn python3 riddle.py EarthPorn
``` ```
Download all images from r/astrophotography to a zip-file: Download all images from [r/astrophotography](https://astrophotography.reddit.com) to a zip-file:
```sh ```sh
python3 riddle.py -z astrophotography python3 riddle.py -z astrophotography
``` ```
Download a maximum of 200 images from r/astrophotography or r/EarthPorn to one zip-file named coolpics.zip: Download a maximum of 200 images from [r/astrophotography](https://astrophotography.reddit.com) and [r/EarthPorn](https://EarthPorn.reddit.com) to one zip-file named coolpics.zip:
```sh ```sh
python3 riddle.py -z -c 100 -o coolpics astrophotography EarthPorn python3 riddle.py -z -c 100 -o coolpics astrophotography EarthPorn

@ -6,8 +6,8 @@ import optparse
import zipfile import zipfile
import urllib.request as urlreq import urllib.request as urlreq
user_agent = 'python:riddle:3.0 (by u/Trivernis)' user_agent = 'python:riddle:3.0 (by u/Trivernis)' # the reddit api user-agent
img_ext = ['jpg', 'jpeg', 'png', 'svg', 'gif'] img_ext = ['jpg', 'jpeg', 'png'] # default used extensions to filter for images
def assert_dir_exist(dirpath): def assert_dir_exist(dirpath):
@ -40,6 +40,9 @@ def download_file(url: str, dest: str):
class ProgressBar: class 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
@ -99,7 +102,7 @@ def get_images(reddit_client: praw.Reddit, subreddit: str, limit: int):
:return: list of images :return: list of images
""" """
print('[~] Fetching images for %s...' % subreddit) print('[~] Fetching images for %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)] # fetches hot images
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]
@ -114,8 +117,8 @@ def download_images(images: list, dl_dir: str):
print('[~] Downloading %s images to %s' % (imgcount, dl_dir)) print('[~] Downloading %s images to %s' % (imgcount, dl_dir))
pb = ProgressBar(total=imgcount, prefix='[~] Downloading', suffix='Complete') pb = ProgressBar(total=imgcount, prefix='[~] Downloading', suffix='Complete')
assert_dir_exist(dl_dir) assert_dir_exist(dl_dir)
for img in images: for img in images: # download each image if it doesn't exist
pb.tick() pb.tick()
imgname = img.split('/')[-1] imgname = img.split('/')[-1]
name = os.path.join(dl_dir, imgname) name = os.path.join(dl_dir, imgname)
@ -132,10 +135,13 @@ def compress_folder(folder: str, zip_fname: str):
""" """
print('[~] Compressing folder...') print('[~] Compressing folder...')
mode = 'w' mode = 'w'
if os.path.isfile(zip_fname):
if os.path.isfile(zip_fname): # append to the zipfile if it already exists
mode = 'a' mode = 'a'
zfile = zipfile.ZipFile(zip_fname, mode) zfile = zipfile.ZipFile(zip_fname, mode)
for _, _, files in os.walk(folder):
for _, _, files in os.walk(folder): # add all files of the folder to the zipfile
for file in files: for file in files:
zfile.write(os.path.join(folder, file), file) zfile.write(os.path.join(folder, file), file)
zfile.close() zfile.close()
@ -144,13 +150,13 @@ def compress_folder(folder: str, zip_fname: str):
def main(): def main():
options, subreddits = parser_init() options, subreddits = parser_init()
with open('config.yaml', 'r') as file: with open('config.yaml', 'r') as file: # loads the config.yaml file
try: try:
settings = yaml.safe_load(file) settings = yaml.safe_load(file)
except yaml.YAMLError as err: except yaml.YAMLError as err:
print(err) print(err)
if settings: if settings:
if 'image-extensions' in settings: if 'image-extensions' in settings: # uses image extensions specified in config.yaml fallback to default
global img_ext global img_ext
img_ext = settings['image-extensions'] img_ext = settings['image-extensions']
credentials = settings['credentials'] credentials = settings['credentials']
@ -162,9 +168,9 @@ def main():
for subreddit in subreddits: for subreddit in subreddits:
dldest = subreddit dldest = subreddit
if options.output: if options.output:
dldest = options.output dldest = options.output # uses the -o output destination instead of a folder with the subreddit name
images = get_images(client, subreddit, limit=options.count) images = get_images(client, subreddit, limit=options.count)
if options.zip: if options.zip: # downloads to a cache-folder first before compressing it to zip
download_images(images, '.cache') download_images(images, '.cache')
compress_folder('.cache', dldest+'.zip') compress_folder('.cache', dldest+'.zip')
shutil.rmtree('.cache') shutil.rmtree('.cache')
@ -173,4 +179,5 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
print('\n--- riddle.py reddit-downloader by u/Trivernis ---\n')
main() main()

Loading…
Cancel
Save