GithubHelp home page GithubHelp logo

swfoodt / reportpeace Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gomqiqi/reportpeace

1.0 0.0 0.0 38 KB

北林自动报平安脚本(在GitHub Action自动执行)

License: MIT License

Python 100.00%

reportpeace's Introduction

北林自动报平安脚本

原创文章,文章地址:https://jetzihan.netlify.app/blog/rpeace

仅供学习使用,任何恶意使用与本人无关

调用报平安接口并使用 GitHub Actions 自动完成北林每日报平安。本脚本 最大的特点是不需要你有一台一直开着的主机或者服务器,让 GitHub 完成这件事即可。

主逻辑来自 @Memory

食用指南

Fork 仓库

进入本仓库,点击右上角的 Fork 按钮,将本仓库 Fork 到自己的账号下。 顺便点击 star按钮收藏(为我点个赞)。

1

注意,此时这个仓库就是你的了,你在你的仓库密匙中填写的任何内容我均无法获取,请放心使用。

添加个人信息

在报平安代码中,有许多涉及隐私的东西。比如学号、密码、用户ID等。我们不希望这些东西在源代码被公开,所以我们需要把它们添加到 GitHub Secrets 中。

下面举一个例子:

在上面我们需要把学号存储为密匙,在代码中,它的变量名为 STUDENT_ID 。 打开你 Fork 后你的仓库。点击 Settings,然后点击 Secrets,点击 New repository secret,在 Name 中输入 STUDENT_ID,在 Value 中输入你的学号,点击 Add secret

2

3

4

5

然后点击添加。

按照上述步骤,你还需要添加下面内容,这些内容打开你平时报平安的界面,获取相同的即可:

STUDENT_ID # 学号,示例:201002401
PASSWORD # 密码,示例:abc123123123,也就是你的校园网密码
NAME # 姓名,示例:张三
SCHOOL # 学院,示例:信息学院
MAJOR # 专业,示例:计算机科学与技术
TYPE # 类型,示例:本科生 | 研究生
PHONE # 手机号,示例:12345678901
MASTER # 审核人,示例:李四
BUILDING # 宿舍楼栋,示例:11号楼
ROOM # 宿舍号,示例:901

以上添加完成后如图所示:

2

修改定时【可选】

yml 脚本中,我已经设置了每天 0:11 执行,为保证准确性,还添加了二次执行时间为早上 8:10. 不选择整点执行而选择在11分执行是为了避开高峰期。 你可以对其修改。在 .github/workflows 文件夹中,打开 main.yml 文件,修改 cron 的值即可。

name: autoReportPeace

on:
  repository_dispatch:
    types: [morning]
  schedule:
    - cron: '11 16,0 * * *'
      # UTC 时间 16:11和 0:11对北京时间 0:11和 8:11
  workflow_dispatch:
jobs:
#......

GitHub 采用世界标准时间,也就是 UTC 时间。北京时间是 UTC+8,因此北京时间比 UTC 时间早8小时。所以 UTC 0:00 对应的北京时间是 8:00。
也就是说,我们想要的时间减去8小时才是 UTC 时间。

corn的语法如下:

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
│ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
│ │ │ │ │                                   
│ │ │ │ │
│ │ │ │ │
* * * * *

例如,我们希望早上九点半执行,那么我们需要设置为 1:30:

corn: '30 1 * * *'

或者我们希望每天8:00和20:00执行:

corn: '0 0,12 * * *'

或者我们只希望每周三周五执行:

corn: '30 1 * * 3,5'

手动测试

除了定时测试外,我们还支持手动进行测试。你只需要打开 GitHub Action 界面。选中左侧的 autoReportPeace,点击右侧的下拉栏中的 Run workflow,即可手动执行。

2

3

接着,会有黄色的运行进程开始,直到进程变成蓝色的勾,表示运行成功。

3

延迟一到两分钟,你的微信会收到学校企业微信的报平安通知。表示部署成功,如果 不如预期,请检查上述步骤。

