Skip to content

Commit

Permalink
Avoiding timeout on requests
Browse files Browse the repository at this point in the history
  • Loading branch information
iu2frl authored Nov 3, 2023
1 parent 441487c commit e69af74
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions frlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import requests
import xml.dom.minidom
import emoji
import requests

# Specify logging level
logging.basicConfig(level=logging.DEBUG)
Expand Down Expand Up @@ -168,10 +169,18 @@ def parseNews(urlsList: list[str]) -> list[newsFromFeed]:
fetchFeed = []
for url in urlsList:
logging.debug("Retrieving feed at [" + url + "]")
r: requests.Response()
try:
fetchFeed.append(feedparser.parse(url)["entries"])
r = requests.get(url, timeout=3)
except Exception as retExc:
logging.error("Cannot retrieve feed at [" + url + "]. Error message: " + str(retExc))
logging.error("Cannot download feed from [" + url + "]. Error message: " + str(retExc))
if r.status_code == 200:
try:
fetchFeed.append(feedparser.parse(r.content)["entries"])
except Exception as retExc:
logging.error("Cannot parse feed from [" + url + "]. Error message: " + str(retExc))
else:
logging.warning("Got error code [" + str(r.status_code) + "] while retrieving content at [" + url + "]")
feedsList = [item for feed in fetchFeed for item in feed]
# Prepare list of news
newsList: list[newsFromFeed] = []
Expand Down Expand Up @@ -663,4 +672,4 @@ def HandleSqliteBackup(inputMessage: telebot.types.Message):
SchedulerLoop()
else:
Main()


0 comments on commit e69af74

Please sign in to comment.