五、圖牀(github+jsDelivr)選擇,圖片上傳並生成鏈接

概述

拿到淘寶商品評論圖片後,需要上傳到圖牀進行顯示,找到了兩個免費的圖牀github和微博:

圖牀 優點 缺點
github github提供RESTful api接口 可以使用jsDelivr加速 訪問速度比較慢,上傳比較慢
weibo 訪問速度較快 只能一張一張上傳 而且有的賬號不支持外鏈

實現

源碼文件

源碼

文件 介紹
github.py 封裝github RESTful API
main.py 啓動程序
settings.py 設置
uploader.py 使用github API上傳圖片

main.py

# -*-coding: utf8 -*-
import os

import settings
from github import GitHub
from uploader import Uploader

#如果圖片不存在,則直接退出
if os.path.exists(settings.STORE_PATH) == False:
    print('download folder not exists')
    exit(1)
#創建GitHub Api
gh = GitHub(access_token=settings.TOKEN)
#切換工作目錄
downloadFolder = os.getcwd() + '\\' + settings.STORE_PATH
os.chdir(downloadFolder)
#循環上傳文件
for f in os.listdir(downloadFolder):
    Uploader(downloadFolder,f,gh).upload()
    os.chdir('..')
#重新切換工作目錄
os.chdir('..')

uploader.py

# coding=utf-8
import os
import base64
import json

import settings
from github import GitHub

class Uploader(object):

    def __init__(self,root,folder,gh):
        self._root = root
        self._folder = folder
        self._gh = gh

    def upload(self):
        fullPath = os.path.join(self._root, self._folder)
        if os.path.isdir(fullPath) != True:
            return
        os.chdir(fullPath)
        print('start iterate '+fullPath)

        if os.path.exists(fullPath+'/filelist.txt') != True:
            print('filelist not exist')
            txtFp = open(fullPath+'/filelist.txt','w+')
        else:   
            txtFp = open(fullPath+'/filelist.txt','a+')

        for f in os.listdir(fullPath):
            fullPathFile = os.path.join(fullPath,f)
            if os.path.isfile(fullPathFile) == False:
                continue
            ext = os.path.splitext(f)
            if len(ext) < 2:
                continue
            if ext[1] in settings.FILTER:
                print('start uploadFile')
                figure = self.uploadFile(fullPathFile,f)
                if len(figure) > 0:
                    txtFp.write(figure)
                    txtFp.write('\r\n')
    
    def uploadFile(self,fullPathFile,file):
        try:
            with open(fullPathFile,'rb') as fp:
                byte = fp.read()
                base64Byte = base64.b64encode(byte).decode('utf-8')
                fp.close()
                print('start put contents ' + file)
                hUrl = self.ghContentsPut(file,base64Byte)
                if len(hUrl) > 0:
                    cdnUrl = settings.CDN_JSDELIVR + self._folder + '/' + file[3:]   
                    figure = '<figure class="wp-block-image"><img src="' + cdnUrl + '" alt=""/></figure>'
                    return figure
                return ''
        except FileNotFoundError:
            print('FileNotFoundError '+fullPathFile)
            return ''
    
    def ghContentsPut(self,file,base64Byte):
        try:
            h = self._gh.repos(settings.OWNER)(settings.REPO).contents(self._folder+'/'+file[3:]).put(message='update by hhs',content=base64Byte)
        except GitHub.ApiNotFoundError:
            print('ApiNotFound Error ' + self._folder+'/'+file)
            return ''
        except GitHub.ApiError:
            print('ApiError ' + self._folder+'/'+file)
            return ''
        except GitHub.ApiAuthError:
            print('ApiAuthError ' + self._folder+'/'+file)
            return ''
        except GitHub.AttributeError:
            print('AttributeError ' + self._folder+'/'+file)
            return ''
        return h.content.download_url

參考

GitHub API v3 | GitHub Developer Guide
A simple & beautiful tool for pictures uploading built by electron-vue https://molunerfinn.com/PicGo/
A simple GitHub v3 API SDK for Python
What is a “callable”?
Github+jsDelivr+PicGo 打造穩定快速、高效免費圖牀

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章