腾讯云加强定时【选做】

提高定时的准确性,可以不做。

但是有一个缺陷,就是 GitHub Action 的定时是不准确的,有时候会延迟十分钟甚至一两个小时。 不过我们通过两个时间点的定时,可以很大程度保证准确性。

2

Note: The event can be delayed during periods of high loads of GitHub Actions workflow runs. High load times include the start of every hour. To decrease the chance of delay, schedule your workflow to run at a different time of the hour.schedule

注意:在 GitHub Actions 工作流高负载运行期间,事件可能会延迟。高负载时间包括每小时的开始时等。为了减少延迟的可能性,请安排工作流在每小时的不同时间运行。

实测使用 GitHub Pages 的定时服务,延迟在 10 分钟到 1 小时不等,极端条件下定时不会执行

而 GitHub Actions 支持 workflow_dispatch 触发器(请参阅 GitHub Docs 上的触发工作流的事件,因此如果您手动触发工作流,它将立刻执行。这意味着您可以使用第三方 cron 调度服务,如 腾讯云、IFTTT、Google Cloud Scheduler 等,向 GithubAPI 发出请求以触发工作流。

那么这里将讲授一种使用腾讯云云函数定时触发的方法,增强其可靠性。如果你认为 GitHub定时两个时间点已经足够可靠,可以不再进行此操作

获取 GitHub Token

想要从远端操作 GitHub,我们需要一个 GitHub Token。

在你的 Github 主页,选择 setting
1
选择左下角的Developer settings
2
然后选择 Personal access tokens
3
填写相关信息,选择 无限期 , 勾选 repoworkflow 。点击创建。
4
然后生成完成,之后它会生成一串密码:
5
在你的 Github 主页,选择 setting
1
选择左下角的Developer settings
2
然后选择 Personal access tokens
3
填写相关信息,选择 无限期 , 勾选 repoworkflow 。点击创建。
4
然后生成完成,之后它会生成一串密码:
5

注意
请记住这一串密码,你将不再能看到它。

创建云函数

创建腾讯云函数

进入 腾讯云 并点击注册。可以使用微信登录。点击右上角进入控制台,搜索 云函数

2

点击新建云函数:

3

不选择模板,从头开始。

3

填写相关参数,选择环境 python

3

提高超时时间。

3

清空原有的代码,填入新的代码:

2

代码结构如下,你只需要修改 TOKEN (上面一步获取的) 这几参数以及 https://api.github.com/repos/你的用户名/reportPeace/dispatches 中的 GitHub 用户名即可。

# 腾讯云执行脚本,如不需要可忽略
import requests


def run():
    headers = {
        'Accept': 'application/vnd.github+json',
        'Authorization': 'token 你的TOKEN',
    #     注:'token' 前缀必不可少 
    }

    data = '{"event_type": "autoReportPeace"}'

    response = requests.post(f'https://api.github.com/repos/你的用户名/reportPeace/dispatches',
                             headers=headers, data=data)


# 云函数入口
def main_handler(event, context):
    return run()

点击启用日志。

2

然后点击完成。进入详细界面的代码界面,点击测试。

3

查看下面的日志:

3

如果出现 204,则表示成功。其他状态码均为失败。

然后到 GitHub 仓库查看 Actions,可以看到已经成功触发了工作流。

2

勾变为蓝色,表示成功。

设置云函数定时触发

在侧边中创建定时:

2

点击创建触发器。

3

其中填写 Cron 表达式。

0 0 8 * * * *   # 每天 8 点
0 0 18 * * * *  # 每天 18 点
0 0 7 * * 1-5 * # 周一到周五 7 点
0 30 6 * * * *  # 每天 6 点半

点击提交后,会在每天指定时间触发执行。注意,腾讯云与 GitHub 不同,腾讯云 使用的是北京时间,这意味着你并不需要考虑时区问题。

一个函数而已,并不会消耗多少资源,所以不用担心。

reportpeace's People

Contributors

inannan423 avatar swfoodt avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.