本地需要有python3环境
按照 requests、psutil、 requests-toolbelt 依赖,若已安装可忽略
1 2 3
| pip install requests pip install psutil pip install requests-toolbelt
|
复制以下代码到一个.py的文件中,如我本地命名为 upd_pic_to_csdn.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| ''' @文件 :csdnUpload.py @时间 :2022/11/24 12:48:46 @作者 :tx @版本 :2.0 @说明 :Typora利用csdn作为图床 ''' from __future__ import print_function import random from io import BytesIO import sys from PIL import Image import os from threading import Thread import psutil import re import requests import http.cookiejar as cookielib import time import requests from requests_toolbelt import MultipartEncoder import ctypes
os.chdir(os.path.expanduser('~')) requests.packages.urllib3.disable_warnings() session = requests.session()
class show_code(Thread): def __init__(self, url): Thread.__init__(self) self.url = url
def run(self): response = requests.get(self.url) img = Image.open(BytesIO(response.content)) img.show()
def IsLogin(): ''' 文件读取cookie,检测cookie是否合法。 ''' try: session.cookies = cookielib.LWPCookieJar(filename=".cookie/csdn.txt") session.cookies.load()
url = "https://imgservice.csdn.net/direct/v1.0/image/upload?type=blog&rtype=markdown&x-image-template=&x-image-app=direct_blog&x-image-dir=direct&x-image-suffix=png" response = session.get(url) if response.json()['msg'] == "success": return True else: return False except Exception as e: return False
def Login(): ''' csdn自动登录,同时保存cookie ''' response = session.get('https://open.weixin.qq.com/connect/qrconnect?appid=wx0ae11b6a28b4b9fc&scope=snsapi_login&redirect_uri=https%3A%2F%2Fpassport.csdn.net%2Fv1%2Fregister%2FpcAuthCallBack%3FpcAuthType%3Dweixin&state=csdn&login_type=jssdk&self_redirect=default&style=white&href=https://csdnimg.cn/release/passport/history/css/replace-wx-style.css', verify=False) uuid = re.findall( '<img class="qrcode lightBorder" src="(.*?)"', response.text)[0] img_url = 'https://open.weixin.qq.com' + uuid t = show_code(img_url) t.start() t.join()
uuid = uuid.split('/')[-1] url = 'https://lp.open.weixin.qq.com/connect/l/qrconnect?uuid='+uuid while True: response = session.get(url, verify=False) code = re.findall("window.wx_code='(.*?)'", response.text) if code != ['']: for proc in psutil.process_iter(): try: if proc.name() == "Microsoft.Photos.exe": proc.kill() except Exception as e: pass break time.sleep(0.5)
url = 'https://passport.csdn.net/v1/register/pcAuthCallBack?pcAuthType=weixin&code=%s&state=csdn' % code[0] session.get(url) session.cookies.save() IsLogin()
def UploadPic(picList: list): ''' 图片上传 '''
for pic in picList: url = "https://imgservice.csdn.net/direct/v1.0/image/upload?type=blog&rtype=markdown&x-image-template=&x-image-app=direct_blog&x-image-dir=direct&x-image-suffix=png" response = session.get(url) key = response.json()['data'] upload_url = key['host'] fields = { 'key': key['filePath'], 'policy': key['policy'], "OSSAccessKeyId": key['accessId'], "success_action_status": "200", "signature": key['signature'], "callback": key['callbackUrl'], 'file': (os.path.basename(pic), open(pic, 'rb'), "image/png"), } multipart_encoder = MultipartEncoder( fields, boundary='-----------------------------' + str(random.randint(1e28, 1e29 - 1))) headers = { 'content-Type': multipart_encoder.content_type, } res = requests.post(upload_url, headers=headers, data=multipart_encoder, verify=False) if (res.status_code == 200): print(res.json()['data']['imageUrl'])
if __name__ == "__main__":
if not os.path.exists(".cookie"): os.mkdir(".cookie") if not os.path.exists(os.path.join(".cookie", "csdn.txt")): tmep_f = open(os.path.join(".cookie", "csdn.txt"), 'w+') tmep_f.close() if not IsLogin(): Login() UploadPic(sys.argv[1:])
|
打开Typora 文件 =》 偏好设置 =》图像 设置界面,按照下图所示进行配置
注意修改命令中的python文件所在目录位置
1
| python D:\Files\Typora\upd_pic_to_csdn.py
|
设置好后点击验证图片上传选项进行验证服务是否正常
首次执行命令会弹出微信扫码登录的二维码,扫描登录即可
查看验证结果
注意
图片有违规校验,例如二维码
另外,CSDN的图片有防盗链,在其他网站引用图片可能无法加载
相关参考
typora使用CSDN图床
相关推荐
基于博客园搭建Typora免费图床
PicGo搭建免费图床实现Typora图片自动上传
图片防盗链导致图片无法加载解决方法
Typora 图片自动上传至腾讯云Cos 服务