使用python编写一个在linux下实现截图分享的脚本的教程

引子

linux下不支持qq等功能丰富的im,虽然可以通过wine运行qq2012,但是还是喜欢在gtalk群中聊天,gtalk群不支持图片方式,这就要靠我们大家自己来解决了,eleven开放了一个image上传和显示接口,提供了使用curl来解决,但是我们公司的网络使用squid禁止了curl的访问,所以整天看他们这么爽的分享图片我也不甘心阿,所以就使用python写了一个分享图片的脚本
实现

使用scrot截图,然后使用urllib2库上传图片,如果存在pyqt4库则会将结果放到剪贴板上,如果不存在则输出,自行复制
代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# author : cold
# e-mail : wh_linux@126.com
# date : 13/01/21 09:54:39
# desc : 贴代码和图片
#
import urllib2, json
import mimetools
import mimetypes
import itertools
__host__ = “http://eleveni386.7axu.com”
class form(object):
def __init__(self):
self.form_fields = []
self.files = []
self.boundary = mimetools.choose_boundary()
self.content_type = “application/x-www-form-urlencoded”
return
def get_content_type(self):
return self.content_type
def add_field(self, name, value):
self.form_fields.append((name, value))
return
def add_file(self, fieldname, filename, filehandle, mimetype=none):
body = filehandle.read()
if mimetype is none:
mimetype = ( mimetypes.guess_type(filename)[0]
or
‘applicatioin/octet-stream’)
self.files.append((fieldname, filename, mimetype, body))
self.content_type = ‘multipart/form-data; boundary=%s’ % self.boundary
return
def __str__(self):
parts = []
part_boundary = ‘–‘ + self.boundary
parts.extend(
[ part_boundary,
‘content-disposition: form-data; name=”%s”‘ % name,
”,
value,
]
for name, value in self.form_fields)
if self.files:
parts.extend([
part_boundary,
‘content-disposition: form-data; name=”%s”; filename=”%s”‘ %\
(field_name, filename),
‘content-type: %s’ % content_type,
”,
body,
] for field_name, filename, content_type, body in self.files)
flattened = list(itertools.chain(*parts))
flattened.append(‘–‘ + self.boundary + ‘–‘)
flattened.append(”)
return ‘\r\n’.join(flattened)
class httphelper(object):
def __init__(self, url = none, form = none, method = ‘get’):
self._url = url
self._form = form
self._body = str(form)
self._method = method
self._dst_url = none
if url:
self.make_request()
def make_request(self):
url = self._url
if not self._url.startswith(‘http://’):
url = ‘http://’ + self._url
self.request = urllib2.request(url)
if self._form:
self.add_header(“content-type”, self._form.get_content_type())
self.add_header(“content-length”, len(self._body))
self.request.add_data(self._body)
def add_header(self, key, val):
self.request.add_header(key, val)
def change(self, url, params = {}, method = ‘get’):
self._url = url
self._params = params
self._method = method
self.make_request()
def open(self):
response = urllib2.urlopen(self.request)
content = response.read()
self._dst_url = response.geturl()
try:
return json.loads(content)
except:
return content
if __name__ == “__main__”:
import argparse
import os
parser = argparse.argumentparser()
parser.add_argument(dest=”path”, nargs=”?”)
args = parser.parse_args()
if args.path:
path = args.path
else:
path = r”/tmp/tmpscrot.png”
os.system(“scrot -s {0}”.format(path))
form = form()
filename = os.path.split(path)[-1]
form.add_file(fieldname=’mypic’, filename=filename,
filehandle=open(path))
http = httphelper( __host__ + ‘/image/’, form)
url = http.open()
try:
from pyqt4.qtgui import qapplication
app = qapplication([])
cb = qapplication.clipboard()
cb.settext(url)
except:
print url

安装

将上面代码保存一个文件,放在path路径里,赋予执行权限即可
使用

默认的不跟图片地址则会截图,截图完毕后自动分享,如安装了pyqt4库则会将结果放到剪贴板,如没有则输出结果.如果脚本给了图片路径参数则上传给定路径的图片

Posted in 未分类