-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
140 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
praw.ini | ||
*.txt | ||
*.jpg | ||
*.png | ||
*.log | ||
*.pyc | ||
__pycache__ | ||
img/ | ||
praw.ini | ||
*.txt | ||
*.jpg | ||
*.png | ||
*.log | ||
*.pyc | ||
__pycache__ | ||
img/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# RedditBot | ||
This Reddit bot downloads the top images of the day from any subreddit. | ||
|
||
The bot implements the Python Reddit API Wrapper (PRAW). Found at https://praw.readthedocs.io/en/latest/ | ||
|
||
Simply install PRAW alongside Python 3 and create an a praw.ini file with valid bot authentication details. An example praw.ini file has been included to simplify | ||
the process. | ||
# RedditBot | ||
This Reddit bot downloads the top images of the day from any subreddit. | ||
|
||
The bot implements the Python Reddit API Wrapper (PRAW). Found at https://praw.readthedocs.io/en/latest/ | ||
|
||
Simply install PRAW alongside Python 3 and create an a praw.ini file with valid bot authentication details. An example praw.ini file has been included to simplify | ||
the process. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
[DEFAULTS] | ||
# Number of Images. Default = 25 | ||
image_num = 25 | ||
|
||
# Where to output images. Default = ./img/ | ||
output_location = ./img/ | ||
|
||
# Name of Reddit bot. | ||
bot = bot1 | ||
|
||
# Name of subreddit to get from. | ||
subreddit = pics | ||
|
||
# Time period to gather images. Default = day | ||
# Options: all, day, hour, month, week, year | ||
time = all | ||
|
||
# Location of log file. Default = ./bot.log | ||
log_location = ./bot.log | ||
[DEFAULTS] | ||
# Number of Images. Default = 25 | ||
image_num = 25 | ||
|
||
# Where to output images. Default = ./img/ | ||
output_location = ./img/ | ||
|
||
# Name of Reddit bot. | ||
bot = bot1 | ||
|
||
# Name of subreddit to get from. | ||
subreddit = pics | ||
|
||
# Time period to gather images. Default = day | ||
# Options: all, day, hour, month, week, year | ||
time = all | ||
|
||
# Location of log file. Default = ./bot.log | ||
log_location = ./bot.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
import re | ||
import pathlib | ||
import urllib.request | ||
import logging | ||
|
||
def getImages(subreddit, time_period, image_num, output_location): | ||
logger = logging.getLogger('prawcore') | ||
# Check if is an imgur link without a file extenstion | ||
# (Example: https://imgur.com/fa33.jpg matches) | ||
is_imgur_no_ext = re.compile('^https?://(www\.)?imgur.com/[a-zA-z0-9]+$') | ||
|
||
# Check if the link is an imgur album. | ||
is_imgur_album = re.compile('^https?://(www\.)?imgur.com/a/') | ||
|
||
# Create an image folder if it doesn't already exist | ||
pathlib.Path(output_location).mkdir(parents=True, exist_ok=True) | ||
|
||
# Get 'images_to_get' submissions and download the image from each | ||
for submission in subreddit.top(time_period, limit=image_num): | ||
if not submission.is_self: # Ignore self posts | ||
if is_imgur_album.match(submission.url): | ||
logger.info("Ignored Imgur Album: %s", submission.url) | ||
if is_imgur_no_ext.match(submission.url): | ||
submission.url = submission.url + '.jpg' | ||
try: | ||
# This line grabs the image from the url | ||
urllib.request.urlretrieve(submission.url, | ||
output_location + submission.id + '.jpg') | ||
except Exception as e: | ||
logger.error('%s at URL: %s', e, submission.url) | ||
import re | ||
import pathlib | ||
import urllib.request | ||
import logging | ||
|
||
def getImages(subreddit, time_period, image_num, output_location): | ||
logger = logging.getLogger('prawcore') | ||
# Check if is an imgur link without a file extenstion | ||
# (Example: https://imgur.com/fa33.jpg matches) | ||
is_imgur_no_ext = re.compile('^https?://(www\.)?imgur.com/[a-zA-z0-9]+$') | ||
|
||
# Check if the link is an imgur album. | ||
is_imgur_album = re.compile('^https?://(www\.)?imgur.com/a/') | ||
|
||
# Create an image folder if it doesn't already exist | ||
pathlib.Path(output_location).mkdir(parents=True, exist_ok=True) | ||
|
||
# Get 'images_to_get' submissions and download the image from each | ||
for submission in subreddit.top(time_period, limit=image_num): | ||
if not submission.is_self: # Ignore self posts | ||
if is_imgur_album.match(submission.url): | ||
logger.info("Ignored Imgur Album: %s", submission.url) | ||
if is_imgur_no_ext.match(submission.url): | ||
submission.url = submission.url + '.jpg' | ||
try: | ||
# This line grabs the image from the url | ||
urllib.request.urlretrieve(submission.url, | ||
output_location + submission.id + '.jpg') | ||
except Exception as e: | ||
logger.error('%s at URL: %s', e, submission.url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import logging | ||
|
||
def setup_logging(location): | ||
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(module)s - %(message)s') | ||
|
||
hdlr = logging.FileHandler(location) | ||
hdlr.setFormatter(formatter) | ||
|
||
logger = logging.getLogger('prawcore') | ||
logger.addHandler(hdlr) | ||
logger.setLevel(logging.DEBUG) | ||
return logger | ||
import logging | ||
|
||
def setup_logging(location): | ||
formatter = logging.Formatter(fmt='%(asctime)s - %(levelname)s - %(module)s - %(message)s') | ||
|
||
hdlr = logging.FileHandler(location) | ||
hdlr.setFormatter(formatter) | ||
|
||
logger = logging.getLogger('prawcore') | ||
logger.addHandler(hdlr) | ||
logger.setLevel(logging.DEBUG) | ||
return logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
import downloadimages | ||
import praw | ||
import requests | ||
import log | ||
import configparser | ||
from datetime import timedelta | ||
from datetime import datetime | ||
|
||
def main(): | ||
print('Started.') | ||
config = configparser.ConfigParser() | ||
config.read('config.cfg') | ||
|
||
image_num = int(config.get('DEFAULTS', 'image_num')) | ||
output_location = config.get('DEFAULTS','output_location') | ||
bot_name = config.get('DEFAULTS','bot') | ||
subreddit_name = config.get('DEFAULTS','subreddit') | ||
time_period = config.get('DEFAULTS','time') | ||
log_location = config.get('DEFAULTS', 'log_location') | ||
|
||
logger = log.setup_logging(log_location) | ||
start = datetime.now() | ||
logger.debug('Starting...') | ||
|
||
# Setup logging and establish bot connection with Reddit | ||
reddit = praw.Reddit(bot_name) | ||
subreddit = reddit.subreddit(subreddit_name) | ||
|
||
print('Getting images... please wait...') | ||
downloadimages.getImages(subreddit, time_period, image_num, output_location) | ||
|
||
|
||
end = datetime.now() | ||
logger.debug('Completed. Time taken: %s', str(end - start)) | ||
print('Completed! The images are stored at \'' + output_location + '\'') | ||
|
||
main() | ||
input('Press ENTER to exit') | ||
import downloadimages | ||
import praw | ||
import requests | ||
import log | ||
import configparser | ||
from datetime import timedelta | ||
from datetime import datetime | ||
|
||
def main(): | ||
print('Started.') | ||
config = configparser.ConfigParser() | ||
config.read('config.cfg') | ||
|
||
image_num = int(config.get('DEFAULTS', 'image_num')) | ||
output_location = config.get('DEFAULTS','output_location') | ||
bot_name = config.get('DEFAULTS','bot') | ||
subreddit_name = config.get('DEFAULTS','subreddit') | ||
time_period = config.get('DEFAULTS','time') | ||
log_location = config.get('DEFAULTS', 'log_location') | ||
|
||
logger = log.setup_logging(log_location) | ||
start = datetime.now() | ||
logger.debug('Starting...') | ||
|
||
# Setup logging and establish bot connection with Reddit | ||
reddit = praw.Reddit(bot_name) | ||
subreddit = reddit.subreddit(subreddit_name) | ||
|
||
print('Getting images... please wait...') | ||
downloadimages.getImages(subreddit, time_period, image_num, output_location) | ||
|
||
|
||
end = datetime.now() | ||
logger.debug('Completed. Time taken: %s', str(end - start)) | ||
print('Completed! The images are stored at \'' + output_location + '\'') | ||
|
||
main() | ||
input('Press ENTER to exit') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
[DEFAULT] | ||
# A boolean to indicate whether or not to check for package updates. | ||
check_for_updates=True | ||
|
||
# Object to kind mappings | ||
comment_kind=t1 | ||
message_kind=t4 | ||
redditor_kind=t2 | ||
submission_kind=t3 | ||
subreddit_kind=t5 | ||
|
||
# The URL prefix for OAuth-related requests. | ||
oauth_url=https://oauth.reddit.com | ||
|
||
# The URL prefix for regular requests. | ||
reddit_url=https://www.reddit.com | ||
|
||
# The URL prefix for short URLs. | ||
short_url=https://redd.it | ||
|
||
[bot1] | ||
client_id= #your_bot_id_here | ||
client_secret= #your_bot_secret_here | ||
password= #(optional) | ||
username= #(optional) | ||
user_agent= # Name this whatever you want | ||
[DEFAULT] | ||
# A boolean to indicate whether or not to check for package updates. | ||
check_for_updates=True | ||
|
||
# Object to kind mappings | ||
comment_kind=t1 | ||
message_kind=t4 | ||
redditor_kind=t2 | ||
submission_kind=t3 | ||
subreddit_kind=t5 | ||
|
||
# The URL prefix for OAuth-related requests. | ||
oauth_url=https://oauth.reddit.com | ||
|
||
# The URL prefix for regular requests. | ||
reddit_url=https://www.reddit.com | ||
|
||
# The URL prefix for short URLs. | ||
short_url=https://redd.it | ||
|
||
[bot1] | ||
client_id= #your_bot_id_here | ||
client_secret= #your_bot_secret_here | ||
password= #(optional) | ||
username= #(optional) | ||
user_agent= # Name this whatever you want |