Skip to content

Commit

Permalink
fixed tracked files
Browse files Browse the repository at this point in the history
  • Loading branch information
owmasch committed May 10, 2018
1 parent 66ff192 commit c27713e
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 169 deletions.
16 changes: 8 additions & 8 deletions .gitignore
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/
14 changes: 7 additions & 7 deletions README.md
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.
29 changes: 0 additions & 29 deletions bot.log

This file was deleted.

38 changes: 19 additions & 19 deletions config.cfg
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
60 changes: 30 additions & 30 deletions downloadimages.py
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)
24 changes: 12 additions & 12 deletions log.py
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
76 changes: 38 additions & 38 deletions main.py
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')
52 changes: 26 additions & 26 deletions praw_example.ini
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

0 comments on commit c27713e

Please sign in to comment.