Skip to content

Commit

Permalink
添加图床设置功能;
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Nov 11, 2015
1 parent 561c91c commit ce2ac84
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 14 deletions.
20 changes: 14 additions & 6 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,24 @@
<key>escaping</key>
<integer>68</integer>
<key>script</key>
<string>query = "{query}"
<string># coding: utf-8
from clipboard import get_paste_img_file
from upload import upload_qiniu
import util
import os
import subprocess
import sys
def noti(msg, title="notice"):
os.system('osascript -e \'display notification "%s" with title "%s"\'' % (msg, title))
if not os.path.exists(util.CONFIG_FILE):
util.generate_config_file()
url = "http://7sbqce.com1.z0.glb.clouddn.com/markdown"
config = util.read_config()
if not config:
util.notice('请先设置你的七牛图床信息')
util.open_with_editor(util.CONFIG_FILE)
sys.exit(0)
url = '%s/%s' % (config['url'], config['prefix'])
img_file = get_paste_img_file()
if img_file:
Expand All @@ -89,9 +97,9 @@ if img_file:
os.system("echo '%s' | pbcopy" % markdown_url)
os.system('osascript -e \'tell application "System Events" to keystroke "v" using command down\'')
ret = upload_qiniu(img_file)
if not ret: noti("upload image to qiniu failed")
if not ret: util.notice("上传图片到图床失败,请检查网络后重试")
else:
noti("there are no image file in clipboard")
util.notice("剪切版里没有图片!")
</string>
<key>type</key>
<integer>3</integer>
Expand Down
13 changes: 5 additions & 8 deletions upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

import os
from qiniu import Auth, put_file
from util import read_config

access_key = '' # AK
secret_key = '' # SK

bucket_name = 'booluimg' # 七牛空间名

q = Auth(access_key, secret_key)
config = read_config()

def upload_qiniu(path):
''' upload file to qiniu'''
q = Auth(config['ak'], config['sk'])
dirname, filename = os.path.split(path)
key = 'markdown/%s' % filename # upload to qiniu's markdown dir
key = '%s/%s' % (config['prefix'], filename) # upload to qiniu's markdown dir

token = q.upload_token(bucket_name, key)
token = q.upload_token(config['bucket'], key)
ret, info = put_file(token, key, path, check_crc=True)
return ret != None and ret['key'] == key
48 changes: 48 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8
import os, re
import ConfigParser

CONFIG_FILE = 'config.ini'

def notice(msg, title="notice"):
''' notoce message in notification center'''
os.system('osascript -e \'display notification "%s" with title "%s"\'' % (msg, title))

def read_config():
''' read congig from config.ini, return a five tuple'''
if not os.path.exists(CONFIG_FILE):
return
cf = ConfigParser.ConfigParser()
cf.read(CONFIG_FILE)

qiniu_section = 'qiniu'
try:
ak = cf.get(qiniu_section, 'ak')
sk = cf.get(qiniu_section, 'sk')
url = cf.get(qiniu_section, 'url')
bucket = cf.get(qiniu_section, 'bucket')
prefix = cf.get(qiniu_section, 'prefix')
except ConfigParser.NoOptionError:
return

keys = ('ak', 'sk', 'url', 'bucket', 'prefix')
res = (ak, sk, url, bucket, prefix)
if not all(map(lambda x: re.match(r'\w+', x), res)):
return
return dict(zip(keys, res))

def open_with_editor(filepath):
''' open file with apple's text editor'''
os.system('open -b "com.apple.TextEdit" "./%s"' % CONFIG_FILE)

def generate_config_file():
import textwrap
config_file_init_content = '''\
[qiniu]
ak=七牛图床的Access Key
sl=七牛图床的Secret Key
url=七牛图床地址
bucket=七牛图床空间名
prefix=七牛图床资源前缀名'''
with open(CONFIG_FILE, 'w') as fp:
fp.write(textwrap.dedent(config_file_init_content))

0 comments on commit ce2ac84

Please sign in to comment.