Skip to content

Instantly share code, notes, and snippets.

@SmashT
Last active August 14, 2019 21:14
Show Gist options
  • Save SmashT/dcf8faf0889e8e32c98f7a09146d86c7 to your computer and use it in GitHub Desktop.
Save SmashT/dcf8faf0889e8e32c98f7a09146d86c7 to your computer and use it in GitHub Desktop.
Reddit Dev Flair Bot
# Subreddit to check
subreddit = 'subreddit'
# Mod flair name
mod_flair = 'Developer'
# Credentials, subreddit management required
user_agent = 'UserAgent'
client_id = 'ClientID'
client_secret = 'ClientSecret'
username = 'Username'
password = 'Password'
import praw
import config
# Gets the correct link flair, None if untagged
def get_flair_id(flair_name, flair_data):
if flair_name is None:
return next(x for x in flair_data
if x['flair_text_editable'])['flair_template_id']
for flair in flair_data:
if flair['flair_css_class'] == (flair_name + 'Dev'):
return flair['flair_template_id']
reddit = praw.Reddit(user_agent=config.user_agent,
client_id=config.client_id,
client_secret=config.client_secret,
username=config.username,
password=config.password)
# Bot running forever
while True:
# Post-already tagged
with open("tagged.txt", "r") as f:
posts_tagged = f.read()
posts_tagged = posts_tagged.split("\n")
posts_tagged = list(filter(None, posts_tagged))
# Goes to subbreddit
battalion1944 = reddit.subreddit(config.subreddit)
# Checks new submissions
for submission in battalion1944.new():
# Already tagged, ignore
if submission.id in posts_tagged:
continue
old_flair = submission.link_flair_text
choices = submission.flair.choices()
# Searches in comments
for comment in submission.comments:
author = comment.author
# Checks mod flair
if comment.author_flair_css_class == config.mod_flair:
print('Developer responded')
template_id = get_flair_id(old_flair, choices)
flair_text = 'Developer Response' if old_flair is not None else 'untaggedDev'
print(template_id)
print(flair_text)
# Changes the link flair
submission.flair.select(template_id, )
# Saves the submission
posts_tagged.append(submission.id)
print('Link flair changed')
# Write our updated list back to the file
with open("tagged.txt", "w") as f:
for post_id in posts_tagged:
f.write(post_id + "\n")
@SmashT
Copy link
Author

SmashT commented Jan 29, 2018

C:\Python36\Scripts\DevFlairBot>bot.py
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
six.raise_from(e, None)
File "", line 2, in raise_from
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 383, in _make_request
httplib_response = conn.getresponse()
File "C:\Python36\lib\http\client.py", line 1331, in getresponse
response.begin()
File "C:\Python36\lib\http\client.py", line 297, in begin
version, status, reason = self._read_status()
File "C:\Python36\lib\http\client.py", line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "C:\Python36\lib\socket.py", line 586, in readinto
return self._sock.recv_into(b)
File "C:\Python36\lib\ssl.py", line 1002, in recv_into
return self.read(nbytes, buffer)
File "C:\Python36\lib\ssl.py", line 865, in read
return self._sslobj.read(len, buffer)
File "C:\Python36\lib\ssl.py", line 625, in read
v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Python36\lib\site-packages\requests\adapters.py", line 440, in send
timeout=timeout
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Python36\lib\site-packages\urllib3\util\retry.py", line 357, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:\Python36\lib\site-packages\urllib3\packages\six.py", line 686, in reraise
raise value
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
chunked=chunked)
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 389, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 309, in _raise_timeout
raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='www.reddit.com', port=443): Read timed out. (read timeout=16.0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Python36\lib\site-packages\prawcore\requestor.py", line 47, in request
return self._http.request(*args, timeout=TIMEOUT, **kwargs)
File "C:\Python36\lib\site-packages\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python36\lib\site-packages\requests\sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "C:\Python36\lib\site-packages\requests\adapters.py", line 521, in send
raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='www.reddit.com', port=443): Read timed out. (read timeout=16.0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Python36\Scripts\DevFlairBot\bot.py", line 36, in
choices = submission.flair.choices()
File "C:\Python36\lib\site-packages\praw\models\reddit\submission.py", line 243, in choices
'link': self.submission.fullname})['choices']
File "C:\Python36\lib\site-packages\praw\reddit.py", line 431, in post
params=params)
File "C:\Python36\lib\site-packages\praw\reddit.py", line 472, in request
params=params)
File "C:\Python36\lib\site-packages\prawcore\sessions.py", line 181, in request
params=params, url=url)
File "C:\Python36\lib\site-packages\prawcore\sessions.py", line 112, in _request_with_retries
data, files, json, method, params, retries, url)
File "C:\Python36\lib\site-packages\prawcore\sessions.py", line 97, in _make_request
params=params)
File "C:\Python36\lib\site-packages\prawcore\rate_limit.py", line 32, in call
kwargs['headers'] = set_header_callback()
File "C:\Python36\lib\site-packages\prawcore\sessions.py", line 141, in _set_header_callback
self._authorizer.refresh()
File "C:\Python36\lib\site-packages\prawcore\auth.py", line 328, in refresh
password=self._password)
File "C:\Python36\lib\site-packages\prawcore\auth.py", line 138, in _request_token
response = self._authenticator._post(url, **data)
File "C:\Python36\lib\site-packages\prawcore\auth.py", line 29, in _post
data=sorted(data.items()))
File "C:\Python36\lib\site-packages\prawcore\requestor.py", line 49, in request
raise RequestException(exc, args, kwargs)
prawcore.exceptions.RequestException: error with request HTTPSConnectionPool(host='www.reddit.com', port=443): Read timed out. (read timeout=16.0)

C:\Python36\Scripts\DevFlairBot>pause
Press any key to continue . . .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment