def telegram_posts_sender(self):
while True:
time_to_wait = (self.wait_seconds // 3) + 1
posts_to_sent = Crawler.get_post_to_send()
grouped_posts = groupby(posts_to_sent, key=lambda x: x.to_send_id)
pending_msgs_to_send = False
for chat_id, posts in grouped_posts:
pending_msgs_to_send = True
posts = list(posts)[:self.post_chunk]
if settings.DEBUG:
print('Sending {} new posts to chat {}'.format(len(posts), chat_id))
for post in posts:
try:
self.telegram.sendMessage(chat_id=chat_id, text=post.description)
if post.image:
try:
self.telegram.sendPhoto(chat_id=chat_id, photo=post.image)
except BadRequest:
self.send_error('ERROR sending picture to {}'.format(post))
post.status = 'SENT'
except RetryAfter as e:
# Flood control exceeded. Retry in 175 seconds
self.send_error('RetryAfter error, waiting {} seconds'.format(e.retry_after))
time_to_wait = max(e.retry_after, time_to_wait)
post.status = 'ERROR'
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
error_stack = 'Send_updates ERROR: {}\n'.format(e)
error_stack += "".join(traceback.format_exception(exc_type, exc_value, exc_traceback))
self.send_error(error_stack)
post.status = 'ERROR'
Crawler.save_posts(posts)
if pending_msgs_to_send:
sleep_time = 3
else:
sleep_time = time_to_wait
gevent.sleep(sleep_time)
telegram_writer.py 文件源码
python
阅读 25
收藏 0
点赞 0
评论 0
评论列表
文章目录