From 5b66d9db96aa83607faaf967a50a749845a37c92 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Mon, 23 Oct 2023 13:12:35 +0100 Subject: [PATCH] caching and beta detection - Caching using etag to lower impact on feeds. - Keyword detection of beta releases. --- issue_generator.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/issue_generator.py b/issue_generator.py index 0ebc0dc..d2201cc 100755 --- a/issue_generator.py +++ b/issue_generator.py @@ -162,18 +162,40 @@ class FeedReader: self.name = name self.url = url self.target = target + self.version_file = CONFIG_DIR + self.name + self.etag_file = CONFIG_DIR + self.name + ".etag" + self.beta_strings = [ "nightly", "beta", "alpha", "rc" ] - def first_item(self) -> dict[str, Any] | None: + def first_item(self) -> dict[str, Any] | None | int: '''Get the first item of the feed (newest)''' - feed = feedparser.parse(self.url) + if os.path.isfile(self.etag_file): + with open(self.etag_file, encoding="UTF-8") as file: + etag = file.readline() + else: etag = None + feed = feedparser.parse(self.url, etag=etag) + if feed.etag and feed.etag != etag: + with open(self.etag_file, mode='w', encoding="UTF-8") as file: + file.write(feed.etag) + if feed.status == 304: + return 304 if len(feed.entries) == 0: return None - return feed.entries[0] + for entry in feed.entries: + skip = False + for beta in self.beta_strings: + if beta in self.entry_get_version(entry)[0]: + skip = True + break + if not skip: + return entry + return None def read_feed(self) -> bool | None: '''Read a feed and post an issue if a new item is found''' entry = self.first_item() + if entry == 304: + return False if not entry: return None version, _id = self.entry_get_version(entry) @@ -192,7 +214,7 @@ class FeedReader: If the version is not new, or the posting fails, the method stops and returns False.''' # Match 1: with local file try: - with open(CONFIG_DIR + self.name, encoding="utf-8") as file: + with open(self.version_file, encoding="utf-8") as file: match = file.readline().strip("\n") == str(_id) except FileNotFoundError: match = False @@ -206,7 +228,7 @@ class FeedReader: # Save to disk if not os.path.isdir(CONFIG_DIR): os.makedirs(CONFIG_DIR) - with open(CONFIG_DIR + self.name, mode="w", encoding="utf-8") as file: + with open(self.version_file, mode="w", encoding="utf-8") as file: file.write(str(_id)) return True