引子
Linux下不支持QQ等功能豐富的IM,雖然可以通過wine運行QQ2012,但是還是喜歡在gtalk群中聊天,gtalk群不支持圖片方式,這就要靠我們大家自己來解決了,eleven開放了一個Image上傳和顯示接口,提供了使用curl來解決,但是我們公司的網絡使用squid禁止了curl的訪問,所以整天看他們這么爽的分享圖片我也不甘心阿,所以就使用Python寫了一個分享圖片的腳本
實現
使用scrot截圖,然后使用urllib2庫上傳圖片,如果存在PyQt4庫則會將結果放到剪貼板上,如果不存在則輸出,自行復制
代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # # Author : cold # E-mail : [email protected] # 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庫則會將結果放到剪貼板,如沒有則輸出結果.如果腳本給了圖片路徑參數則上傳給定路徑的圖片