From cfd8ab5e0cb7c03a470be61fd190fd5325d11213 Mon Sep 17 00:00:00 2001 From: a3510377 Date: Wed, 24 Apr 2024 06:19:40 +0800 Subject: [PATCH] feat sync script --- .github/workflows/releaser.yml | 2 ++ .github/workflows/sync.yaml | 48 ++++++++++++++++++++++++++++++++++ .gitignore | 1 + scripts/requirements.txt | 2 +- scripts/summon.py | 33 +++++++++++------------ 5 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/sync.yaml create mode 100644 .gitignore diff --git a/.github/workflows/releaser.yml b/.github/workflows/releaser.yml index 01fcf7d..cb68678 100644 --- a/.github/workflows/releaser.yml +++ b/.github/workflows/releaser.yml @@ -7,6 +7,8 @@ on: - README.md - .github/** - '!.github/workflows/**' + paths: + - server/** env: # Use docker.io for Docker Hub if empty diff --git a/.github/workflows/sync.yaml b/.github/workflows/sync.yaml new file mode 100644 index 0000000..8169144 --- /dev/null +++ b/.github/workflows/sync.yaml @@ -0,0 +1,48 @@ +name: sync + +on: + workflow_dispatch: + push: + branches: + - main + +permissions: + contents: read + pages: write + id-token: write + +jobs: + release: + name: Release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: '3.10' + cache: pip + + - name: Install dependencies + run: pip install -r scripts/requirements.txt + + - name: Sync + run: python scripts/summon.py + + # - name: deploy + # run: | + # cd output + # [ -f 404.html ] || ln -s index.html 404.html + + - name: Setup Pages + uses: actions/configure-pages@v4 + + - name: Upload artifact + uses: actions/upload-pages-artifact@v3 + with: + # Upload entire repository + path: './output' + + - name: Deploy to GitHub Pages + id: deployment + uses: actions/deploy-pages@v4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea1472e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +output/ diff --git a/scripts/requirements.txt b/scripts/requirements.txt index ee4ba4f..f229360 100644 --- a/scripts/requirements.txt +++ b/scripts/requirements.txt @@ -1 +1 @@ -aiohttp +requests diff --git a/scripts/summon.py b/scripts/summon.py index 56333a7..f731fac 100644 --- a/scripts/summon.py +++ b/scripts/summon.py @@ -1,28 +1,25 @@ +import json import os -import yaml -import aiohttp -import asyncio +from pathlib import Path +from typing import Dict, List, Optional, TypedDict + +import requests BASE_API_URL = os.getenv("BASE_API_URL", "https://example.com/api") MEMBERS_API_URL = f"{BASE_API_URL}/members" -async def fetch(s: aiohttp.ClientSession, url: str) -> dict: - try: - async with s.get(url) as response: - return await response.json() - except aiohttp.ClientError as e: - print(e) - return await fetch(s, url) - +class Member(TypedDict): + uuid: str + name: str + introduction: Optional[str] -async def main(): - async with aiohttp.ClientSession() as s: - print(yaml.safe_load("")) - # url = "https://api.coinmarketcap.com/v1/ticker/" - # data = await fetch(s, url) - # print(json.dumps(data, indent=4)) +# { [group: str]: Member[] } +def get_members() -> Dict[str, List[Member]]: + return requests.get(MEMBERS_API_URL).json() -asyncio.run(main()) +OUTPUT_DIR = Path("output") +OUTPUT_DIR.mkdir(parents=True, exist_ok=True) +(OUTPUT_DIR / "member.json").write_text(json.dumps(get_members(), separators=(",", ":")))