forked from tupton/pastebin-cl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pastebin.py
executable file
·336 lines (246 loc) · 10.8 KB
/
pastebin.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#! /usr/bin/env python
import sys
import os
import urllib
import urllib2
import optparse
import time
from subprocess import Popen, PIPE
VERSION = """%prog 0.6 by Thomas Upton
Use `%prog -h` for help and a list of options.
"""
class Pastebin(object):
"""A wrapper for the pastebin API"""
PASTEBIN_EXPIRE_NEVER = "N"
PASTEBIN_EXPIRE_TEN_MINUTES = "10M"
PASTEBIN_EXPIRE_ONE_HOUR = "1H"
PASTEBIN_EXPIRE_ONE_DAY = "1D"
PASTEBIN_EXPIRE_ONE_MONTH = "1M"
PASTEBIN_EXPIRE = [PASTEBIN_EXPIRE_NEVER, PASTEBIN_EXPIRE_TEN_MINUTES, PASTEBIN_EXPIRE_ONE_HOUR,
PASTEBIN_EXPIRE_ONE_DAY, PASTEBIN_EXPIRE_ONE_MONTH]
PASTEBIN_USER_KEY_CACHE = '.pastebin_user_key_cache'
pastebin_api = "http://pastebin.com/api/api_post.php"
pastebin_user_api = "http://pastebin.com/api/api_login.php"
pastebin_dev_key = "564b1c623712f731a96c7820dff4ab9f"
def __init__(self, paste_code, paste_name=None, paste_private=False,
paste_expire_date=PASTEBIN_EXPIRE_NEVER, paste_format=None, username=None,
password=None):
self.set_paste_code(paste_code)
self.set_paste_name(paste_name)
self.set_paste_private(paste_private)
self.set_paste_expire_date(paste_expire_date)
self.set_paste_format(paste_format)
self.set_username(username)
self.set_password(password)
def paste(self):
"""Submit the paste request to pastebin"""
request = self._build_request()
response = urllib2.urlopen(request)
response = response.read()
return response
def set_paste_code(self, paste_code):
self._paste_code = paste_code
def set_paste_name(self, paste_name):
self._paste_name = paste_name
def set_paste_private(self, paste_private):
paste_private = paste_private == True
self._paste_private = paste_private
def set_paste_expire_date(self, paste_expire_date):
if not paste_expire_date in self.PASTEBIN_EXPIRE:
paste_expire_date = self.PASTEBIN_EXPIRE_NEVER
self._paste_expire_date = paste_expire_date
def set_paste_format(self, paste_format):
self._paste_format = paste_format
def set_username(self, username):
self._username = username
def set_password(self, password):
self._password = password
def get_paste_code(self):
return self._paste_code
def get_paste_name(self):
return self._paste_name
def get_paste_private(self):
paste_private = "0"
if self._paste_private is True:
paste_private = "1"
return paste_private
def get_paste_expire_date(self):
return self._paste_expire_date
def get_paste_format(self):
return self._paste_format
def get_username(self):
return self._username
def get_password(self):
return self._password
def _build_request(self):
request_url = self.pastebin_api
user_key = self._get_user_key()
request_data = self._build_param_string(user_key)
return urllib2.Request(url=request_url, data=request_data)
def _build_param_string(self, user_key):
params = dict()
if not self.get_paste_code():
raise Exception, "No paste_code was given"
params['api_option'] = 'paste'
params['api_dev_key'] = self.pastebin_dev_key
params['api_paste_code'] = self.get_paste_code()
if self.get_paste_name() is not None:
params['api_paste_name'] = self.get_paste_name()
if self.get_paste_private() != "0":
params['api_paste_private'] = self.get_paste_private()
params['api_paste_expire_date'] = self.get_paste_expire_date()
if self.get_paste_format() is not None:
params['api_paste_format'] = self.get_paste_format()
if user_key is not None:
params['api_user_key'] = user_key
return self._encode_params(params)
def _build_user_param_string(self):
params = dict()
params['api_user_name'] = self.get_username()
params['api_user_password'] = self.get_password()
params['api_dev_key'] = self.pastebin_dev_key
return self._encode_params(params)
def _encode_params(self, params):
encoded = urllib.urlencode([(k, v) for (k, v) in params.iteritems()])
return encoded
def _get_user_key(self):
username = self.get_username()
password = self.get_password()
user_key = None
if username is not None and password is not None:
user_key = self._get_user_key_from_cache(username)
if user_key is None:
user_request_url = self.pastebin_user_api
user_request_data = self._build_user_param_string()
user_request = urllib2.Request(url=user_request_url, data=user_request_data)
user_response = urllib2.urlopen(user_request)
user_key = user_response.read()
self._put_user_key_in_cache(username, user_key)
return user_key
def _get_user_key_from_cache(self, username):
user_key = None
try:
cache = open(self.PASTEBIN_USER_KEY_CACHE, 'r')
except IOError:
cache = open(self.PASTEBIN_USER_KEY_CACHE, 'w')
cache.close()
cache = open(self.PASTEBIN_USER_KEY_CACHE, 'r')
for line in cache:
parts = line.split()
if len(parts) == 2 and parts[0] == username:
user_key = parts[1]
cache.close()
return user_key
def _put_user_key_in_cache(self, username, user_key):
cache = open(self.PASTEBIN_USER_KEY_CACHE, 'a')
cache.write(' '.join([username, user_key]))
cache.write('\n')
cache.close()
def copy_text(text):
"""
Copy text to the system clipboard
"""
cb_name = get_clipboard_name()
if cb_name is not None:
clipboard = Popen(cb_name, shell=True, stdin=PIPE).stdin
clipboard.write(text)
clipboard.close()
def get_clipboard_name():
"""Get the name of the system clipboard"""
cb_list = ['pbcopy', 'xclip', 'putclip']
cb_name = None
for cb in cb_list:
if cli_exists(cb):
cb_name = cb
break
return cb_name
def cli_exists(command):
"""Determine whether or not a command line command exists"""
exists = False
test = 'type %s >> /dev/null 2>&1' % command
process = Popen(test, shell=True, stdout=PIPE)
process.stdout.close()
if not process.wait():
exists = True
return exists
def create_opt_parser():
"""
Creates an option parser using optparse
"""
parser = optparse.OptionParser(usage="""usage: pastebin.py [-h] [-v] [-cp] [-f FILE] [--paste-name PASTE_NAME]
[--paste-expire-date PASTE_EXPIRE_DATE]
[--paste-format PASTE_FORMAT]
[--username USERNAME]
[--password PASSWORD]""", epilog="Paste text to pastebin.com.",
version=VERSION)
parser.add_option('-c', '--copy-text', default=False, action='store_true',
help="copy the text that is posted to pastebin.com before copying the pastebin.com URL")
parser.add_option('-f', '--file', default=False, action='store',
help="read from FILE instead of stdin")
parser.add_option('-p', '--print-response', default=False, action='store_true',
help="print the response from the pastebin API instead of copying it to the clipboard")
# pastebin API options
pastebin_api_group = optparse.OptionGroup(parser, "Pastebin API Options",
"These options are passed to the pastebin API request.")
pastebin_api_group.add_option('--paste-name', default=None, action='store',
help="the name to give to the pasted text")
pastebin_api_group.add_option('--paste-private', default=False, action='store_true',
help="whether to make the paste private")
pastebin_api_group.add_option('--paste-expire-date', default=None, action='store',
help="when to expire the paste; valid values are N, 10M, 1H, 1D, and 1M")
pastebin_api_group.add_option('--paste-format', default=None, action='store',
help="the format used for syntax highlighting; see http://pastebin.com/api.php for valid values")
pastebin_api_group.add_option('--username', default=None, action='store',
help="your pastebin.com username")
pastebin_api_group.add_option('--password', default=None, action='store',
help="your pastebin.com password")
parser.add_option_group(pastebin_api_group)
return parser
def growl_notify(pastebin_response):
"""Use Growl via Applescript to create a notification with pastebin's response."""
if not cli_exists('osascript'):
return
growl_app = 'Growl'
growl_helper_applescript = 'tell application "System Events" to (name of processes) contains "GrowlHelperApp"'
osascript = Popen("osascript -e '" + growl_helper_applescript + "'", shell=True, stdout=PIPE)
stdout,stderr = osascript.communicate()
if stdout.strip() == "true":
growl_app = 'GrowlHelperApp'
applescript = """ tell application "%(growl_app)s"
set allNotificationsList to {"Pastebin Notification"}
set enabledNotificationsList to {"Pastebin Notification"}
register as application "Pastebin Commandline" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Script Editor"
notify with name "Pastebin Notification" title "Pastebin" description "%(pastebin_response)s" application name "Pastebin Commandline"
end tell """ % {'growl_app': growl_app, 'pastebin_response': pastebin_response}
osascript = Popen("osascript -e '%s'" % applescript, shell=True)
osascript.communicate()
def paste_to_pastebin(lines, opts):
"""
Post the given text to pastebin and return the response from the api
"""
pastebin = Pastebin(paste_code=lines, paste_name=opts.paste_name,
paste_private=opts.paste_private, paste_expire_date=opts.paste_expire_date,
paste_format=opts.paste_format, username=opts.username, password=opts.password)
response = pastebin.paste()
return response
def main(argv):
"""
Parse the command lines options and paste the given text to pastebin.com
"""
parser = create_opt_parser()
opts, args = parser.parse_args(argv)
if opts.file != False:
file = open(opts.file, 'r')
else:
file = sys.stdin
lines = "".join(file.readlines())
if opts.copy_text:
copy_text(lines)
pastebin_response = paste_to_pastebin(lines, opts)
growl_notify(pastebin_response)
if opts.print_response:
print pastebin_response
else:
copy_text(pastebin_response)
if __name__ == '__main__':
main(sys.argv[1:])