Created
September 15, 2015 19:17
-
-
Save trlewis/bfb02937ec510020efc0 to your computer and use it in GitHub Desktop.
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
# run this first | |
import praw | |
from bot_config import * | |
r = praw.Reddit('OAuth getter for wizards_to_imgur script') | |
r.set_oauth_app_info(client_id=reddit_client_id, | |
client_secret=reddit_client_secret, | |
redirect_uri=reddit_redirect_uri) | |
# get_authorize_url takes 3 params: | |
# 1) state: a string of your choice that represents this client | |
# 2) scope: the reddit scope(s) we ask permission for | |
# 3) refreshable: determines whether we can refresh the access_token which will allow permanent access | |
scope = 'identity read submit' | |
url = r.get_authorize_url(user_agent, scope, True) | |
import webbrowser | |
webbrowser.open(url) | |
# the GET value of the page you're redirected to will contain an access code, set it as | |
# the corresponding value of access_code in get_access_information.py |
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
# run this second | |
import praw | |
from bot_config import * | |
access_code = '<access code here>' | |
red = praw.Reddit(user_agent=user_agent) | |
red.set_oauth_app_info(client_id=reddit_client_id, | |
client_secret=reddit_client_secret, | |
redirect_uri=reddit_redirect_uri) | |
access_info = red.get_access_information(access_code) | |
with open('access_information.txt', 'w') as file: | |
file.write(str(access_info)) |
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
# this is the actual bot. after the access information is saved just run this file | |
# the following bash script (runbot.sh) will run this script once a minute until stopped. | |
import praw | |
import re | |
import os | |
import ast | |
from bot_config import * | |
from imgurpython import ImgurClient | |
replies_file_name = 'posts_replied_to.txt' | |
if not os.path.isfile(replies_file_name): | |
posts_replied_to = [] | |
else: | |
with open(replies_file_name, 'r') as file: | |
id_list = file.read() | |
id_list = id_list.split('\n') | |
posts_replied_to = [id for id in id_list if id is not None and id is not ''] | |
newly_replied_to = [] | |
reddit = praw.Reddit(user_agent=user_agent) | |
reddit.set_oauth_app_info(client_id=reddit_client_id, | |
client_secret=reddit_client_secret, | |
redirect_uri=reddit_redirect_uri) | |
with open('access_information.txt', 'r') as rf: | |
info = rf.read() | |
access_information = ast.literal_eval(info) | |
reddit.set_access_credentials(**access_information) | |
reddit.refresh_access_information(access_information['refresh_token']) | |
subreddit = reddit.get_subreddit('magictcg') | |
url_pattern = r'^https?:\/\/(www\.)?media\.wizards\.com\/.+\.(png|jpg)$' | |
imgur_client = ImgurClient(imgur_client_id, imgur_client_secret) | |
submissions = subreddit.get_new(limit=25) | |
for submission in submissions: | |
s = '{0}^ ({1}) {2} [{3}]'.format(submission.score, submission.id, submission.title, submission.url) | |
match = re.search(url_pattern, submission.url) | |
if match and submission.id not in posts_replied_to: | |
image = imgur_client.upload_from_url(submission.url, config=None, anon=True) | |
comment = '[{0}]({0})\n\n' \ | |
'This is bot rehosts images that might otherwise be blocked for people. ' \ | |
'For issues contact <my user account>' | |
comment = comment.format(image['link']) | |
submission.add_comment(comment) | |
newly_replied_to.append(submission.id) | |
print(s) | |
with open(replies_file_name, 'a') as file: | |
for post_id in newly_replied_to: | |
file.write(post_id + '\n') |
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
#!/bin/bash | |
while : | |
do | |
python3 magic_bot.py | |
sleep 1m | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment