搜 索

Python使用Github免费图床

  • 2.2k阅读
  • 2022年05月17日
  • 0评论
首页 / 技术 / 正文

简介

github的空间是免费的,而cdn.jsdelivr.net的github文件加速也是免费的,那么就可以把图片上传到github上,然后把链接使用jsdelivr加速,来达到图床的目的。虽然速度慢,但是是免费的啊!

教程

https://docs.github.com/cn/github/authenticating-to-github/creating-a-personal-access-token

代码

import json
import random
import time
from ipaddress import IPv4Address
import requests

class Github(object):
    __githubUserName = ''
    __githubRepository = ''
    __githubAccessToken = ''
    __githubEmail = ''

    def __init__(self):
        self.__githubUserName = "abigsoft"
        self.__githubRepository = "bed"
        self.__githubAccessToken = "ghp_******"
        self.__githubEmail = "***"

        self.params = {
            "access_token": self.__githubAccessToken
        }
        self.headers = {
            'Content-Type': 'application/json',
            "Authorization": "token {}".format(self.__githubAccessToken)
        }

    def checkRepoSize(self):
        """
        检查仓库大小,考虑到后续超1G后切换仓库
        :return:
        """
        url = "https://api.github.com/repos/{}/{}".format(self.__githubUserName, self.__githubRepository)
        self.headers["X-Forwarded-For"] = str(IPv4Address(random.getrandbits(32)))
        try:
            response = requests.request("GET", url, headers=self.headers)
            assert response.status_code == 200
            return int(response.json()["size"])
        except Exception as e:
            return None

    def createRepo(self):
        """
        新建github仓库
        :return:
        """
        url = "https://api.github.com/{}/repos".format(self.__githubUserName)
        self.headers["X-Forwarded-For"] = str(IPv4Address(random.getrandbits(32)))
        payload = {
            "name": "{}{}".format(self.__githubRepository, random.randint(1, 10))
        }

        try:
            response = requests.request("GET", url, headers=self.headers, data=json.dumps(payload))
            assert response.status_code == 200
        except Exception as e:
            return None

    def uploadFile(self, path, name, Content):
        url = "https://api.github.com/repos/{}/{}/contents/{}/{}".format(self.__githubUserName, self.__githubRepository,path, name)
        payload = {
            "message": "upload by spider",
            "committer": {
                "name": self.__githubUserName,
                "email": self.__githubEmail
            },
            "content": bytes.decode(Content)
        }
        self.headers["X-Forwarded-For"] = str(IPv4Address(random.getrandbits(32)))
        i = 0
        response = requests.request("PUT", url, headers=self.headers, data=json.dumps(payload))
        if response.status_code == 201 or response.status_code == 200:
            url = "https://cdn.jsdelivr.net/gh/{}/{}/{}/{}".format(self.__githubUserName, self.__githubRepository, path, name)
            return url

调用方式

Github().uploadFile('文件路径', "文件名称", base64.b64encode("图片content"))
打 赏
  • 支付宝
  • 微信
  • QQ
Alipay
WeChatPay
QQPay
评论区
暂无评论
avatar