前言 软件下载地址:https://wshuo.lanzoui.com/i7yWVw2w7jc typora 是一款好用的 markdown 文档编辑器,而且开源。 我之前研究过CSDN上传图片的接口,前几天发现了Typora 这个好用的软件,可以自定义图床,因为我很多文章图片资源用的都是CSDN,那么我就可以采用CSDN作为图床,直接就实现拖拽图片上传的CSDN。 需要的环境python3 requests requests-toolbelt 代码- import os
- import sys
- os.chdir(os.path.dirname(os.path.realpath(sys.executable))) #这是打包版本的代码
- import requests
- import re
- from threading import Thread
- import time
- import requests
- import http.cookiejar as cookielib
- import psutil
- import os
- from requests_toolbelt import MultipartEncoder
- requests.packages.urllib3.disable_warnings()
- def is_login():
- session = requests.session()
- try:
- session.cookies = cookielib.LWPCookieJar(filename='.cookie/csdn.txt')
- session.cookies.load()
- url = 'https://me.csdn.net/api/user/show'
- response = session.post(url)
- if response.json()['message'] == "成功":
- return True
- else:
- return False
- except Exception as e:
- return False
- def login():
- session = requests.session()
- session.cookies = cookielib.LWPCookieJar(filename='.cookie/csdn.txt')
- 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
- imgData = session.get(img_url).content
- with open("qrcode.jpg","wb") as f:
- f.write(imgData)
- os.popen("qrcode.jpg")
- uuid = uuid.split('/')[-1]
- url = 'https://long.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(): # 遍历当前process
- try:
- if proc.name() == "dllhost.exe":
- proc.kill() # 关闭该process
- except Exception as e:
- pass
- break
- time.sleep(1)
- url = 'https://passport.csdn.net/v1/register/pcAuthCallBack?pcAuthType=weixin&code=%s&state=csdn' % code[0]
- session.get(url)
- session.cookies.save()
- def updatePicture(picList):
- for pic in picList:
- session = requests.session()
- session.cookies = cookielib.LWPCookieJar(filename='.cookie/csdn.txt')
- session.cookies.load()
- fields = {
- 'file':(os.path.basename(pic),open(pic,'rb'),"image/jpeg"),
- }
- m = MultipartEncoder(fields, boundary='------WebKitFormBoundarynTBa3OWoSMrcVf0F')
- headers = {
- 'content-Type': m.content_type,
- }
- url = 'https://blog-console-api.csdn.net/v1/upload/img?shuiyin=2'
- res = session.post(url, headers=headers, data=m,verify=False)
- print(res.json()["data"]["url"])
- if __name__ == '__main__':
- if not os.path.exists(".cookie"):
- os.mkdir(".cookie")
- if not os.path.exists(os.path.join(".cookie","csdn.txt")):
- with open(os.path.join(".cookie","csdn.txt"),"w") as f:
- f.write("")
- if not is_login():
- login()
- updatePicture(sys.argv[1:])
复制代码 说明
- os.chdir(os.path.dirname(os.path.realpath(sys.executable)))
复制代码 这句是为了获取打包后的真实exe所在路径(因为否则会获取到临时路径,而cookie也会存到临时路径里导致每次都要登录),如果是单独脚本运行可以将其改为:
- os.chdir(os.path.dirname(os.path.abspath(__file__)))
复制代码 脚本运行配置
D:\Python36\python.exe D:\software\Typora\upload.py exe运行配置
D:\software\Typora\upload.exe 其它正常来说第一次登录是需要微信扫码登录的,登录成功会自动保存cookie, 并且关闭二维码显示,如果没有关闭说明你显示图片的进程不是"dllhost.exe",可以自己修改一下,我是win7的系统,命令行调用图片就是这个进程。 另外我发现一个typora 的问题,不知道是不是bug,每次会将启动位置作为当前环境变量,这也是我为什么写成绝对路径的原因,因为你不能保证markdown文件在哪个路径,解决办法就是写成绝对路径。
|