Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.config = yaml.load(open('config.yaml'))
#Load banlist
self.log("Loading banlists...")
sys.stdout.flush()
bottiquette = self.r.subreddit('Bottiquette').wiki['robots_txt_json']
banlist = json.loads(bottiquette.content_md)
btqban = (banlist['disallowed'] +\
banlist['posts-only'] +\
banlist['permission'])
try:
mybans = self.get_wiki('conf/blacklist')
mybans = [line for line in mybans.content_md.split('\n')\
if not (line.strip() == '' or line.startswith('#'))]
except prawcore.exceptions.ResponseException:
self.log("Couldn't load bot-specific blacklist")
mybans = []
self.bans = [x.strip().lower() for x in (btqban + mybans)]
self.log("Ignoring subreddits: %s",(', '.join(self.bans)))
q.exit_queue()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
while True:
try:
for i in q.get_jobs():
print(i, i.origin_host, i.origin_id)
result = process_comment(reddit, original_context=CommentContext.from_json(reddit, dict(i.context)))
if result == SUCCESS or result == USER_FAILURE:
q.remove_job(i)
time.sleep(consts.sleep_time * failure_counter / 4)
except prawcore.exceptions.ResponseException as e: # Something funky happened
q.exit_queue()
print("Did a comment go missing?", e, vars(e))
time.sleep(consts.sleep_time)
except prawcore.exceptions.RequestException: # Unable to connect to Reddit
q.exit_queue()
print("Unable to connect to Reddit, is the internet down?")
time.sleep(consts.sleep_time * 2)
except KeyboardInterrupt:
q.exit_queue()
print("Exiting...")
break
except OperationalError:
print("Unable to connect to database")
async def main(config):
async with aiohttp.ClientSession() as session:
fpl_bot = FPLBot(config, session)
while True:
try:
fpl_bot.run()
except prawcore.exceptions.ServerError as http_error:
logger.error(http_error)
time.sleep(120)
except prawcore.exceptions.ResponseException as response_error:
logger.error(response_error)
time.sleep(120)
except Exception as error:
logger.error(error)
time.sleep(360)
print("You can now safely end the process")
break
reddit.inbox.mark_read(mark_read)
mark_read.clear()
if failure:
print("An upload failed, extending wait")
# failure_counter += 1
else:
failure_counter = 1
if q:
q.clean()
time.sleep(consts.sleep_time * failure_counter)
except prawcore.exceptions.ResponseException as e: # Something funky happened
print("Did a comment go missing?", e, vars(e))
time.sleep(consts.sleep_time)
except prawcore.exceptions.RequestException: # Unable to connect to Reddit
print("Unable to connect to Reddit, is the internet down?")
time.sleep(consts.sleep_time * 2)
except KeyboardInterrupt:
reddit.inbox.mark_read(mark_read)
print("Exiting...")
break
except OperationalError:
print("Unable to connect to database")
if db_connected:
reddit.redditor(operator).message("GRB Error", "The bot was unable to connect to the database. If "
submission_id = REPatterns.reddit_submission.findall(r.url)
url = None
audio = False
if not submission_id:
print("Deleted?")
return False
elif submission_id[0][2]:
submission = self.host.ghm.reddit.submission(id=submission_id[0][2])
try:
if submission.is_video:
if submission.media:
url = submission.media['reddit_video']['fallback_url']
else:
print("Submission is video but there is no media data")
return False
except ResponseException as e:
print("Video is inaccessible, likely deleted")
return False
else: # Maybe it was deleted?
print("Deleted?")
return False
r = requests.get(url)
file = BytesIO(r.content)
r = requests.get("https://v.redd.it/{}/DASH_audio.mp4".format(self.id), headers=headers)
if r.status_code == 200:
file = concat(file, BytesIO(r.content))
audio = True
self.type = consts.MP4
time.sleep(self._SLEEPING_TIME_BEFORE_RECONNECT_ATTEMPT_SEC)
except InvalidToken as e:
# expired, try again
self.logger.exception(e, True, f"Error when receiving Reddit feed: '{e}'")
self.logger.info(f"Try to continue after {self._SLEEPING_TIME_BEFORE_RECONNECT_ATTEMPT_SEC} seconds.")
time.sleep(self._SLEEPING_TIME_BEFORE_RECONNECT_ATTEMPT_SEC)
except ServerError as e:
# server error, try again
self.logger.exception(e, True, "Error when receiving Reddit feed: '{e}'")
self.logger.info(f"Try to continue after {self._SLEEPING_TIME_BEFORE_RECONNECT_ATTEMPT_SEC} seconds.")
time.sleep(self._SLEEPING_TIME_BEFORE_RECONNECT_ATTEMPT_SEC)
except OAuthException as e:
self.logger.exception(e, True, f"Error when receiving Reddit feed: '{e}' this may mean that reddit "
f"login info in config.json are wrong")
self.keep_running = False
except ResponseException as e:
message_complement = "this may mean that reddit login info in config.json are invalid." \
if not self.credentials_ok else \
f"Try to continue after {self._SLEEPING_TIME_BEFORE_RECONNECT_ATTEMPT_SEC} seconds."
self.logger.exception(e, True,
f"Error when receiving Reddit feed: '{e}' this may mean {message_complement}")
if not self.credentials_ok:
self.connect_attempts += 1
else:
self.connect_attempts += 0.1
time.sleep(self._SLEEPING_TIME_BEFORE_RECONNECT_ATTEMPT_SEC)
except Exception as e:
self.logger.error(f"Error when receiving Reddit feed: '{e}'")
self.logger.exception(e, True, f"Error when receiving Reddit feed: '{e}'")
self.keep_running = False
return False
self.response = response
resp_dict = self.response.json() # assumes valid JSON
self.message = resp_dict.get("message", "")
self.reason = resp_dict.get("reason", "")
self.special_errors = resp_dict.get("special_errors", [])
PrawcoreException.__init__(
self, "Special error {!r}".format(self.message)
)
class TooLarge(ResponseException):
"""Indicate that the request data exceeds the allowed limit."""
class UnavailableForLegalReasons(ResponseException):
"""Indicate that the requested URL is unavilable due to legal reasons."""
def _post(self, url, success_status=codes["ok"], **data):
response = self._requestor.request(
"post",
url,
auth=self._auth(),
data=sorted(data.items()),
headers={"Connection": "close"},
)
if response.status_code != success_status:
raise ResponseException(response)
return response
:param response: A requests.response instance containing a message
and a list of special errors.
"""
self.response = response
resp_dict = self.response.json() # assumes valid JSON
self.message = resp_dict.get("message", "")
self.reason = resp_dict.get("reason", "")
self.special_errors = resp_dict.get("special_errors", [])
PrawcoreException.__init__(
self, "Special error {!r}".format(self.message)
)
class TooLarge(ResponseException):
"""Indicate that the request data exceeds the allowed limit."""
class UnavailableForLegalReasons(ResponseException):
"""Indicate that the requested URL is unavilable due to legal reasons."""