2
0
Fork 0

caching and beta detection

- Caching using etag to lower impact on feeds.
- Keyword detection of beta releases.
This commit is contained in:
Carlos Galindo 2023-10-23 13:12:35 +01:00
parent 0c0822b9b9
commit 5b66d9db96

View file

@ -162,18 +162,40 @@ class FeedReader:
self.name = name self.name = name
self.url = url self.url = url
self.target = target 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)''' '''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: if len(feed.entries) == 0:
return None 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: def read_feed(self) -> bool | None:
'''Read a feed and post an issue if a new item is found''' '''Read a feed and post an issue if a new item is found'''
entry = self.first_item() entry = self.first_item()
if entry == 304:
return False
if not entry: if not entry:
return None return None
version, _id = self.entry_get_version(entry) 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.''' If the version is not new, or the posting fails, the method stops and returns False.'''
# Match 1: with local file # Match 1: with local file
try: 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) match = file.readline().strip("\n") == str(_id)
except FileNotFoundError: except FileNotFoundError:
match = False match = False
@ -206,7 +228,7 @@ class FeedReader:
# Save to disk # Save to disk
if not os.path.isdir(CONFIG_DIR): if not os.path.isdir(CONFIG_DIR):
os.makedirs(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)) file.write(str(_id))
return True return True