diff options
author | Arjun Satarkar <me@arjunsatarkar.net> | 2023-08-04 16:39:08 +0000 |
---|---|---|
committer | Arjun Satarkar <me@arjunsatarkar.net> | 2023-08-04 16:39:08 +0000 |
commit | 8f5488436b90d85969abb8bdcbb0ecf14d8936bd (patch) | |
tree | 8fc1c4cb5201c344ea1a1586e19e1a50d4121f23 | |
parent | f8aa1606b8733f629762f54785660305d4eb636e (diff) | |
download | tagrss-8f5488436b90d85969abb8bdcbb0ecf14d8936bd.tar tagrss-8f5488436b90d85969abb8bdcbb0ecf14d8936bd.tar.gz tagrss-8f5488436b90d85969abb8bdcbb0ecf14d8936bd.zip |
Handle if a non-feed URL is given
-rw-r--r-- | README.md | 2 | ||||
-rwxr-xr-x | serve.py | 8 | ||||
-rw-r--r-- | tagrss.py | 10 |
3 files changed, 17 insertions, 3 deletions
@@ -15,8 +15,6 @@ See `LICENSE.txt` in the root of this repository for the text of the license. ## To do * Add JS to make the feed/tag input situation work like one would normally expect rather than like it's 1985. (Progressive enhancement, though.) -* Do more user input validation -* Handle more `requests` and `feedparser` error conditions * Add support for authentication * Allow specifying update interval on a per-feed basis @@ -228,6 +228,12 @@ def add_feed_effect(): ) else: raise bottle.HTTPError(500, f"Failed to fetch feed from {feed_source}.") + except tagrss.NotAFeedError: + raise bottle.HTTPError( + 400, + f"Could not add feed from {feed_source} as the content at the location is " + "not a valid feed.", + ) return bottle.template( "add_feed", after_add=True, @@ -316,7 +322,7 @@ def update_feeds(run_event: threading.Event): for feed in feeds: try: core.update_feed(feed.id) - except tagrss.FeedFetchError as e: + except (tagrss.FeedFetchError, tagrss.NotAFeedError) as e: logging.error( f"Failed to update feed {feed.id} with source {feed.source} " f"due to the following error: {e}." @@ -59,6 +59,10 @@ class FeedFetchError(Exception): super().__init__(f"Get {feed_source} failed: {underlying}") +class NotAFeedError(Exception): + pass + + FeedId = int Epoch = int ParsedFeed = feedparser.FeedParserDict @@ -431,6 +435,12 @@ class TagRss: io.BytesIO(bytes(response.text, encoding="utf-8")), response_headers={"Content-Location": base}, ) + if not ( + getattr(parsed.feed, "title", None) + or getattr(parsed.feed, "link", None) + or getattr(parsed.feed, "id", None) + ): + raise NotAFeedError(source) return (parsed, epoch_downloaded) def add_feed( |