From 6fe80c8d682d05fcb6b70d7c2c19c027853f7c87 Mon Sep 17 00:00:00 2001 From: Mariotaku Date: Mon, 2 Oct 2023 22:58:44 +0900 Subject: [PATCH] saving icons to local repo --- pelicanconf.py | 6 ++++-- repogen/apidata.py | 3 +++ repogen/icons.py | 25 +++++++++++++++++++++++++ repogen/plugin.py | 2 ++ repogen/siteurl.py | 8 ++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 repogen/icons.py create mode 100644 repogen/siteurl.py diff --git a/pelicanconf.py b/pelicanconf.py index efc2edd..3d03d5a 100644 --- a/pelicanconf.py +++ b/pelicanconf.py @@ -8,10 +8,11 @@ from webassets.cache import MemoryCache import repogen +from repogen.siteurl import siteurl AUTHOR = 'webOS Homebrew Project' SITENAME = 'webOS Homebrew Project' -SITEURL = '' +SITEURL = siteurl() THEME = 'webosbrew' theme_dir = Path(__file__, '..', 'theme').resolve() @@ -28,11 +29,12 @@ PATH = 'content' -STATIC_PATHS = ['extra/CNAME', 'extra/favicon.ico', 'schemas'] +STATIC_PATHS = ['extra/CNAME', 'extra/favicon.ico', 'schemas', 'apps/icons'] ARTICLE_EXCLUDES = ['api'] PAGE_PATHS = ['pages', 'apps', '../packages'] EXTRA_PATH_METADATA = { + 'apps/icons': {'path': 'apps/icons/'}, 'extra/CNAME': {'path': 'CNAME'}, 'extra/favicon.ico': {'path': 'favicon.ico'}, } diff --git a/repogen/apidata.py b/repogen/apidata.py index 5d76c68..b81efc4 100755 --- a/repogen/apidata.py +++ b/repogen/apidata.py @@ -10,7 +10,9 @@ from repogen import pkg_info from repogen.common import ITEMS_PER_PAGE, ensure_open +from repogen.icons import obtain_icon from repogen.pkg_info import PackageInfo +from repogen.siteurl import siteurl MANIFEST_KEYS = ('id', 'title', 'iconUri', 'manifestUrl', 'manifest', 'manifestUrlBeta', 'manifestBeta', 'pool') @@ -40,6 +42,7 @@ def package_item(p_info: PackageInfo, in_apps_dir: bool): package['fullDescriptionUrl'] = f'{p_info["id"]}/full_description.html' else: package['fullDescriptionUrl'] = f'apps/{p_info["id"]}/full_description.html' + package['iconUri'] = obtain_icon(package['id'], p_info["iconUri"], siteurl()) return package packages_length = len(packages) diff --git a/repogen/icons.py b/repogen/icons.py new file mode 100644 index 0000000..0f57ae1 --- /dev/null +++ b/repogen/icons.py @@ -0,0 +1,25 @@ +import os +from pathlib import Path + +import requests + +_approot = Path(__file__).parent.parent + +assert _approot.samefile(os.getcwd()) + +_iconspath = _approot.joinpath('content', 'apps', 'icons') + +_iconspath.mkdir(parents=True, exist_ok=True) + + +def obtain_icon(pkg_id: str, uri: str, siteurl: str) -> str: + resp = requests.get(uri, allow_redirects=True) + extension = uri[uri.rfind('.') + 1:] + icon_name = f'{pkg_id}.{extension}' + icon_path = _iconspath.joinpath(icon_name) + out_uri = f'{siteurl.removesuffix("/")}/apps/icons/{icon_name}' + if icon_path.exists(): + return out_uri + with icon_path.open('wb') as icon_f: + icon_f.write(resp.content) + return out_uri diff --git a/repogen/plugin.py b/repogen/plugin.py index b9260e6..eac54ce 100644 --- a/repogen/plugin.py +++ b/repogen/plugin.py @@ -10,6 +10,7 @@ from pelican.themes.webosbrew import pagination_data from repogen import funding, apidata, pkg_info +from repogen.icons import obtain_icon log = logging.getLogger(__name__) @@ -25,6 +26,7 @@ def __init__(self, *args, **kwargs): def read(self, filename: str): info = pkg_info.from_package_info_file(Path(filename), offline='CI' not in os.environ) + info['iconUri'] = obtain_icon(info['id'], info['iconUri'], self.settings['SITEURL']) metadata = { 'title': info['title'], 'override_save_as': f'apps/{info["id"]}.html', diff --git a/repogen/siteurl.py b/repogen/siteurl.py new file mode 100644 index 0000000..178a0be --- /dev/null +++ b/repogen/siteurl.py @@ -0,0 +1,8 @@ +from pathlib import Path + + +def siteurl() -> str: + cname_file = Path(__file__, '..', '..', 'content', 'extra', 'CNAME') + if not cname_file.exists(): + return '' + return f'https://{cname_file.read_text().strip()}'