Typora編寫markdown插入圖片時自動上傳圖片到博客園
1.登錄博客園后獲取cookie值
Cookie的key為`.Cnblogs.AspNetCore.Cookies`,僅此一個就足夠了
2.上腳本代碼
# upload_image2cnblogs.py
# 支持本地圖片及網絡圖片
import re
import requests
import sys
from pathlib import Path
from uuid import uuid4
ip_middle_octet = r"(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5]))"
ip_last_octet = r"(?:\.(?:0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5]))"
regex = re.compile( # noqa: W605
r"^"
# protocol identifier
r"(?:(?:https?|ftp)://)"
# user:pass authentication
r"(?:[-a-z\u00a1-\uffff0-9._~%!$&'()*+,;=:]+"
r"(?::[-a-z0-9._~%!$&'()*+,;=:]*)?@)?"
r"(?:"
r"(?P<private_ip>"
# IP address exclusion
# private & local networks
r"(?:(?:10|127)" + ip_middle_octet + r"{2}" + ip_last_octet + r")|"
r"(?:(?:169\.254|192\.168)" + ip_middle_octet + ip_last_octet + r")|"
r"(?:172\.(?:1[6-9]|2\d|3[0-1])" + ip_middle_octet + ip_last_octet + r"))"
r"|"
# private & local hosts
r"(?P<private_host>"
r"(?:localhost))"
r"|"
# IP address dotted notation octets
# excludes loopback network 0.0.0.0
# excludes reserved space >= 224.0.0.0
# excludes network & broadcast addresses
# (first & last IP address of each class)
r"(?P<public_ip>"
r"(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])"
r"" + ip_middle_octet + r"{2}"
r"" + ip_last_octet + r")"
r"|"
# IPv6 RegEx from https://stackoverflow.com/a/17871737
r"\[("
# 1:2:3:4:5:6:7:8
r"([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|"
# 1:: 1:2:3:4:5:6:7::
r"([0-9a-fA-F]{1,4}:){1,7}:|"
# 1::8 1:2:3:4:5:6::8 1:2:3:4:5:6::8
r"([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|"
# 1::7:8 1:2:3:4:5::7:8 1:2:3:4:5::8
r"([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|"
# 1::6:7:8 1:2:3:4::6:7:8 1:2:3:4::8
r"([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|"
# 1::5:6:7:8 1:2:3::5:6:7:8 1:2:3::8
r"([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|"
# 1::4:5:6:7:8 1:2::4:5:6:7:8 1:2::8
r"([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|"
# 1::3:4:5:6:7:8 1::3:4:5:6:7:8 1::8
r"[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|"
# ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::
r":((:[0-9a-fA-F]{1,4}){1,7}|:)|"
# fe80::7:8%eth0 fe80::7:8%1
# (link-local IPv6 addresses with zone index)
r"fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|"
r"::(ffff(:0{1,4}){0,1}:){0,1}"
r"((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}"
# ::255.255.255.255 ::ffff:255.255.255.255 ::ffff:0:255.255.255.255
# (IPv4-mapped IPv6 addresses and IPv4-translated addresses)
r"(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|"
r"([0-9a-fA-F]{1,4}:){1,4}:"
r"((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}"
# 2001:db8:3:4::192.0.2.33 64:ff9b::192.0.2.33
# (IPv4-Embedded IPv6 Address)
r"(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])"
r")\]|"
# host name
r"(?:(?:(?:xn--)|[a-z\u00a1-\uffff\U00010000-\U0010ffff0-9]-?)*"
r"[a-z\u00a1-\uffff\U00010000-\U0010ffff0-9]+)"
# domain name
r"(?:\.(?:(?:xn--)|[a-z\u00a1-\uffff\U00010000-\U0010ffff0-9]-?)*"
r"[a-z\u00a1-\uffff\U00010000-\U0010ffff0-9]+)*"
# TLD identifier
r"(?:\.(?:(?:xn--[a-z\u00a1-\uffff\U00010000-\U0010ffff0-9]{2,})|"
r"[a-z\u00a1-\uffff\U00010000-\U0010ffff]{2,}))"
r")"
# port number
r"(?::\d{2,5})?"
# resource path
r"(?:/[-a-z\u00a1-\uffff\U00010000-\U0010ffff0-9._~%!$&'()*+,;=:@/]*)?"
# query string
r"(?:\?\S*)?"
# fragment
r"(?:#\S*)?"
r"$",
re.UNICODE | re.IGNORECASE
)
pattern = re.compile(regex)
def is_url(value, public=False):
result = pattern.match(value)
if not public:
return result
return result and not any(
(result.groupdict().get(key) for key in ('private_ip', 'private_host'))
)
argv = sys.argv
if len(argv) <= 1:
sys.exit(0)
arg = sys.argv[1]
if not is_url(arg):
img_path = Path(arg)
if not img_path.exists() or img_path.is_dir():
sys.exit(0)
else:
img_name = img_path.name
img_content = img_path.open('rb')
content_type = f'image/{img_path.suffix[1:]}'
else:
res = requests.get(arg)
content_type = res.headers['Content-Type']
if 'image' in content_type:
temp_list = arg.split('/')
img_name = temp_list[-1] if temp_list else uuid4().hex
img_content = res.content
else:
sys.exit(0)
headers = {
'x-mime-type': content_type,
}
cookies = {
'.Cnblogs.AspNetCore.Cookies': 'Your Cookie',
}
upload_url = f"http://upload.cnblogs.com/imageuploader/processupload?host=www.cnblogs.com&qqfile={img_name}"
img = {"qqfile": (img_name, img_content, content_type)}
res = requests.post(upload_url, files=img, cookies=cookies, headers=headers)
if res.status_code == 200:
data = res.json()
success = data['success']
message = data['message']
if success:
print('Upload Success:')
print(message)
3.設置Typora
-
上傳服務選擇Custom Command
-
命令為
python(或其他虛擬環境python解釋器路徑) py腳本絕對路徑
,Typora會自動往腳本里傳參圖片的絕對路徑,參數的長度和參數的個數都是不確定的 -
圖片上傳完成后,Typora 需要你按指定格式在控制台輸出圖片的 URL 給它
Upload Success: https://stormbuf.coding.net/p/piccoding/d/piccoding/git/raw/master/typora-icon2.png
第一行一定要是
Upload Success:
,然后第二個非空行開始,一行一個圖片 URL -
插入圖片時:這個選項可以選擇上傳圖片或者選擇無特殊操作
- 選擇上傳圖片時當你選擇插入本地圖片時會自動把圖片上傳到博客園並返回上傳后的圖片地址
- 選擇無特殊操作時,當你選擇插入本地圖片時會自動提示你進行其他操作,比如上傳圖片等
-
上傳圖片時,如有報錯,請仔細查看報錯信息,如無明顯報錯信息,請檢查Cookie是否已失效.
4.總結
當前腳本是上傳圖片到博客園做圖床,目前只適用於當前博客園的上傳圖片接口,腳本是否過期,自行判斷。此腳本只是做了個引子,讀者照葫蘆畫瓢也可以上傳其它OSS或者圖床等。