-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathpastebin.py
More file actions
396 lines (290 loc) · 16.3 KB
/
pastebin.py
File metadata and controls
396 lines (290 loc) · 16.3 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""
.. module:: pastebin
:synopsis: This module contains the main class to be instantiated to use pastebin.com functionality
.. moduleauthor:: Ferdinand Silva <ferdinandsilva@ferdinandsilva.com>
"""
import re
import requests
from xml.dom.minidom import parseString
from pastebin_options import OPTION_PASTE, OPTION_LIST, OPTION_TRENDS, OPTION_DELETE, OPTION_USER_DETAILS
from pastebin_constants import PASTEBIN_API_POST_URL, PASTEBIN_API_LOGIN_URL, PASTEBIN_RAW_URL
from pastebin_exceptions import PastebinBadRequestException, PastebinNoPastesException, PastebinFileException, PastebinHTTPErrorException
class PastebinPython(object):
"""This is the main class to be instantiated to use pastebin.com functionality
"""
def __init__(self, **kwargs):
"""You need to put your **API Key** when instantiating this class
:param kwargs: keyword arguments to set settings that can be use to call pastebin.com API function
:type kwargs: dict
:returns: class -- :class:`pastebin_python.pastebin.PastebinPython`
**Example:**::
>>> pasteBin = PastebinPython(api_dev_key='123456789')
>>> print pasteBin.api_dev_key
123456789
"""
self.api_dev_key = kwargs.get('api_dev_key', '')
self.__api_user_key = ""
self.__api_user_paste_list = []
self.api_session = requests.session()
self.web_session = requests.session()
@property
def api_user_key(self):
"""This is where the api_user_key is stored after calling :meth:`pastebin_python.pastebin.PastebinPython.createAPIUserKey`
:returns: str -- the api_user_key
"""
return self.__api_user_key
@property
def api_user_paste_list(self):
"""This where the list of pastes of the current user is stored after calling :meth:`pastebin_python.pastebin.PastebinPython.listUserPastes`
:returns: list -- current user pastes list
"""
return self.__api_user_paste_list
def createPaste(self, api_paste_code, api_paste_name='', api_paste_format='', api_paste_private='', api_paste_expire_date=''):
"""This will create a new paste
:param api_paste_code: this is the text that will be written inside your paste
:type api_paste_code: str
:param api_paste_name: this will be the name / title of your paste
:type api_paste_name: str
:param api_paste_format: this will be the syntax highlighting value, values to be assign can be found at :mod:`pastebin_python.pastebin_formats`
:type api_paste_format: str
:param api_paste_private: this makes a paste public or private, values to be assign can be found at :mod:`pastebin_python.pastebin_constants`
:type api_paste_private: int
:param api_paste_expire_date: this sets the expiration date of your paste, values to be assign can be found at :mod:`pastebin_python.pastebin_constants`
:type api_paste_expire_date: str
:returns: str -- pastebin.com paste URL
:raises: :exc:`pastebin_python.pastebin_exceptions.PastebinBadRequestException`
.. note::
*api_paste_code* is the only required parameter
"""
api_user_key = self.api_user_key if self.api_user_key else ""
api_paste_code = api_paste_code.encode('utf-8') if api_paste_code else ""
postData = {
'api_option': OPTION_PASTE,
'api_dev_key': self.api_dev_key
}
localVar = locals()
for k, v in localVar.items():
if re.search('^api_',k) and v != "":
postData[k] = v
return self.__processRequest('POST', PASTEBIN_API_POST_URL, postData)
def createPasteFromFile(self, filename, api_paste_name='', api_paste_format='', api_paste_private='', api_paste_expire_date=''):
"""Almost the same as :meth:`pastebin_python.pastebin.PastebinPython.createPaste` ,the only difference is that the value of *api_paste_code* came from the file you opened
:param filename: the full path of the file
:type filename: str
:param api_paste_name: this will be the name / title of your paste
:type api_paste_name: str
:param api_paste_format: this will be the syntax highlighting value, values to be assign can be found at :mod:`pastebin_python.pastebin_formats`
:type api_paste_format: str
:param api_paste_private: this makes a paste public or private, values to be assign can be found at :mod:`pastebin_python.pastebin_constants`
:type api_paste_private: int
:param api_paste_expire_date: this sets the expiration date of your paste, values to be assign can be found at :mod:`pastebin_python.pastebin_constants`
:type api_paste_expire_date: str
:returns: str -- pastebin.com paste URL
:raises: :exc:`pastebin_python.pastebin_exceptions.PastebinFileException`
.. note::
*filename* is the only required field
"""
try:
fileToOpen = open(filename, 'r')
fileToPaste = fileToOpen.read()
fileToOpen.close()
return self.createPaste(fileToPaste, api_paste_name, api_paste_format, api_paste_private, api_paste_expire_date)
except IOError as e:
raise PastebinFileException( str(e))
def __processRequest(self, method, url, data):
"""A private function that is responsible of calling/executing the pastebin.com functionality
:param url: the url of the pastebin.com **API**
:type url: str
:param data: the data to be POSTed to the pastebin.com **API**
:type data: dict
:returns: str -- the successfull output of the pastebin.com **API** if no exception raised
:raises: :exc:`pastebin_python.pastebin_exceptions.PastebinBadRequestException`, :exc:`pastebin_python.pastebin_exceptions.PastebinNoPastesException`
"""
if url == PASTEBIN_API_LOGIN_URL:
web_data = {'submit_hidden': 'submit_hidden',
'user_name': data['api_user_name'],
'user_password': data['api_user_password'],
'submit': 'Login', }
self.web_session.get('http://pastebin.com/login')
self.web_session.post('http://pastebin.com/login.php', data=web_data)
if url == PASTEBIN_RAW_URL:
req = self.web_session.get(url % data)
else:
req = self.api_session.request(method, url, data=data)
response = req.content
if re.search('^Bad API request', response):
raise PastebinBadRequestException(response)
elif re.search('^No pastes found', response):
raise PastebinNoPastesException
return response
def createAPIUserKey(self, api_user_name, api_user_password):
"""This is used to request an *api_user_key* which can be used to create a paste as a logged in user
:param api_user_name: this is the pastebin.com username
:type api_user_name: str
:param api_user_password: this is the pastebin.com password
:type api_user_password: str
:returns: str -- unique user session key
:raises: :exc:`pastebin_python.pastebin_exceptions.PastebinBadRequestException`
.. note::
If successfull the unique user session key will be assigned to the private variable *__api_user_key* and can be get with the property *api_user_key*
"""
postData = {
'api_dev_key': self.api_dev_key,
'api_user_name': api_user_name,
'api_user_password': api_user_password
}
self.__api_user_key = self.__processRequest('POST', PASTEBIN_API_LOGIN_URL, postData)
self.__api_user_paste_list = []
return self.__api_user_key
def listUserPastes(self, api_results_limit=50):
"""This will list pastes created by a user
:param api_results_limit: this is not required but the min value should be 1 and the max value should be 1000
:type api_results_limit: int
:returns: list -- the list of of pastes in a dictionary type
:raises: :exc:`pastebin_python.pastebin_exceptions.PastebinBadRequestException`, :exc:`pastebin_python.pastebin_exceptions.PastebinNoPastesException`
.. note::
Need to call the :meth:`pastebin_python.pastebin.PastebinPython.createAPIUserKey` first before calling this function
Pastes list will be stored to the private variable *__api_user_paste_list* and can be retrieve by the property *api_user_key*
"""
postData = {
'api_dev_key': self.api_dev_key,
'api_user_key': self.api_user_key,
'api_results_limit': api_results_limit,
'api_option': OPTION_LIST
}
pastesList = self.__processRequest('POST', PASTEBIN_API_POST_URL, postData)
self.__api_user_paste_list = self.__parseXML(pastesList)
return self.__api_user_paste_list
def listTrendingPastes(self):
"""This will list the 18 currently trending pastes
:returns: list -- the 18 currently trending pastes in a dictionary format
:raises: :exc:`pastebin_python.pastebin_exceptions.PastebinBadRequestException`
"""
postData = {
'api_dev_key': self.api_dev_key,
'api_option': OPTION_TRENDS
}
trendsList = self.__processRequest('POST', PASTEBIN_API_POST_URL, postData)
trendsList = self.__parseXML(trendsList)
return trendsList
def __parseUser(self, xmlString):
"""This will parse the xml string returned by the function :meth:`pastebin_python.pastebin.PastebinPython.getUserInfos`
:param xmlString: this is the returned xml string from :meth:`pastebin_python.pastebin.PastebinPython.getUserInfos`
:type xmlString: str
:returns: list -- user info in a dictionary format
"""
retList = []
userElement = xmlString.getElementsByTagName('user')[0]
retList.append({
'user_name':userElement.getElementsByTagName('user_name')[0].childNodes[0].nodeValue,
'user_avatar_url':userElement.getElementsByTagName('user_avatar_url')[0].childNodes[0].nodeValue,
'user_account_type':userElement.getElementsByTagName('user_account_type')[0].childNodes[0].nodeValue
})
formatElement = userElement.getElementsByTagName('user_format_short')
if formatElement:
retList[0]['user_format_short'] = formatElement[0].childNodes[0].nodeValue
expireElement = userElement.getElementsByTagName('user_expiration')
if expireElement:
retList[0]['user_expiration'] = expireElement[0].childNodes[0].nodeValue
privateElement = userElement.getElementsByTagName('user_private')
if privateElement:
retList[0]['user_private'] = privateElement[0].childNodes[0].nodeValue
websiteElement = userElement.getElementsByTagName('user_website')
if websiteElement:
retList[0]['user_website'] = websiteElement[0].childNodes[0].nodeValue
emailElement = userElement.getElementsByTagName('user_email')
if emailElement:
retList[0]['user_email'] = emailElement[0].childNodes[0].nodeValue
locationElement = userElement.getElementsByTagName('user_location')
if locationElement:
retList[0]['user_location'] = locationElement[0].childNodes[0].nodeValue
return retList
def __parsePaste(self, xmlString):
"""This will parse the xml string returned by the the function :meth:`pastebin_python.pastebin.PastebinPython.listUserPastes` or :meth:`pastebin_python.pastebin.PastebinPython.listTrendingPastes`
:param xmlString: this is the returned xml string from :meth:`pastebin_python.pastebin.PastebinPython.listUserPastes` or :meth:`pastebin_python.pastebin.PastebinPython.listTrendingPastes`
:type xmlString: str
:returns: list -- pastes info in a dictionary format
"""
retList = []
pasteElements = xmlString.getElementsByTagName('paste')
for pasteElement in pasteElements:
try:
paste_title = pasteElement.getElementsByTagName('paste_title')[0].childNodes[0].nodeValue
except IndexError:
paste_title = ""
retList.append({
'paste_title': paste_title,
'paste_key': pasteElement.getElementsByTagName('paste_key')[0].childNodes[0].nodeValue,
'paste_date': pasteElement.getElementsByTagName('paste_date')[0].childNodes[0].nodeValue,
'paste_size': pasteElement.getElementsByTagName('paste_size')[0].childNodes[0].nodeValue,
'paste_expire_date': pasteElement.getElementsByTagName('paste_expire_date')[0].childNodes[0].nodeValue,
'paste_private': pasteElement.getElementsByTagName('paste_private')[0].childNodes[0].nodeValue,
'paste_format_long': pasteElement.getElementsByTagName('paste_format_long')[0].childNodes[0].nodeValue,
'paste_format_short': pasteElement.getElementsByTagName('paste_format_short')[0].childNodes[0].nodeValue,
'paste_url': pasteElement.getElementsByTagName('paste_url')[0].childNodes[0].nodeValue,
'paste_hits': pasteElement.getElementsByTagName('paste_hits')[0].childNodes[0].nodeValue,
})
return retList
def __parseXML(self, xml, isPaste=True):
"""This will handle all of the xml string parsing
:param xml: xml string
:type xml: str
:param isPaste: if True then it will parse the pastes info else it will parse the user info
:type isPaste: bool
:returns: list -- info in a dictionary format
"""
retList = []
xmlString = parseString("<pasteBin>%s</pasteBin>" % xml)
if isPaste:
retList = self.__parsePaste(xmlString)
else:
retList = self.__parseUser(xmlString)
return retList
def deletePaste(self, api_paste_key):
"""This will delete pastes created by certain users
:param api_paste_key: this is the paste key that which you can get in the :meth:`pastebin_python.pastebin.PastebinPython.listUserPastes` function
:type api_paste_key: str
:returns: bool -- True if the deletion is successfull else False
.. note::
Before calling this function, you need to call the :meth:`pastebin_python.pastebin.PastebinPython.createAPIUserKey` first then call the :meth:`pastebin_python.pastebin.PastebinPython.listUserPastes`
"""
postData = {
'api_dev_key': self.api_dev_key,
'api_user_key': self.api_user_key,
'api_paste_key': api_paste_key,
'api_option': OPTION_DELETE
}
try:
retMsg = self.__processRequest('POST', PASTEBIN_API_POST_URL, postData)
except PastebinBadRequestException as e:
retMsg = str(e)
if re.search('^Paste Removed', retMsg):
return True
return False
def getUserInfos(self):
"""You can obtain a users personal info and certain settings by calling this function
:returns: list -- user info in a dictionary format
:raises: :exc:`pastebin_python.pastebin_exceptions.PastebinBadRequestException`
.. note::
You need to call the :meth:`pastebin_python.pastebin.PastebinPython.createAPIUserKey` before calling this function
"""
postData = {
'api_dev_key': self.api_dev_key,
'api_user_key': self.api_user_key,
'api_option': OPTION_USER_DETAILS
}
retData = self.__processRequest('POST', PASTEBIN_API_POST_URL, postData)
retData = self.__parseXML(retData, False)
return retData
def getPasteRawOutput(self, api_paste_key):
"""This will get the raw output of the paste
:param api_paste_key: this is the paste key that which you can get in the :meth:`pastebin_python.pastebin.PastebinPython.listUserPastes` function
:type api_paste_key: str
:returns: str -- raw output of the paste
:raises: :exc:`pastebin_python.pastebin_exceptions.PastebinHTTPErrorException`
"""
try:
retMsg = self.__processRequest('GET', PASTEBIN_RAW_URL, api_paste_key)
except PastebinBadRequestException as e:
retMsg = str(e)
return retMsg.decode('utf-8')