Skip to content

Commit

Permalink
Optimze: bearer token设置在环境变量中
Browse files Browse the repository at this point in the history
Update: 取消图片限制,任意地址图片均可下载
  • Loading branch information
Hongwing committed Dec 22, 2023
1 parent c7f6f5d commit bf5cd62
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
Binary file modified .DS_Store
Binary file not shown.
15 changes: 11 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import subprocess
import json
import time

from flask import Flask, render_template, request
app = Flask(__name__)
Expand All @@ -26,10 +27,10 @@
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


BEARER_TOKEN = ''
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 upload_memos(pic_url):
save_path, image_name = download_pic_request(pic_url)
Expand All @@ -38,6 +39,9 @@ def upload_memos(pic_url):
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)
Expand All @@ -61,8 +65,10 @@ def cwebp_convert_pic(name):
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.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:
Expand All @@ -80,6 +86,7 @@ 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
Expand Down
9 changes: 1 addition & 8 deletions blog_oss.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# 再调用 memo接口 携带资源生成PicGO memo.

from DrissionPage import WebPage
import requests
import os
import subprocess
Expand Down Expand Up @@ -58,14 +59,6 @@ def download_pic(pic_url):
page.download(pic_url, SAVE_PATH)
return SAVE_PATH + '/' + image_name, image_name

def download_pic_request(pic_url):
image_name = pic_url.split('/')[4].split('?')[0]
response = requests.get(pic_url)
if response.status_code == 200:
with open(SAVE_PATH + '/' + image_name, 'wb') as f:
f.write(response.content)


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'))
Expand Down
Binary file removed uploads/1241175-3ffb9015aa085bdd.png.webp
Binary file not shown.

0 comments on commit bf5cd62

Please sign in to comment.