-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
128 lines (109 loc) · 4.21 KB
/
app.py
File metadata and controls
128 lines (109 loc) · 4.21 KB
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
####
# 将简书中的图片 上传至memos.
####
# 1. 上传图片后获得一个返回地址
# 2. 下载该图片
# 3. cwebp 转换该图片为一个webp
# 4. 上传该webp至memos
# 简化版本
# 1. 给一个上传后的地址, 返回一个memos地址 https://memos.henryhe.cn/o/r/36
# 先调用 blob接口 上传资源
# 再调用 memo接口 携带资源生成PicGO memo.
import requests
import os
import subprocess
import json
import time
from flask import Flask, render_template, request
app = Flask(__name__)
# 设置上传文件的保存目录
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
BEARER_TOKEN = os.environ.get('MEMOS_BEARER_TOKEN')
HEADERS = {'Authorization': 'Bearer ' + BEARER_TOKEN}
SAVE_PATH = os.path.join(app.config['UPLOAD_FOLDER'], '')
IMAGE_COUTER = 'download.jpg'
def refresh_token_by_localstorage():
'''
启动时从环境变量加载Token,运行时可通过页面变更token后从LocalStorage加载
'''
def upload_memos(pic_url):
save_path, image_name = download_pic_request(pic_url)
print(save_path, image_name)
save_path = cwebp_convert_pic(image_name)
r_list = get_memos_with_id(31)
print(r_list)
pic_id = post_memo_blob(save_path)
if not pic_id:
print('upload to memos failed.')
return None
memo_pic_url = 'https://memos.henryhe.cn/o/r/'
r_list.append(pic_id)
print(r_list)
result = patch_memos_with_resourcelist(31, r_list)
print(result)
print('\n==========================\nyour pic: \n' + memo_pic_url + str(pic_id))
return memo_pic_url + str(pic_id)
def cwebp_convert_pic(name):
origin_path = SAVE_PATH + '/' + name
result_path = SAVE_PATH + '/' + name +'.webp'
command = r'cwebp ' + origin_path + ' -o ' + result_path
r_code =subprocess.Popen(command, shell=True)
r_code.wait()
if 'returncode: 0' in str(r_code):
print('convert to cwebp successfully: ' + result_path)
else:
print('convert failed.')
# 清楚原文件
os.remove(origin_path)
return result_path
def download_pic_request(pic_url):
# url = pic_url.split('?')[0]
# image_name = pic_url.split('/')[4].split('?')[0]
url = pic_url
image_name = str((int)(time.time())) + '_' + IMAGE_COUTER
response = requests.get(url)
if response.status_code == 200:
with open(SAVE_PATH + '/' + image_name, 'wb') as f:
f.write(response.content)
return SAVE_PATH + '/' + image_name, image_name
def get_memos_with_id(id):
response = requests.get('https://memos.henryhe.cn/api/v1/memo/' + str(id), headers=HEADERS)
# print(response.json().get('resourceList'))
dic = response.json().get('resourceList')
# 过滤当前id的memo的所有resourceList ids
return [obj['id'] for obj in dic]
def post_memo_blob(filepath):
# filepath: '/Users/henryhe/t.webp'
files = {'file': open(filepath, 'rb')}
final_resp = requests.post('https://memos.henryhe.cn/api/v1/resource/blob', files=files, headers=HEADERS)
print(final_resp.json())
# 获取id
pic_id = final_resp.json().get('id')
return pic_id
def patch_memos_with_resourcelist(memo_id, resource_list):
payload = {
"id": memo_id,
"content": "#PicGo ",
"visibility": "PUBLIC",
"resourceIdList": resource_list,
"relationList": []
}
response = requests.patch('https://memos.henryhe.cn/api/v1/memo/' + str(memo_id), headers=HEADERS, data=json.dumps(payload))
# print(response.json().get('resourceList'))
dic = response.json().get('resourceList')
return [obj['id'] for obj in dic]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/process_input', methods=['POST'])
def process_input():
user_input = request.form['user_input']
saved_token = request.form['savedPassword']
print('pic_url is: ' + user_input + '\n BearToken is: ' + saved_token)
result_url = upload_memos(user_input)
return render_template('index.html', user_input=result_url)
if __name__ == '__main__':
app.run(debug=True)
### ?imageMogr2/auto-orient/strip|imageView2/2/w/400/format/webp
# upload_memos('https://upload-images.jianshu.io/upload_images/1241175-5578d60f55fac4fd.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240')