Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
authorizer = readonly_authorizer(requestor=requestor)
session_instance.request.reset_mock()
# Fail on subsequent request
exception = ConnectionError()
session_instance.request.side_effect = exception
expected = (
"prawcore",
"WARNING",
"Retrying due to ConnectionError() status: GET "
"https://oauth.reddit.com/",
)
with LogCapture(level=logging.WARNING) as log_capture:
with self.assertRaises(RequestException) as context_manager:
prawcore.Session(authorizer).request("GET", "/")
log_capture.check(expected, expected)
self.assertIsInstance(context_manager.exception, RequestException)
self.assertIs(exception, context_manager.exception.original_exception)
self.assertEqual(3, session_instance.request.call_count)
with open(self.processed_submissions_file, 'a') as file:
file.write(submission.id + '\n')
except IOError as e:
logging.error('[submissions] IO error while writing to %s: %s', self.processed_submissions_file, e)
if len(self.processed_submissions) > 600:
logging.info('[submissions] Pruning %s to 500 submissions', self.processed_submissions_file)
try:
with open(self.processed_submissions_file, 'w') as file:
for submission in self.processed_submissions[-500:]:
file.write(submission + '\n')
self.processed_submissions = self.processed_submissions[-500:]
except IOError as e:
logging.error('[submissions] IO error while writing to %s: %s', self.processed_submissions_file, e)
except (praw.exceptions.APIException,
praw.exceptions.ClientException,
prawcore.exceptions.RequestException) as e:
logging.warning('[submissions] Got an exception: %s', e)
logging.warning('[submissions] Will go to sleep for %d seconds', self.sleeptime)
time.sleep(self.sleeptime)
except KeyboardInterrupt:
logging.critical('[submissions] Bot has been killed by keyboard interrupt. Exiting')
sys.exit(0)
r = Reddit('bot_archiver')
configure_logging(config, log_name='archiver.log')
tor = configure_tor(r, config)
initialize(tor, config)
logging.info('Initialization complete.')
archive = r.subreddit('ToR_Archive')
try:
while True:
try:
run(tor, config, archive)
time.sleep(300) # 5 minutes
except (
prawcore.exceptions.RequestException,
prawcore.exceptions.ServerError,
prawcore.exceptions.Forbidden
) as e:
logging.warning(
'{} - Issue communicating with Reddit. Sleeping for 60s!'
''.format(e)
)
time.sleep(60)
except KeyboardInterrupt:
logging.info('Received keyboard interrupt! Shutting down!')
sys.exit(0)
except Exception as e:
explode_gracefully('ToR_archivist', e, tor)
try:
number_of_loops = 0
# primary loop
while True:
check_inbox()
for sub in Context.subreddits_to_check:
check_submissions(sub, Context)
set_meta_flair_on_other_posts(tor, Context)
number_of_loops += 1
if number_of_loops > 9:
# reload configuration every ten loops
initialize(tor)
number_of_loops = 0
if Context.debug_sleep:
time.sleep(60)
except prawcore.exceptions.RequestException as e:
logging.error(e)
logging.error(
'PRAW encountered an error communicating with Reddit.'
)
logging.error(
'Sleeping for 60 seconds and trying program loop again.'
)
time.sleep(60)
except KeyboardInterrupt:
logging.error('User triggered shutdown. Shutting down.')
sys.exit(0)
except Exception as e:
# try to raise one last flag as it goes down
tor.message('I BROKE', traceback.format_exc())
item.mark_read()
for item in subreddit_stream:
if item is None:
break
inbox_item = InboxItem(item)
if isinstance(item, Submission):
await check_submission_item(inbox_item)
else:
item.mark_read()
except KeyboardInterrupt:
logger.info('Exiting...')
break
except (RequestException, ResponseException, ServerError) as e:
handle_bad_request(bad_requests, inbox_item, e)
except Exception as e:
if config.is_testing_environ:
raise e
if isinstance(e, Error):
logger.warning(e, inbox_item=inbox_item)
else:
logger.exception(e, inbox_item=inbox_item)
args = parser.parse_args()
logging.debug('Initializing bot')
with open('subreddits.json') as subreddits_file:
sub = '+'.join(json.load(subreddits_file))
bot = TitleToImageBot(sub)
logging.info('Bot initialized, processing the last %s submissions/messages every %s seconds',
args.limit, args.interval)
while True:
try:
logging.debug('Running bot')
bot.run(args.limit)
logging.debug('Bot finished, restarting in %s seconds', args.interval)
except (requests.exceptions.ReadTimeout,
requests.exceptions.ConnectionError,
ResponseException,
RequestException):
logging.error('Reddit api timed out, restarting')
continue
time.sleep(args.interval)
print("unflairing... responded to: " + msg.author.name)
msg.reply("Flair removed." + footer)
elif expired:
msg.mark_read()
title_url = msg.submission.url
is_expired = check_price(msg.submission, title_url)
if is_expired:
msg.submission.mod.flair(text='Deal Expired', css_class='expired')
print("flairing... responded to: " + msg.author.name)
msg.reply("Deal marked as expired. Reply with \"oops\" if this is incorrect." + footer)
else:
print("not expired... responded to: " + msg.author.name)
msg.reply("This still appears to be a deal, not marked as expired. The deal is probably not availible in your region. Please avoid marking deals as expired unless you are 100% sure they are." + footer)
except AttributeError:
print("error checking comment by: " + msg.author.name)
except (prawcore.exceptions.RequestException, prawcore.exceptions.ResponseException):
print ("Error connecting to reddit servers. Retrying in 5 minutes...")
time.sleep(300)
except praw.exceptions.APIException:
print ("rate limited, wait 5 seconds")
time.sleep(5)
async def _start_service_feed(self):
while not self.should_stop and self.connect_attempts < self.MAX_CONNECTION_ATTEMPTS:
try:
await self._start_listener()
except RequestException:
# probably a connexion loss, try again
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
time.sleep(sleep_seconds)
except (praw.exceptions.PRAWException, prawcore.exceptions.PrawcoreException) as e:
if isinstance(e, praw.exceptions.APIException):
if e.error_type == 'RATELIMIT':
logger.info('Exception: ratelimit exceeded: {}'.format(e.message))
time.sleep(11*60)
else:
logger.warning('Exception: unhandled PRAW APIException exception:', exc_info=True)
elif isinstance(e, prawcore.exceptions.ResponseException):
logger.info('Exception: ResponseException: {}'.format(e.response))
time.sleep(5)
elif isinstance(e, prawcore.exceptions.RequestException):
logger.info('Exception: RequestException: {}'.format(e.original_exception))
time.sleep(5)
else:
logger.warning('Exception: unhandled PRAW exception:', exc_info=True)
except Exception:
logger.error('Exception: unhandled exception:', exc_info=True)