https://gist.github.com/jiaxianhua/90d96f10074034c10145b3ca37dbfef0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json
import os
from pprint import pprint
upload_url = 'https://upload.qiniup.com/'
# put cookie to cookie file
def getCookie():
try:
with open('cookie', 'r') as f:
return f.readline().strip()
except Exception as error:
print(error)
def uploadImage(cookie, filepath):
filename = os.path.basename(filepath)
token_url = 'https://www.jianshu.com/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': cookie,
}
response = requests.get(token_url, headers=headers)
response.encoding = response.apparent_encoding
pprint(vars(response))
token_key = json.loads(response.text)
pprint("token and key: {}".format(token_key))
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=filename, img_url=img_url)
return img_md
if __name__ == '__main__':
cookie = getCookie()
img_md = uploadImage(cookie, 'map.png')
print(img_md)
网友评论