From 0ced2d33e9375496a076586d734e3a1ffae42f6f Mon Sep 17 00:00:00 2001 From: NNTin Date: Sun, 16 Sep 2018 10:27:41 +0200 Subject: [PATCH] more error handling --- main.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 502e8639..1e4a78f6 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from tweepy.api import API from discord import Webhook, RequestsWebhookAdapter, Embed from time import gmtime, strftime +from time import sleep import discord import random import json @@ -10,6 +11,7 @@ import html import re from config import data_json +import urllib3 class StdOutListener(StreamListener): @@ -236,4 +238,31 @@ def on_warning(self, notice): stream = Stream(auth, l) print('Twitter stream started.') - stream.filter(follow=data_json['twitter_ids']) + while True: + try: + stream.filter(follow=data_json['twitter_ids']) + except urllib3.exceptions.ProtocolError as error: + print('---------Error---------') + print('This is probably caused by "Connection reset by peer." Ignore. Nothing you can do.') + print(error) + print('Sleeping for 10 seconds then continuing.') + sleep(10) + print('Twitter streaming continues.') + print('-----------------------') + except ConnectionResetError as error: + print('---------Error---------') + print('This is probably caused by "Connection reset by peer." Ignore. Nothing you can do.') + print(error) + print('Sleeping for 10 seconds then continuing.') + sleep(10) + print('Twitter streaming continues.') + print('-----------------------') + except Exception as error: + print('---------Error---------') + print('unknown error') + print("You've found an error. Please contact the owner (https://discord.gg/JV5eUB) " + "and send him what follows below:") + print(error) + print(data_json) + print('-----------------------') + break