Skip to content

Instantly share code, notes, and snippets.

@jiaxianhua
Created April 16, 2019 08:46
Show Gist options
  • Save jiaxianhua/90d96f10074034c10145b3ca37dbfef0 to your computer and use it in GitHub Desktop.
Save jiaxianhua/90d96f10074034c10145b3ca37dbfef0 to your computer and use it in GitHub Desktop.
#!/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 = '![{text}]({img_url})'.format(text=filename, img_url=img_url)
return img_md
if __name__ == '__main__':
cookie = getCookie()
img_md = uploadImage(cookie, 'map.png')
print(img_md)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment