From 872b451012afddb43749c7c2ea8886296924454a Mon Sep 17 00:00:00 2001 From: geirawsm Date: Mon, 26 Aug 2024 15:12:31 +0200 Subject: [PATCH] Better handling of HTTP errors --- sausage_bot/cogs/rss.py | 5 +++++ sausage_bot/util/feeds_core.py | 20 ++++++++------------ sausage_bot/util/net_io.py | 11 ++++++----- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/sausage_bot/cogs/rss.py b/sausage_bot/cogs/rss.py index 69b20fe..040c26f 100755 --- a/sausage_bot/cogs/rss.py +++ b/sausage_bot/cogs/rss.py @@ -158,6 +158,11 @@ async def rss_add( 'Urlen er ikke en RSS/XML feed', ephemeral=True ) return + elif isinstance(valid_feed, int): + await interaction.followup.send( + f'Urlen gir feilkode: {valid_feed}', ephemeral=True + ) + return log.verbose('Adding feed to db') await feeds_core.add_to_feed_db( 'spotify', str(feed_name), str(feed_link), channel.name, AUTHOR diff --git a/sausage_bot/util/feeds_core.py b/sausage_bot/util/feeds_core.py index c17eb6b..82c91d3 100755 --- a/sausage_bot/util/feeds_core.py +++ b/sausage_bot/util/feeds_core.py @@ -26,17 +26,6 @@ async def check_if_feed_name_exist(feed_name): return True -async def check_url_validity(url): - 'Make sure that `url` is a valid link' - log.verbose(f'Checking `{url}`') - req = await net_io.get_link(url) - log.debug(f'req is ({type(req)})') - if req is None: - return False - else: - return True - - async def check_feed_validity(URL): 'Make sure that `URL` is a valid link with feed items' sample_item = None @@ -46,6 +35,8 @@ async def check_feed_validity(URL): if req is None: log.verbose('Returned None') return None + elif isinstance(req, int): + return req if 'open.spotify.com/show/' in URL: log.verbose('Discovered Spotify branded link') sample_item = await net_io.check_spotify_podcast(URL) @@ -164,6 +155,9 @@ async def add_to_feed_db( if test_link is None: log.verbose(f'`test_link` is None') return None + elif isinstance(test_link, int): + log.verbose(f'`test_link` returns code {test_link}') + return test_link date_now = datetime_handling.get_dt(format='datetime') if feed_type in ['rss', 'spotify']: await db_helper.insert_many_some( @@ -263,6 +257,8 @@ async def get_feed_links(feed_type, feed_info): ) log.debug(f'Got {len(links_out)} items from `get_items_from_rss`') return links_out + elif isinstance(req, int): + return req else: return @@ -471,7 +467,7 @@ async def review_feeds_status(feed_type: str = None): db_updates['status_url_counter'].append( ('uuid', UUID, 0) ) - elif not is_valid_feed: + elif not is_valid_feed or isinstance(is_valid_feed, int): log.debug( f'`URL_STATUS_COUNTER` ({URL_STATUS_COUNTER}) vs ' f'`envs.FEEDS_URL_ERROR_LIMIT` ({envs.FEEDS_URL_ERROR_LIMIT})' diff --git a/sausage_bot/util/net_io.py b/sausage_bot/util/net_io.py index 2f7dea6..7ed6dc9 100755 --- a/sausage_bot/util/net_io.py +++ b/sausage_bot/util/net_io.py @@ -29,12 +29,13 @@ async def get_link(url): 'Get contents of requests object from a `url`' content_out = None + url_status = 0 if type(url) is not str: log.error(envs.RSS_INVALID_URL.format(url)) return None - if re.search(r'^http(s)?', url): + if re.search(r'^http(s)?\:', url): log.debug('Found scheme in url') - elif re.match(r'^((http:\/\/|^https:\/\/))?((www\.))?', url) is not None: + elif re.match(r'^((http\:\/\/|^https\:\/\/))?((www\.))?', url) is not None: log.debug('Did not found scheme, adding') url = f'https://{url}' try: @@ -42,16 +43,16 @@ async def get_link(url): session = aiohttp.ClientSession() async with session.get(url) as resp: url_status = resp.status - content_out = await resp.text() log.debug(f'Got status: {url_status}') + content_out = await resp.text() log.debug(f'Got content_out: {content_out[0:500]}...') await session.close() except Exception as e: - log.error(f'Error when getting `url`: {e}') + log.error(f'Error when getting `url`:({url_status}) {e}') return None if 399 < int(url_status) < 600: log.error(f'Got error code {url_status}') - return None + return int(url_status) if content_out is None: return None else: