一直懒没搞这个东西,终于抽空搞了一下,最后效果:每天检查star项目,每8小时同步镜像代码

  • 搭建gitea或gogs
  • 启动crontab定时任务

python3脚本如下

# -*- coding: utf-8 -*-

import importlib
import sys

import requests

num = 0

importlib.reload(sys)


# sys.setdefaultencoding("utf8")


def push_star(url, name, desc):
    try:
        # gitea api地址,用于创建镜像项目
        baseUrl = "http://10.10.10.10/api/v1/repos/migrate?access_token=这里修改key"
        payload = (
                "{\"clone_addr\": \"%s\",  \"mirror\": true,  \"repo_name\": \"%s\", \"service\": \"github\",\"uid\": 1,\"description\": \"%s\"}" % (
            url, name, desc))
        print(payload)
        headers = {
            'Accept': 'application/json',
            'authorization': '这里填写basic授权',
            'Content-Type': 'application/json',
            'Cookie': 'lang=en-US; i_like_gitea=8161faea745c9885; _csrf=xS11Xj36QSWLD9Gaatf1eA46D9o6MTYwNTExNDYyMzYzNjU3MTI4MA'
        }
        response = requests.request("POST", baseUrl, headers=headers, data=payload.encode('utf-8'),
                                    timeout=600)
        code = response.status_code
        if code == 409:
            print(response.json()['message'])
        else:
            print(response.content)
    except requests.exceptions.RequestException as e:
        print(e)


def parse_data(stars):
    global num
    for star in stars:
        num += 1
        url = star['clone_url']
        name = star['full_name'].replace('/', '-')
        desc = star['description']
        print('start push:' + name)
        push_star(url, name, desc)
        print('end push:' + name)
    return


def get_data(url):
    response = requests.get(url)
    result = response.json()
    parse_data(result)
    print(response.status_code)
    if 'next' in response.links.keys():
        nextUrl = response.links['next']['url']
        print(nextUrl)
        get_data(nextUrl)
    else:
        print('no')
    return


getData("https://api.github.com/users/这里是github用户名/starred?per_page=80")
print(num)