Skip to content

paracraft download page

DreamAndDead edited this page Sep 4, 2017 · 1 revision

paracraft 下载页迁移

下载过程

原有的过程

  1. 连接 http://www.paracraft.cn/?lang=zh ,点击下载

  2. 转到 http://www.paracraft.cn/download?lang=zh ,再点击三个不同版本的下载按钮

  3. 都会转到同一个地址 http://www.nplproject.com/zh/download/download-paracraft ,再选择相应的下载

新的过程(理想)

http://www.paracraft.cn/?lang=zh 直接到 http://keepwork.com/dreamanddead/downloads/index

dreamanddead为自己的个人帐户,实际上线的时候,要使用存放官方页面的一个帐户,帐户名可能为 paracraft?official?

gitlab api探索

ps: 在keepwork.com每注册一个新用户,都会在gitlab.keepwork.com生成一个同样帐号密码的帐户,存储了所有keepwork的页面数据

实际的下载页面是作为个人网站的一个single page,每个page都是一个markdown文件,都存储于gitlab后端。想要自动化的更新页面,就需要用代码进行md文件更新的操作。

实际上,每一个用户编辑页面并保存,都是git的一个新的提交,提交的增量为页面的改动。

在gitlab访问刚才的下载页: http://gitlab.keepwork.com/gitlab_rls_dreamanddead/keepworkdownloads/blob/master/dreamanddead/downloads/index.md

好在gitlab提供了api来对数据进行操作。

前提

每个帐户都有一个private token,在进行api操作时必须在http header中带上token. ref: https://docs.gitlab.com/ee/api/README.html#private-tokens

commit api

ref: https://docs.gitlab.com/ee/api/commits.html#create-a-commit-with-multiple-files-and-actions

用api来创建提交,是我们最终的目的。根据api的要求,创建http post

POST /projects/:id/repository/commits

这里的id标识了是哪一个project。每一个project都是一个git仓库,每一个页面都存在于一个git repo中。所以project+path可以定位每一个页面。

post数据根据api要求,update操作可以满足我们的需求

{
  "branch": "master",
  "commit_message": "update download page from CI",
  "actions": [
    {
      "action": "update",
      "file_path": "dreamanddead/downloads/index.md",
      "content": "page content here"
    }
  ]
}

content是我们每次更新的页面内容

project id ?

上面的逻辑非常清楚,剩下的问题在于,如何得到 project id

ps: 虽然我们可以用新申请的keepwork帐户登陆gitlab.keepwork.com,但是实际在gitlab后台的用户名为 gitlab_rls_${keepwork_name}

从用户名开始,得到用户id,ref:https://docs.gitlab.com/ee/api/users.html#for-admins

以我的用户名dreamanddead为例

GET /users?username=gitlab_rls_dreamanddead

返回

[
    {
        "name": "gitlab_rls_dreamanddead",
        "username": "gitlab_rls_dreamanddead",
        "id": 1630,
        "state": "active",
        "avatar_url": "http://www.gravatar.com/avatar/19969b73d47d4ac197141bcc2e965193?s=80&d=identicon",
        "web_url": "http://git.keepwork.com/gitlab_rls_dreamanddead"
    }
]

用户id为1630

进一步,取得用户的项目信息,ref:https://docs.gitlab.com/ee/api/projects.html#list-a-user-39-s-projects

GET /users/1630/projects

返回

[
    {
        "id": 3432,
        "description": null,
        "default_branch": "master",
        "tag_list": [],
        "archived": false,
        "visibility": "public",
        "ssh_url_to_repo": "git@git.keepwork.com:gitlab_rls_dreamanddead/keepworkdownloads.git",
        "http_url_to_repo": "http://git.keepwork.com/gitlab_rls_dreamanddead/keepworkdownloads.git",
        "web_url": "http://git.keepwork.com/gitlab_rls_dreamanddead/keepworkdownloads",
        "owner": {
            "name": "gitlab_rls_dreamanddead",
            "username": "gitlab_rls_dreamanddead",
            "id": 1630,
            "state": "active",
            "avatar_url": "http://www.gravatar.com/avatar/19969b73d47d4ac197141bcc2e965193?s=80&d=identicon",
            "web_url": "http://git.keepwork.com/gitlab_rls_dreamanddead"
        },
        "name": "keepworkdownloads",
        "name_with_namespace": "gitlab_rls_dreamanddead / keepworkdownloads",
        "path": "keepworkdownloads",
        "path_with_namespace": "gitlab_rls_dreamanddead/keepworkdownloads",
        "container_registry_enabled": true,
        "issues_enabled": true,
        "merge_requests_enabled": true,
        "wiki_enabled": true,
        "jobs_enabled": true,
        "snippets_enabled": true,
        "created_at": "2017-08-31T07:00:37.569Z",
        "last_activity_at": "2017-09-04T06:52:00.315Z",
        "shared_runners_enabled": true,
        "lfs_enabled": true,
        "creator_id": 1630,
        "namespace": {
            "id": 1874,
            "name": "gitlab_rls_dreamanddead",
            "path": "gitlab_rls_dreamanddead",
            "kind": "user",
            "full_path": "gitlab_rls_dreamanddead",
            "parent_id": null
        },
        "import_status": "none",
        "avatar_url": null,
        "star_count": 0,
        "forks_count": 0,
        "open_issues_count": 0,
        "public_jobs": true,
        "ci_config_path": null,
        "shared_with_groups": [],
        "only_allow_merge_if_pipeline_succeeds": false,
        "request_access_enabled": true,
        "only_allow_merge_if_all_discussions_are_resolved": false,
        "printing_merge_request_link_enabled": true,
        "permissions": {
            "project_access": {
                "access_level": 40,
                "notification_level": 3
            },
            "group_access": null
        }
    },
    ....
    ....
    ....
    ....
    ....
    ....
    ....
]

project id即为 3432

运行脚本

脚本地址: https://github.com/tatfook/KeepMonitor/tree/master/paracraft/download-page

脚本的进行逻辑很简单

  1. 取得最新的版本号
  2. 用版本号与相关下载地址,与页面模板,生成下载页内容
  3. 更新下载页

其中用模板生成下载页的内容,委托给python3脚本来写,bash在这方面太弱

Clone this wiki locally