Last active
April 9, 2018 13:08
-
-
Save ficapy/8c57998d14c434ab5df3 to your computer and use it in GitHub Desktop.
当做图床使用,读取粘贴板的图像保存上传,完成后将url地址写入到剪贴板
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python2 | |
# -*- coding: utf-8 -*- | |
# Author: Ficapy | |
# Create: '16/1/1' | |
import datetime | |
import os | |
import sys | |
import atexit | |
import requests | |
import imghdr | |
from cStringIO import StringIO | |
from hashlib import md5 | |
from subprocess import call | |
from base64 import b64encode | |
from os.path import expanduser, exists, basename, getsize | |
NAME = '--------' | |
PWD = '---------' | |
URL = 'http://v0.api.upyun.com/' | |
BUCKET = 'ficapy' | |
DIR = 'blogimg' if sys.argv.__len__() > 1 else 'capture' | |
TINYPNG = 'Basic ' + b64encode('api:------------') | |
s = requests.Session() | |
if sys.argv.__len__() > 2: | |
sys.exit('argv error') | |
def capture(): | |
try: | |
call(['/usr/local/bin/pngpaste', '-v'], stderr=open('/dev/null', 'w')) | |
except OSError: | |
print('please preinstall pngpaste use `brew install pngpaste` before use this script') | |
sys.exit() | |
file_name = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S.png') | |
file_path = os.path.join('/tmp', file_name) | |
atexit.register(lambda x: os.remove(x) if os.path.exists(x) else None, file_path) | |
save = call(['/usr/local/bin/pngpaste', file_path]) | |
if save == 1: | |
sys.exit() | |
return file_path, file_name | |
if sys.argv.__len__() == 1: | |
file_path, file_name = capture() | |
else: | |
# 任意字符代表捕获粘贴板 | |
file_path = expanduser(sys.argv[-1]) | |
if not exists(file_path): | |
file_path, file_name = capture() | |
else: | |
if imghdr.what(file_path) not in ['jpeg', 'jpg', 'png']: | |
sys.exit('file type error') | |
file_name = basename(file_path) | |
# 速度太慢,可以当CDN存在大量文件后使用桌面工具批量处理再上传 | |
# def tinypng(file_path): | |
# # TODO 当超过限制的时候不使用压缩 | |
# if getsize(file_path) > 4.5 * 1024 * 1024: | |
# sys.exit('file too large') | |
# with open(file_path) as f: | |
# r = s.post('https://api.tinify.com/shrink', headers={'Authorization': TINYPNG}, data=f).json() | |
# if 'error' in r: | |
# sys.exit(r['message']) | |
# url = r['output']['url'] | |
# f = StringIO() | |
# for chunk in s.get(url, headers={'Authorization': TINYPNG}, stream=True).iter_content(chunk_size=1024): | |
# f.write(chunk) | |
# return f | |
# f = tinypng(file_path) | |
# f.seek(0, os.SEEK_END) | |
# size = f.tell() | |
# f.seek(0) | |
size = getsize(file_path) | |
date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT') | |
# method,path,date,CONTENT_LENGTH,password | |
params = ['PUT', '/' + BUCKET + '/' + DIR + '/' + file_name, date, str(size), md5(PWD).hexdigest()] | |
sign = md5('&'.join(params)).hexdigest() | |
with open(file_path) as f: | |
url = URL + BUCKET + '/' + DIR + '/' + file_name | |
ret = requests.put(url, headers={'Authorization': 'UpYun ' + NAME + ':' + sign, 'Date': date}, data=f) | |
ret.raise_for_status() | |
ret = 'https://{0}.b0.upaiyun.com/{1}/{2}'.format(BUCKET, DIR, file_name) | |
call('echo {0} | tr -d "\n" | pbcopy'.format(ret), shell=True) | |
print(ret) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ONLY OSX