Sync Sponsors to README #7399
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Sync Sponsors to README | |
| on: | |
| schedule: | |
| - cron: '0 0 * * 0' # 每周日凌晨执行一次 | |
| workflow_dispatch: # 也支持手动触发 | |
| jobs: | |
| sync-sponsors: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: 3.9 | |
| - name: Install dependencies | |
| run: | | |
| pip install requests | |
| - name: Fetch sponsors data | |
| id: fetch-sponsors | |
| env: | |
| USER_ID: ${{ secrets.USER_ID }} | |
| API_TOKEN: ${{ secrets.API_TOKEN }} | |
| run: | | |
| import requests | |
| import time | |
| from hashlib import md5 | |
| import os | |
| import base64 | |
| def get_sign(user_id, token, params, ts): | |
| return md5(f"{token}params{params}ts{ts}user_id{user_id}".encode("utf-8")).hexdigest() | |
| user_id = os.getenv('USER_ID') | |
| token = os.getenv('API_TOKEN') | |
| ts = int(time.time()) | |
| data = { | |
| "user_id": user_id, | |
| "ts": ts | |
| } | |
| params = '{"page":1}' | |
| data['sign'] = get_sign(user_id, token, params, ts) | |
| data['params'] = params | |
| res = requests.post("https://afdian.com/api/open/query-sponsor", json=data) | |
| try: | |
| sponsors = res.json()['data']['list'] | |
| except: | |
| print(res.text) | |
| exit(1) | |
| sponsors_info = [] | |
| for sponsor in sponsors: | |
| user = sponsor['user'] | |
| amount = sponsor["all_sum_amount"] | |
| avatar = user.get('avatar', '') | |
| name = user.get('name', '') | |
| sponsors_info.append(f"<img src=\"{avatar}\" alt=\"{name}\" width=\"35\" height=\"35\"> {name} - {amount}元") | |
| if sponsors_info == []: | |
| sponsors_text = "暂时没有人给我赞助:( \n" | |
| else: | |
| sponsors_text = ' \n'.join(sponsors_info) | |
| encoded_text = base64.b64encode(sponsors_text.encode('utf-8')).decode('utf-8') | |
| print(f"::set-output name=sponsors_text::{encoded_text}") | |
| shell: python | |
| - name: Update README | |
| id: update_readme | |
| env: | |
| ENCODED_TEXT: ${{ steps.fetch-sponsors.outputs.sponsors_text }} | |
| run: | | |
| import os | |
| import base64 | |
| sponsors_info = base64.b64decode(os.getenv("ENCODED_TEXT")).decode('utf-8').split("\n") | |
| print(sponsors_info) | |
| with open("README.md",encoding="utf-8") as f: | |
| f.seek(0,0) | |
| content = f.readlines() | |
| with open("README.md","w",encoding="utf-8") as f: | |
| start_line = -1 | |
| end_line = -1 | |
| for i in range(len(content)): | |
| if "<!-- START_SPONSORS -->" in content[i]: | |
| start_line = i | |
| elif "<!-- END_SPONSORS -->" in content[i]: | |
| end_line = i | |
| break | |
| if start_line == -1 or end_line == -1: | |
| raise Exception("未找到标记") | |
| num = 0 | |
| i = start_line + 1 | |
| while "<!-- END_SPONSORS -->" not in content[i]: | |
| del content[i] | |
| while num < len(sponsors_info): | |
| content.insert(i,sponsors_info[num]+'\n') | |
| num += 1 | |
| i += 1 | |
| f.writelines(content) | |
| shell: python | |
| - name: Check for changes | |
| id: check-changes | |
| run: | | |
| if git diff --quiet README.md; then | |
| echo "::set-output name=has_changes::false" | |
| else | |
| echo "::set-output name=has_changes::true" | |
| fi | |
| - name: Commit and push changes(github) | |
| if: steps.check-changes.outputs.has_changes == 'true' | |
| run: | | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| git add README.md | |
| git commit --allow-empty-message -m "" | |
| git push | |
| - name: Commit and push changes(gitee) | |
| env: | |
| HAS_CHANGES: ${{ steps.check-changes.outputs.has_changes }} | |
| API_TOKEN: ${{ secrets.API_TOKEN2 }} | |
| ENCODED_TEXT: ${{ steps.fetch-sponsors.outputs.sponsors_text }} | |
| run: | | |
| import os | |
| import base64 | |
| import requests | |
| import re | |
| if os.getenv("HAS_CHANGES") == 'true': | |
| api_token = os.getenv("API_TOKEN") | |
| content = base64.b64decode(os.getenv("ENCODED_TEXT").encode('utf-8')).decode('utf-8').split("\n") | |
| text = "" | |
| try: | |
| for i in content: | |
| sponsor = re.findall("<img src=\"(.*)\" alt=\"(.*)\" width=\"35\" height=\"35\"> .* - (.*)元",i)[0] | |
| text += f"{sponsor[1]} {sponsor[2]} {sponsor[0]}\n" | |
| final_text = base64.b64encode(text.encode('utf-8')).decode('utf-8') | |
| except: | |
| final_text = "" | |
| r1 = requests.get(f"https://gitee.com/api/v5/repos/sun589/sponsors/contents/sponsors?access_token={api_token}").json() | |
| sha = r1['sha'] | |
| data = {"access_token":api_token, | |
| "content":final_text, | |
| "sha":sha, | |
| "message":"update sponsors list"} | |
| r2 = requests.put(f"https://gitee.com/api/v5/repos/sun589/sponsors/contents/sponsors",data=data) | |
| shell: python |