Skip to content

Commit

Permalink
使用第三方 cron
Browse files Browse the repository at this point in the history
github action 太不准时了
  • Loading branch information
sakurasep committed Mar 23, 2024
1 parent e1db3bb commit f92d94d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/checkout.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: 自动签退

on:
workflow_dispatch:
schedule:
- cron: '30 13 * * *' # 北京时间每天的21:30
repository_dispatch:
types: [schedule-run]

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/rebook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: 重新预约此位置

on:
workflow_dispatch:
schedule:
- cron: '20 0 * * *' # 北京时间每天的21:30
repository_dispatch:
types: [schedule-run]

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tomorrow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: 预约明天的位置

on:
workflow_dispatch:
schedule:
- cron: '10 11 * * *' # 北京时间每天的19:15
repository_dispatch:
types: [schedule-run]

jobs:
build:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 注意,本项目正在 bug 修复阶段,如果出现运行问题,请拉取仓库的最新提交。如果无法解决,请带着 log 进行反馈。

# qfnuLibraryBook

曲Star图书馆预约程序
Expand All @@ -10,7 +12,7 @@
2. 本项目目的是告别卡顿的预约界面,采用更快捷的方式自动预约座位。推荐使用云服务器,甚至路由的 openwrt 也可以运行本程序,实现24小时自动监控。
3. 希望预约后能够有效利用如此好的学习环境,这么好的环境不是让某些人谈恋爱和打游戏的,如果你只是去做以上事情,你可以不预约座位,图书馆开放了很多自由活动的空间。
4. 欢迎提出修改建议,请在 issue 中进行交流
5. 本项目仅供交流学习,禁止将本项目用于商业行为
5. 本项目仅供交流学习,严禁在任何国内平台宣传此项目,禁止将本项目用于商业行为

## 环境

Expand Down
7 changes: 4 additions & 3 deletions py/main/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 通知渠道选择
PUSH_METHOD: "ANPUSH"

# Telegram 相关
CHANNEL_ID: ""
TELEGRAM_BOT_TOKEN: ""
Expand All @@ -18,12 +21,10 @@ PASSWORD: ""
GITHUB: True

# 程序设置
MODE: "1"
MODE: "2"
SEAT_ID:
- 7520
- 7543
DATE: "tomorrow"
CLASSROOMS_NAME:
- 西校区图书馆-五层自习室
- 西校区图书馆-四层自习室
- 西校区图书馆-三层自习室
57 changes: 29 additions & 28 deletions py/main/get_seat.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
BARK_EXTRA = ""
ANPUSH_TOKEN = ""
ANPUSH_CHANNEL = ""
PUSH_METHOD = ""


# 读取YAML配置文件并设置全局变量
def read_config_from_yaml():
global CHANNEL_ID, TELEGRAM_BOT_TOKEN, \
CLASSROOMS_NAME, MODE, SEAT_ID, DATE, \
USERNAME, PASSWORD, GITHUB, BARK_EXTRA, BARK_URL, ANPUSH_TOKEN, ANPUSH_CHANNEL
USERNAME, PASSWORD, GITHUB, BARK_EXTRA, \
BARK_URL, ANPUSH_TOKEN, ANPUSH_CHANNEL, PUSH_METHOD
current_dir = os.path.dirname(os.path.abspath(__file__)) # 获取当前文件所在的目录的绝对路径
config_file_path = os.path.join(current_dir, 'config.yml') # 将文件名与目录路径拼接起来
with open(config_file_path, 'r') as yaml_file:
Expand All @@ -62,6 +64,7 @@ def read_config_from_yaml():
BARK_EXTRA = config.get("BARK_EXTRA", "")
ANPUSH_TOKEN = config.get("ANPUSH_TOKEN", "")
ANPUSH_CHANNEL = config.get("ANPUSH_CHANNEL", "")
PUSH_METHOD = config.get("PUSH_METHOD", "")


# 在代码的顶部定义全局变量
Expand Down Expand Up @@ -101,7 +104,8 @@ def print_variables():
"BARK_URL": BARK_URL,
"BARK_EXTRA": BARK_EXTRA,
"ANPUSH_TOKEN": ANPUSH_TOKEN,
"ANPUSH_CHANNEL": ANPUSH_CHANNEL
"ANPUSH_CHANNEL": ANPUSH_CHANNEL,
"PUSH_METHOD": PUSH_METHOD
}
for var_name, var_value in variables.items():
logger.info(f"{var_name}: {var_value} - {type(var_value)}")
Expand All @@ -125,16 +129,23 @@ def send_post_request_and_save_response(url, data, headers):
retries += 1
logger.error("超过最大重试次数,请求失败。")
MESSAGE += "\n超过最大重试次数,请求失败。"
send_get_request(BARK_URL + MESSAGE + BARK_EXTRA)
asyncio.run(send_seat_result_to_channel())
send_message_anpush()
send_message()
sys.exit()


