From 130fc6028ad4749d4ada82ea7f63afd03284e4cd Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sun, 7 Jan 2024 13:41:53 +0000 Subject: [PATCH] feat(updatechecker): added support for thumbnails in gitea/forgejo --- updatechecker/updatechecker.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/updatechecker/updatechecker.py b/updatechecker/updatechecker.py index 520d116..c1e426c 100644 --- a/updatechecker/updatechecker.py +++ b/updatechecker/updatechecker.py @@ -32,6 +32,7 @@ SOFTWARE. import asyncio import traceback from datetime import datetime +from urllib.parse import urlparse import aiohttp import discord @@ -126,11 +127,12 @@ class UpdateChecker(commands.Cog): else: url = repo.url + "rss/branch/" + repo.branch response = await self.fetch_feed(url) + parsed_url = urlparse(repo.url) try: commit = response.entries[0]["id"][33:] hash = "[" + commit + "](" + response.entries[0]["link"] + ")" cn = response.entries[0]["title"] + " - " + response.entries[0]["author"] - image = None + image = await self.fetch_gitea_thumbnail(parsed_url.scheme + "://" + parsed_url.netloc + "/api/v1/repos" + parsed_url.path) except AttributeError: continue saving_dict[repo_name] = commit @@ -260,6 +262,16 @@ class UpdateChecker(commands.Cog): return None return ret + async def fetch_gitea_thumbnail(self, url: str) -> str: + timeout = aiohttp.client.ClientTimeout(total=15) + try: + async with self.session.get(url, timeout=timeout) as response: + data = await response.read() + except (aiohttp.ClientError, asyncio.TimeoutError): + return None + + return data['avatar_url'] + @commands.is_owner() @commands.group(name="cogupdater", aliases=["cu"]) async def update(self, ctx):