Skip to content

Commit 609f936

Browse files
authored
Use X-authkey to interact with Satwiki (#135)
Enhances authenticated Satwiki interactions by injecting the `X-authkey` header and refactors the `wiki` utility logic for conditional client instantiation based on branch or workflow context, while also streamlining code formatting, bumping the CI Python version with updated secrets, and updating documentation and Dependabot schedules.
1 parent 2d87a1e commit 609f936

File tree

7 files changed

+250
-162
lines changed

7 files changed

+250
-162
lines changed

.github/dependabot.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ updates:
33
- package-ecosystem: "github-actions"
44
directory: "/"
55
schedule:
6-
interval: "daily"
7-
time: "06:00"
6+
interval: "cron"
7+
cronjob: "0 16 * * 5"
88
timezone: "Asia/Shanghai"
99
reviewers:
1010
- "yusancky"
@@ -13,8 +13,8 @@ updates:
1313
- package-ecosystem: "pip"
1414
directory: "/"
1515
schedule:
16-
interval: "daily"
17-
time: "06:00"
16+
interval: "cron"
17+
cronjob: "0 16 * * 5"
1818
timezone: "Asia/Shanghai"
1919
reviewers:
2020
- "yusancky"

.github/workflows/AllUp.yml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
name: AllUp
2-
32
on:
43
push:
54
branches: [ main ]
@@ -9,32 +8,27 @@ on:
98
- synchronize
109
- reopened
1110
schedule:
12-
- cron: '26 1 * * 5'
11+
- cron: '26 1 * * *'
1312
workflow_dispatch:
14-
1513
permissions:
1614
contents: write
1715
pull-requests: write
18-
1916
env:
2017
TZ: Asia/Shanghai
21-
2218
jobs:
2319
AllUp:
2420
runs-on: ubuntu-latest
2521
steps:
26-
- name: Checkout
27-
uses: actions/checkout@v4.2.2
28-
- name: Setup Python
29-
uses: actions/setup-python@v5.6.0
22+
- uses: actions/checkout@v4.2.2
23+
- uses: actions/setup-python@v5.6.0
3024
with:
31-
python-version: '3.12'
25+
python-version: '3.13'
3226
cache: 'pip'
33-
- name: Install Python Dependencies
34-
run: pip install -r requirements.txt
35-
- name: Main
36-
run: python main.py
27+
- run: pip install -r requirements.txt
28+
- run: python main.py
3729
env:
30+
BOT_PASSWORD: ${{ secrets.BOT_PASSWORD }}
31+
X_AUTHKEY: ${{ secrets.X_AUTHKEY }}
3832
GITHUB_EVENT_NAME: ${{ github.event_name }}
3933
GITHUB_REF: ${{ github.ref }}
4034
GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }}

AllUp_utils/web.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
# Copyright (c) yusancky. All rights reserved.
2-
# Licensed under the Apache License 2.0. See License in the project root for license information.
1+
# Copyright (c) yusancky. All rights reserved.
2+
# Licensed under the Apache License 2.0. See License in the project root for license information.
33

44
from requests import get
55
from selenium import webdriver
66
from selenium.webdriver.chrome.options import Options
77

8+
89
def configure_chromedriver():
910
chrome_options = Options()
10-
for option in ['--headless','--disable-gpu','--window-size=1920,1200','--ignore-certificate-errors','--disable-extensions','--no-sandbox','--disable-dev-shm-usage']:
11+
for option in [
12+
"--headless",
13+
"--disable-gpu",
14+
"--window-size=1920,1200",
15+
"--ignore-certificate-errors",
16+
"--disable-extensions",
17+
"--no-sandbox",
18+
"--disable-dev-shm-usage",
19+
]:
1120
chrome_options.add_argument(option)
12-
return webdriver.Chrome(options = chrome_options)
21+
return webdriver.Chrome(options=chrome_options)
22+
1323

14-
def fetch_data(url : str,need_selenium = False):
24+
def fetch_data(url: str, need_selenium=False):
1525
if need_selenium:
1626
need_selenium.get(url)
1727
return need_selenium.page_source

AllUp_utils/wiki.py

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
1-
# Copyright (c) yusancky. All rights reserved.
2-
# Licensed under the Apache License 2.0. See License in the project root for license information.
1+
# Copyright (c) yusancky. All rights reserved.
2+
# Licensed under the Apache License 2.0. See License in the project root for license information.
33

44
from os import environ
55
from pwiki.wiki import Wiki
6+
import requests
67

