Skip to content

Commit ce2ac84

Browse files
author
tiann
committed
添加图床设置功能;
1 parent 561c91c commit ce2ac84

File tree

3 files changed

+67
-14
lines changed

3 files changed

+67
-14
lines changed

info.plist

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,24 @@
6767
<key>escaping</key>
6868
<integer>68</integer>
6969
<key>script</key>
70-
<string>query = "{query}"
70+
<string># coding: utf-8
7171
from clipboard import get_paste_img_file
7272
from upload import upload_qiniu
73+
import util
7374
import os
7475
import subprocess
76+
import sys
7577
76-
def noti(msg, title="notice"):
77-
os.system('osascript -e \'display notification "%s" with title "%s"\'' % (msg, title))
78+
if not os.path.exists(util.CONFIG_FILE):
79+
util.generate_config_file()
7880
79-
url = "http://7sbqce.com1.z0.glb.clouddn.com/markdown"
81+
config = util.read_config()
82+
if not config:
83+
util.notice('请先设置你的七牛图床信息')
84+
util.open_with_editor(util.CONFIG_FILE)
85+
sys.exit(0)
86+
87+
url = '%s/%s' % (config['url'], config['prefix'])
8088
8189
img_file = get_paste_img_file()
8290
if img_file:
@@ -89,9 +97,9 @@ if img_file:
8997
os.system("echo '%s' | pbcopy" % markdown_url)
9098
os.system('osascript -e \'tell application "System Events" to keystroke "v" using command down\'')
9199
ret = upload_qiniu(img_file)
92-
if not ret: noti("upload image to qiniu failed")
100+
if not ret: util.notice("上传图片到图床失败,请检查网络后重试")
93101
else:
94-
noti("there are no image file in clipboard")
102+
util.notice("剪切版里没有图片!")
95103
</string>
96104
<key>type</key>
97105
<integer>3</integer>

upload.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22

33
import os
44
from qiniu import Auth, put_file
5+
from util import read_config
56

6-
access_key = '' # AK
7-
secret_key = '' # SK
8-
9-
bucket_name = 'booluimg' # 七牛空间名
10-
11-
q = Auth(access_key, secret_key)
7+
config = read_config()
128

139
def upload_qiniu(path):
1410
''' upload file to qiniu'''
11+
q = Auth(config['ak'], config['sk'])
1512
dirname, filename = os.path.split(path)
16-
key = 'markdown/%s' % filename # upload to qiniu's markdown dir
13+
key = '%s/%s' % (config['prefix'], filename) # upload to qiniu's markdown dir
1714

18-
token = q.upload_token(bucket_name, key)
15+
token = q.upload_token(config['bucket'], key)
1916
ret, info = put_file(token, key, path, check_crc=True)
2017
return ret != None and ret['key'] == key

util.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8
2+
import os, re
3+
import ConfigParser
4+
5+
CONFIG_FILE = 'config.ini'
6+
7+
def notice(msg, title="notice"):
8+
''' notoce message in notification center'''
9+
os.system('osascript -e \'display notification "%s" with title "%s"\'' % (msg, title))
10+
11+
def read_config():
12+
''' read congig from config.ini, return a five tuple'''
13+
if not os.path.exists(CONFIG_FILE):
14+
return
15+
cf = ConfigParser.ConfigParser()
16+
cf.read(CONFIG_FILE)
17+
18+
qiniu_section = 'qiniu'
19+
try:
20+
ak = cf.get(qiniu_section, 'ak')
21+
sk = cf.get(qiniu_section, 'sk')
22+
url = cf.get(qiniu_section, 'url')
23+
bucket = cf.get(qiniu_section, 'bucket')
24+
prefix = cf.get(qiniu_section, 'prefix')
25+
except ConfigParser.NoOptionError:
26+
return
27+
28+
keys = ('ak', 'sk', 'url', 'bucket', 'prefix')
29+
res = (ak, sk, url, bucket, prefix)
30+
if not all(map(lambda x: re.match(r'\w+', x), res)):
31+
return
32+
return dict(zip(keys, res))
33+
34+
def open_with_editor(filepath):
35+
''' open file with apple's text editor'''
36+
os.system('open -b "com.apple.TextEdit" "./%s"' % CONFIG_FILE)
37+
38+
def generate_config_file():
39+
import textwrap
40+
config_file_init_content = '''\
41+
[qiniu]
42+
ak=七牛图床的Access Key
43+
sl=七牛图床的Secret Key
44+
url=七牛图床地址
45+
bucket=七牛图床空间名
46+
prefix=七牛图床资源前缀名'''
47+
with open(CONFIG_FILE, 'w') as fp:
48+
fp.write(textwrap.dedent(config_file_init_content))

0 commit comments

Comments
 (0)