Added nsfw filter

- defaults to exclude nsfw results
- added cli-option --nsfw to include nsfw results
pull/1/head
Trivernis 6 years ago
parent 60a660be2f
commit fe8858c3ed

@ -49,6 +49,7 @@ Options:
The name of the output folder. If none is specified,
it's the subreddits name.
-z, --zip Stores the images in a zip file if true
-n, --nsfw If set nsfw-content is also downloaded.
```
### Example

@ -93,19 +93,24 @@ def parser_init():
parser.add_option('-z', '--zip', dest='zip',
action='store_true', default=False,
help='Stores the images in a zip file if true')
parser.add_option('-n', '--nsfw', dest='nsfw',
action='store_true', default=False,
help='If set nsfw-content is also downloaded.')
return parser.parse_args()
def get_images(reddit_client: praw.Reddit, subreddit: str, limit: int):
def get_images(reddit_client: praw.Reddit, subreddit: str, limit: int, nsfw: bool = False):
"""
Uses the reddit api to fetch all image posts
:param reddit_client: instance of the reddit client
:param subreddit: reddit subreddit name
:param limit: max images to download. if set to None the maximum fetchable amout is used.
:param nsfw: if set to true, nsfw-images won't be filtered
:return: list of images
"""
print('[~] Fetching images for r/%s...' % subreddit)
urls = [submission.url for submission in reddit_client.subreddit(subreddit).hot(limit=limit)] # fetches hot images
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
return [url for url in urls if url.split('.')[-1] in img_ext]
@ -180,7 +185,7 @@ def main():
dldest = subreddit
if 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, nsfw=options.nsfw)
if options.zip: # downloads to a cache-folder first before compressing it to zip
download_images(images, '.cache')
compress_folder('.cache', dldest+'.zip')

Loading…
Cancel
Save