7-
def PR_TEST():
8-
return environ['GITHUB_EVENT_NAME'] == 'pull_request'
9-
10-
def MAIN_REPO_BRANCH():
11-
return (environ['GITHUB_REF'] == 'refs/heads/main' and environ['GITHUB_REPOSITORY_OWNER'] == 'yusancky')
12-
13-
def pull(title : str,split_line = False):
14-
if MAIN_REPO_BRANCH() or PR_TEST():
15-
print('Unable to find test sources.')
16-
try:
17-
wiki = Wiki('sat.huijiwiki.com')
18-
return wiki.page_text(title)
19-
except:
20-
print(f'You do not have permission to get password.\nREF: {environ["GITHUB_REF"]}\nREPO_OWNER: {environ["GITHUB_REPOSITORY_OWNER"]}')
8+
try:
9+
MAIN_REPO_BRANCH = (
10+
environ["GITHUB_REPOSITORY_OWNER"] == "yusancky"
11+
and environ["GITHUB_REF"] == "refs/heads/main"
12+
)
13+
TEST_DISPATCH = (
14+
environ["GITHUB_REPOSITORY_OWNER"] == "yusancky"
15+
and environ["GITHUB_EVENT_NAME"] == "workflow_dispatch"
16+
)
17+
TEST_PR = environ["GITHUB_EVENT_NAME"] == "pull_request"
18+
except Exception:
19+
MAIN_REPO_BRANCH, TEST_DISPATCH, TEST_PR = False, False, False
20+
21+
_original_session_init = requests.Session.__init__
22+
23+
24+
def _patched_session_init(self, *args, **kwargs):
25+
_original_session_init(self, *args, **kwargs)
26+
self.headers["X-authkey"] = environ["X_AUTHKEY"]
27+
28+
29+
requests.Session.__init__ = _patched_session_init
30+
31+
if MAIN_REPO_BRANCH or TEST_DISPATCH or TEST_PR:
32+
wiki = Wiki("sat.huijiwiki.com", "雨伞CKY", environ["BOT_PASSWORD"])
33+
34+
35+
def pull(title: str, split_line=False):
36+
if MAIN_REPO_BRANCH or TEST_DISPATCH or TEST_PR:
37+
return wiki.page_text(title)
2138
else:
22-
try:
23-
wiki = Wiki('sat.huijiwiki.com')
24-
return wiki.page_text(title)
25-
except:
26-
print(f'You do not have permission to get password.\nREF: {environ["GITHUB_REF"]}\nREPO_OWNER: {environ["GITHUB_REPOSITORY_OWNER"]}')
27-
28-
def push(title : str,content_id : str,content : str):
29-
open(f'{title}.wikitext', 'w').write(content)
30-
if PR_TEST():
31-
print(f'### {content_id}\n\n```go\n{content}\n```\n\n',file = open('PR_preview.md','a'))
39+
return ""
40+
41+
42+
def push(title: str, content_id: str, content: str):
43+
open(f"{title}.wikitext", "w").write(content)
44+
if MAIN_REPO_BRANCH or TEST_DISPATCH:
45+
wiki.edit(title, content, "Edit via AllUp-Satwiki")
46+
if TEST_PR:
47+
print(
48+
f"### {content_id}\n\n```go\n{content}\n```\n\n",
49+
file=open("PR_preview.md", "a"),
50+
)

AllUp_utils/wikitext.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
# Copyright (c) yusancky. All rights reserved.
2-
# Licensed under the Apache License 2.0. See License in the project root for license information.
1+
# Copyright (c) yusancky. All rights reserved.
2+
# Licensed under the Apache License 2.0. See License in the project root for license information.
3+
34

45
def build_switch(data_map):
5-
result = '{{#switch:{{{' + data_map['switch_key'] + '|}}}'
6-
for data_key,data_value in data_map.items():
7-
if data_key == 'switch_key':
6+
result = "{{#switch:{{{" + data_map["switch_key"] + "|}}}"
7+
for data_key, data_value in data_map.items():
8+
if data_key == "switch_key":
89
continue
9-
if isinstance(data_value,str):
10-
result += f'|{data_key}={data_value}'
11-
elif isinstance(data_value,dict):
12-
result += f'|{data_key}={build_switch(data_value)}'
10+
if isinstance(data_value, str):
11+
result += f"|{data_key}={data_value}"
12+
elif isinstance(data_value, dict):
13+
result += f"|{data_key}={build_switch(data_value)}"
1314
else:
14-
raise TypeError(f"A string or a dictionary is required, not '{type(data_value)}'.")
15-
result += '}}'
15+
raise TypeError(
16+
f"A string or a dictionary is required, not '{type(data_value)}'."
17+
)
18+
result += "}}"
1619
return result

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@
22

33
![Python: 3.12 | 3.13 | 3.14](https://img.shields.io/badge/Python-3.12%20%7C%203.13%20%7C%203.14-python?style=social&logo=python&logoColor=blue) ![License: Apache-2.0](https://img.shields.io/github/license/yusancky/AllUp-Satwiki?style=social)
44

5-
AllUp 项目是由 [雨伞CKY](https://github.com/yusancky) 维护的、自动化爬取并集合数据以便动态更新的项目<!--AllUp 项目的数据将于每日北京时间 9 时 26 分爬取数据并修改。[^1]-->由于近日 GitHub Actions 运行器未能与灰机 wiki 服务器进行成功连接,近期无法推送内容至卫星百科。
5+
AllUp-Satwiki 项目是由 [雨伞CKY](https://github.com/yusancky) 维护的、自动化爬取并集合数据以便动态更新的项目,其于北京时间每天 9 时 26 分获取数据并同步到卫星百科[^1]
66

77
![Contributions in the Last 30 Days](https://repobeats.axiom.co/api/embed/3c013245586cfcc386dd553450db134d7617991c.svg)
88

9-
# 使用
10-
11-
克隆或下载本存储库后,(切换到本项目根目录后)使用 `pip install -r requirements.txt` 安装所有依赖。之后,你可以分别通过运行 `main.py``TSS/main.py` 生成主模板内容、天宫空间站任务列表的 Apache ECharts 模板内容。生成的内容不会主动输出。
12-
13-
# 贡献
14-
15-
请前往 [贡献文档](/.github/CONTRIBUTING.md) 查看贡献相关内容。
16-
17-
[^1]: 定时进行任务基于 GitHub Action 的 `schedule` 事件。但 `schedule` 事件在 Actions 工作流运行期间负载过高时可能会延迟。据以往经历,一般延迟在 30 分钟至 45 分钟。
9+
[^1]: 定时任务基于 GitHub Actions 的 `schedule` 事件,但其在 Actions 工作流运行负载过高时可能会延迟。

0 commit comments

Comments
 (0)