Usage
To extract only the parts that you will need, use:
from reddit_photo_aggregator import SubReddit, ImageURLValidator, SortBy
Alternatively, import just the package:
import reddit_photo_aggregator
Set the image host whitelist using .set_image_host_whitelist()
ImageURLValidator.set_image_host_whitelist([
'i.redd.it',
'i.imgur.com',
])
Set the file extension whitelist using .set_image_extension_whitelist()
ImageURLValidator.set_image_extension_whitelist([
'png',
'jpeg',
'jpg'
])
Create a SubReddit
object with the sub-reddit name that you want to aggregate images from
sub = SubReddit('r/LandscapePhotography')
You can then discover images, this can be sorted using the SortBy
class, this will also give you the amount of images found and the sub.loaded_images
becomes filled out
amount = sub.discover_images(SortBy.hot)
print(amount)
print(sub.loaded_images)
Save the images to an /images
directory.
for image in sub.loaded_images:
print(f'Saving to {image.filename}')
image.save(to='./images')
Full Example
from src import SubReddit, ImageURLValidator, SortBy
ImageURLValidator.set_image_host_whitelist([
'i.redd.it',
'i.imgur.com',
])
ImageURLValidator.set_image_extension_whitelist([
'png',
'jpeg',
'jpg'
])
sub = SubReddit('r/LandscapePhotography')
amount = sub.discover_images(SortBy.hot)
print(amount)
for image in sub.loaded_images:
print(f'Saving to {image.filename}')
image.save(to='./images')