當(dāng)前位置:首頁 > IT技術(shù) > Web編程 > 正文

Markdown圖床任意轉(zhuǎn)換到其他圖床或其他上傳接口
2022-04-25 23:14:16

免費圖床經(jīng)常自動刪除圖片,所以這里把全部筆記轉(zhuǎn)到語雀或者其他,本地圖片和網(wǎng)絡(luò)圖片均可。

使用方法就是抓取上傳的接口然后cookie等然后替換,把XX.MD文件和腳本放在一個文件夾就可以了。

from multiprocessing.spawn import prepare
import re
import sys
from turtle import up
import requests
import os
def md_img_find():
    try:
        post = None
        path=sys.path[0]
        files=os.listdir(path)
        for file in files:
            if not os.path.isdir(file) and ".md" in file: 
                print("當(dāng)前處理的文件:"+file)  
                with open(file, 'r',encoding='utf-8') as f:
                    post = f.read()
                    matches = re.compile('!\[.*?\]\((.*?)\)|<img.*?src=['"](.*?)['"].*?>').findall(post)     # 匹配md文件中的圖片
                    if matches and len(matches) > 0:
                        for sub_match in matches:       # 正則里有個或,所以有分組,需要單獨遍歷去修改   
                            for match in sub_match:     # 遍歷去修改每個圖片
                                if match and len(match) > 0:
                                    print("match pic : ", match)
                                    if re.match('((http(s?))|(ftp))://.*', match):  # 判斷是不是已經(jīng)是一個圖片的網(wǎng)址
                                        res_url=upload_net(match)
                                        post = post.replace(match, res_url)
                                    else:
                                        res_url=upload_local(match)
                                        print(res_url)
                                        post = post.replace(match, res_url)
                        if post: open(file, 'w',encoding='utf-8').write(post)
    except Exception as e:
        print(e)

def upload(body):
    try:
        headers={
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
            'Cookie':'xx',
            'Referer': 'https://www.yuque.com/x'
        }    
        response = requests.post(url='文件上傳接口',headers=headers,files=body)
        pattern = re.compile(r'https(.+?).png')#提取上傳成功的接口
        content=response.content.decode()
        return "https"+pattern.findall(content)[0]+".png"
    except Exception as e:
        print(e)

def upload_net(match):
    try:
        ip="127.0.0.1"
        port="7890"
        proxies = {
                "http": "http://"+ip+':'+port,
                "https": "https://"+ip+':'+port
                } 
        body = {
                    'image_file': ("image.jpg", requests.get(match,proxies=proxies).content, 'image/png')
                    # 代理是為了例如sm.sm圖床 訪問慢
                    }
        return upload(body)
    except Exception as e:
        print(e)

def upload_local(img_name):
	with open(img_name, "rb")as f_abs:# 以2進制方式打開圖片
		body = {
			'image_file': ('image.png', f_abs, 'image/png')
			
			}
		return upload(body)
        
if __name__=='__main__':
    md_img_find()

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >