こんにちは!!
最近,SQL ばかり書いているヨシオリです!!
ちょっと気晴らしにコードを書きたかったのですが,
丁度,id:kusigahama が「社内 Gyazo が欲しいよぉ」
って言ってたのを思い出した気がしてのでサクっと作ってみました.
超シンプル!!
サーバ側は web.py で動いています.
#!/usr/bin/env python # -*- coding: utf-8 -*- import web,dbm,hashlib urls = ( '/(.*)','Gyazo' ) app = web.application(urls, globals()) db = dbm.open('gyazo','c') class Gyazo: def GET(self,param): param = param.split('.')[0] image = db.get(param) if not image: return 'error' else: return image def POST(self,param): i = web.input() image = i.get('image') if not image: return 'error' else: key = hashlib.md5(image).hexdigest() db[key] = image return key if __name__ == '__main__': app.run()
クライアント側は Linux か Mac でしか動きません.
っていうか,urllib2 が multipart なデータを POST 出来ないおかげで,
無駄に長くなっちゃいました><
#!/usr/bin/env python # -*- coding: utf-8 -*- import os, tempfile, commands, httplib, platform GYAZO_HOST = 'localhost:8080' OPEN_URL = 'http://%s/' % GYAZO_HOST _system = platform.system() command_cap = 'import' command_open = 'firefox' if _system == 'Darwin': command_cap = 'screencapture -i ' command_open = 'open' def get_screenshot(): img_fd, img_path = tempfile.mkstemp('.png') commands.getoutput('%s %s' % (command_cap, img_path)) t = open(img_path) result = t.read() t.close() os.remove(img_path) return result def post_img(url,img): BOUNDARY = '----bOUndArybOUndAry----' data =[] data.append('--' + BOUNDARY) data.append('Content-Disposition: form-data; name="image"; filename="hoge.png"') data.append('Content-Type: image/png' ) data.append('') data.append(img) data.append('--' + BOUNDARY + '--') data.append('') body = '\r\n'.join(data) h = httplib.HTTP(url) h.putrequest('POST', '/') h.putheader('Content-Type', 'multipart/form-data; boundary=%s' % BOUNDARY) h.putheader('Content-Length', str(len(body))) h.endheaders() h.send(body) errcode, errmsg, headers = h.getreply() return h.file.read() if __name__ == '__main__': img = get_screenshot() r = post_img(GYAZO_HOST,img) url = OPEN_URL + r + '.png' commands.getoutput('%s %s' % (command_open,url))
さぁ,みんなも社外秘なアレやコレやを Gyazo りましょう!!!