Skip to content

Commit

Permalink
Merge pull request #8 from nishuiq/main
Browse files Browse the repository at this point in the history
添加支持Github Actions 执行定时任务
  • Loading branch information
Maojuan-lang authored Nov 20, 2023
2 parents 93ba89c + 8ea948d commit 9484bae
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/schedule.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: SenKongDao Daily Schedule Workflow

on:
schedule:
- cron: '0 20 * * *' # UTC+8=凌晨4点
workflow_dispatch: # 添加手动触发事件

jobs:
run-python-job:
runs-on: ubuntu-latest

env:
UID: ${{ secrets.UID }}
ATOKEN: ${{ secrets.ATOKEN }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Python script
if: github.event_name == 'schedule'
run: python SenKongDao.py

- name: Manual Test Step
if: github.event_name == 'workflow_dispatch'
run: python SenKongDao.py
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@

xxxxxx&yyyyyyyyyy

### 使用 Github Actions 定时运行执行签到
1. 首先 fork 仓库到自己账号下
2. 设置 `UID``TOKEN`
3. 你可以参考上面教程新建 `SenKongDao_config.txt` 文件,填入 `uid` 以及 `TOKEN` 字段(不推荐,如非必要请在私人仓库这么做)
4. 或者在自己项目页面,依次点击 Settings → Secrets → Secrets and variables → Actions,
点击 New repository secret,并创建 `UID``TOKEN`,填写值,`UID``TOKEN` 值一一对应,对于多账号则依次换行填写。
最后在项目中点击 Actions,`(Enable workflow)` 启动对应的 Actions 即可每日自动签到
5. 若要手动运行,找到对应的 Actions 并点击 Run workflow 即可立即运行

### 或使用 docker 运行
1. 参考上面教程新建 `SenKongDao_config.txt` 文件,填入 `uid` 以及 `cred` 字段
2. 运行 `docker run -v ./SenKongDao_config.txt:/app/SenKongDao_config.txt maojuan180/senkongdao` 其中 `./SenKongDao_config.txt` 为配置文件路径,可自行修改
Expand All @@ -44,4 +53,4 @@ xxxxxx&yyyyyyyyyy

<a href="https://github.com/Maojuan-lang/SenKongDao/graphs/contributors">
<img src="https://contrib.rocks/image?repo=Maojuan-lang/SenKongDao" />
</a>
</a>
46 changes: 32 additions & 14 deletions SenKongDao.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import sys
import time
import datetime
Expand All @@ -15,22 +16,39 @@
# 打印当前时间
print("当前时间为:" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

# 读取cookie
cookie_file = open("SenKongDao_config.txt", "r+", encoding="utf8")
cookie_lines = cookie_file.readlines()
cookie_file.close()
print("已读取" + str(len(cookie_lines)) + "个cookie")
print(str(sleep_time) + "秒后进行签到...")
time.sleep(sleep_time)
def get_uid_and_atoken():
account_list = []
# Github Actions 传递的 secrets
if "UID" in os.environ and "ATOKEN" in os.environ:
# 使用环境变量中的 UID 和 ATOKEN
print("使用环境变量")
uid_env = os.environ["UID"]
atoken_env = os.environ["ATOKEN"]
uids = uid_env.split('\n')
atokens = atoken_env.split('\n')

# 遍历所有账号
account_list = [(uid.strip(), atoken.strip()) for uid, atoken in zip(uids, atokens)]
else:
cookie_file = open("SenKongDao_config.txt", "r+", encoding="utf8")
cookie_lines = cookie_file.readlines()
cookie_file.close()

# 遍历cookie
for cookie_line in cookie_lines:
# 将文件中的配置存储为元组的列表
for cookie_line in cookie_lines:
configs = cookie_line.split("&")
uid = configs[0].strip()
atoken = configs[1].strip()
account_list.append((uid, atoken))

# 准备签到信息
configs = cookie_line.split("&")
uid = configs[0].strip()
atoken = configs[1].strip()
print("已读取" + str(len(account_list)) + "个cookie")
print(str(sleep_time) + "秒后进行签到...")
time.sleep(sleep_time)
return account_list

# 遍历cookie
account_list = get_uid_and_atoken()
for uid, atoken in account_list:
# 获取签到用的值
cred_resp = headersGenerator.get_cred_by_token(atoken)
sign_token = cred_resp['token']
Expand Down Expand Up @@ -86,4 +104,4 @@
# 休眠指定时间后,继续下个账户
time.sleep(sleep_time)

print("程序运行结束")
print("程序运行结束")

0 comments on commit 9484bae

Please sign in to comment.