def send_message():
if PUSH_METHOD == "TG":
asyncio.run(send_message_telegram())
if PUSH_METHOD == "ANPUSH":
send_message_anpush()
if PUSH_METHOD == "BARK":
send_message_bark()


# 推送到 Bark
def send_get_request(url):
def send_message_bark():
try:
response = requests.get(url)
response = requests.get(BARK_URL + MESSAGE + BARK_EXTRA)
# 检查响应状态码是否为200
if response.status_code == 200:
logger.info("成功推送消息到 Bark")
Expand Down Expand Up @@ -164,7 +175,7 @@ def send_message_anpush():
# logger.info(response.text)


async def send_seat_result_to_channel():
async def send_message_telegram():
try:
# 使用 API 令牌初始化您的机器人
bot = Bot(token=TELEGRAM_BOT_TOKEN)
Expand Down Expand Up @@ -210,9 +221,7 @@ def check_book_seat():
name = entry["nameMerge"]
FLAG = True
MESSAGE += f"预约成功:你当前的座位是 {name} {seat_id}\n"
send_get_request(BARK_URL + MESSAGE + BARK_EXTRA)
asyncio.run(send_seat_result_to_channel())
send_message_anpush()
send_message()
break
elif entry["statusName"] == "使用中" and DATE == "today":
logger.info("存在正在使用的座位")
Expand Down Expand Up @@ -401,10 +410,10 @@ def rebook_seat_or_checkout():
if res is not None:
# 延长半小时,寻找已预约的座位
if MODE == "5":
current_time = datetime.datetime.now()
logger.info(current_time)
# current_time = datetime.datetime.now()
# logger.info(current_time)
for item in res["data"]["data"]:
if item["statusName"] == "预约开始提醒":
if item["statusName"] == "预约开始提醒" or item["statusName"] == "预约成功":
ids = item["id"] # 获取 id
space = item["space"] # 获取 seat_id
name_merge = item["nameMerge"] # 获取名称(nameMerge)
Expand All @@ -414,11 +423,9 @@ def rebook_seat_or_checkout():
cancel_seat(ids)
post_to_get_seat(space, segment)
else:
logger.error("没有找到已经预约的座位,你可能没有预约座位")
MESSAGE += "\n没有找到已经预约的座位,你可能没有预约座位"
send_get_request(BARK_URL + MESSAGE + BARK_EXTRA)
asyncio.run(send_seat_result_to_channel())
send_message_anpush()
logger.error("没有找到已经预约的座位")
MESSAGE += "\n没有找到已经预约的座位"
send_message()
sys.exit()
# 签退,寻找正在使用的座位
if MODE == "4":
Expand Down Expand Up @@ -456,18 +463,14 @@ def rebook_seat_or_checkout():
logger.info("签退状态:" + status)
if status == "完全离开操作成功":
MESSAGE += "\n恭喜签退成功"
send_get_request(BARK_URL + MESSAGE + BARK_EXTRA)
asyncio.run(send_seat_result_to_channel())
send_message_anpush()
send_message()
sys.exit()
else:
logger.info("已经签退")
else:
logger.error("没有找到正在使用的座位,今天你可能没有预约座位")
MESSAGE += "\n没有找到正在使用的座位,今天你可能没有预约座位"
send_get_request(BARK_URL + MESSAGE + BARK_EXTRA)
asyncio.run(send_seat_result_to_channel())
send_message_anpush()
send_message()
sys.exit()
# todo 没有遇到此错误
else:
Expand Down Expand Up @@ -506,9 +509,7 @@ def check_time():
if time_difference > 1200:
logger.info("距离预约时间过长,程序将自动停止。")
MESSAGE += "\n距离预约时间过长,程序将自动停止"
send_get_request(BARK_URL + MESSAGE + BARK_EXTRA)
asyncio.run(send_seat_result_to_channel())
send_message_anpush()
send_message()
sys.exit()
# 如果距离时间在合适的范围内, 将设置等待时间
elif time_difference > 30:
Expand Down

0 comments on commit f92d94d

Please sign in to comment.