最近在寫東西時發(fā)現(xiàn)一些文檔里圖片較多(自己一般都是用md寫東西),每次上傳時經常要花時間手動去拉圖上傳,簡書不能在拷貝md文檔時直接自動上傳所有圖片,非常麻煩;感覺簡書這方面做得也很差,完全沒體驗可言。還是自己動手寫個小工具吧,花了點時間研究下簡書的圖片上傳接口,寫個python腳本匹配文檔中的圖片位置,自動上傳后并替換圖片鏈接,以后可以省點事。
工具
Charles - 用來抓包分析圖片上傳數(shù)據(jù)報文流程(只在分析圖片上傳的HTTP包流程時用)
Chrome插件EditThisCookie - 用來導出簡書的cookies(導出格式為name=value pairs)
流程與代碼
用Chrome瀏覽器登錄簡書賬號,然后導出jianshu的cookies,保存為文件(導出格式為name=value pairs),文件名為cookies.txt并與腳本文件保存在同一目錄,用于上傳圖片時的會話驗證。簡書的cookies有效期好像是一月,時間夠用了。
-
運行腳本,替換完后會輸出新文件output.md
replace_md.py xxx.md -
源碼:
#!/usr/bin/env python3 import requests import json import os import sys import datetime import re import imghdr import _locale _locale._getdefaultlocale = (lambda *args: ['zh_CN', 'utf8']) cookie_file = 'cookies.txt' upload_url = 'https://upload.qiniup.com/' # get cookie from cookie file def getCookie(path): try: with open(path, 'r') as f: content = f.readlines() for line in content: cook = line.split(';') if len(cook) > 1: break cookies = '' for str in cook: if str.find('token') != -1: cookies += str+';' if str.find('uid') != -1: cookies += str+';' if str.find('sensorsdata') != -1: cookies += str+';' return cookies except Exception as error: print(error) def uploadImage(cook_path, filepath, name='img'): cookStr = getCookie(cook_path) #print(cookStr) filename = os.path.basename(filepath) fname,suffix=os.path.splitext(filepath) if suffix == '': suffix = imghdr.what(filepath) filename = fname+'.'+suffix token_url = 'http://www.itdecent.cn/upload_images/token.json?filename={}'.format(filename) headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36', 'Cookie': cookStr, } response = requests.get(token_url, headers=headers) response.encoding = response.apparent_encoding token_key = json.loads(response.text) if 'token' not in token_key: return None with open(filepath, 'rb') as file: files = { 'file': (filename, file), 'token': (None, token_key['token']), 'key': (None, token_key['key']), } response = requests.post(upload_url, headers=headers, files=files) response.encoding = response.apparent_encoding img_url = json.loads(response.text)['url'] img_md = ''.format(text=name, img_url=img_url) return img_md def outputFile(path): try: with open(path, 'r') as fd, open("output.md", "w") as fd1: tmpPath = '__tmp__' content = fd.readlines() for line in content: # match http link image obj = re.search( r'!\[(.*)\]\((http.+)\)', line) if obj: name = obj.group(1) filePath = obj.group(2) with open(tmpPath, 'wb') as tmpFd: ir = requests.get(filePath) tmpFd.write(ir.content) newline = uploadImage(cookie_file, tmpPath, name) if newline is None: print('Err: ', 'uploadImage() error!') else: print('Ok: ', 'uploadImage() ok!') line = newline fd1.write(line) continue # match local file image obj = re.search( r'!\[(.*)\]\((.+)\)', line) if obj: name = obj.group(1) filePath = obj.group(2) newline = uploadImage(cookie_file, filePath, name) if newline is None: print('Err: ', 'uploadImage() error!') else: print('Ok: ', 'uploadImage() ok!') line = newline fd1.write(line) if os.path.exists(tmpPath): os.remove(tmpPath) return None except Exception as error: print('err:', error) def main(path): if os.path.exists(path): outputFile(path) return True else: print("File path %s not exist!" % (path)) return False if __name__ == '__main__': if len(sys.argv) <= 1: print("Usage error: replace_md.py %path") sys.exit(-1) else: main(sys.argv[1]) sys.exit(0)基本流程:
- 使用正則匹配出md文檔中的圖片位置
- 根據(jù)圖片位置的鏈接
- 如果是本地圖片地址,直接上傳,返回圖片鏈接
- 如果是網絡圖片鏈接,先下載再上傳,返回圖片鏈接
- 用返回的圖片鏈接替換md文檔中原來的圖片位置