1
+ import re , urllib2 , urllib
2
+ from xml .dom .minidom import parseString
3
+
4
+ from pastebin_options import OPTION_PASTE , OPTION_LIST , \
5
+ OPTION_TRENDS , OPTION_DELETE , \
6
+ OPTION_USER_DETAILS
7
+
8
+ from pastebin_constants import PASTEBIN_API_POST_URL , PASTEBIN_API_LOGIN_URL
9
+ from pastebin_exceptions import PastebinBadRequestException , PastebinNoPastesException , \
10
+ PastebinFileException
11
+
12
+ class PastebinPython (object ):
13
+
14
+ def __init__ (self , ** kwargs ):
15
+
16
+ self .api_dev_key = kwargs .get ('api_dev_key' ,'' )
17
+ self .__api_user_key = ""
18
+ self .__api_user_paste_list = []
19
+
20
+ @property
21
+ def api_user_key (self ):
22
+ return self .__api_user_key
23
+
24
+ @property
25
+ def api_user_paste_list (self ):
26
+ return self .__api_user_paste_list
27
+
28
+ def createPaste (self , api_paste_code , api_paste_name = '' , api_paste_format = '' , api_paste_private = '' , api_paste_expire_date = '' ):
29
+
30
+ api_user_key = self .api_user_key if self .api_user_key else ""
31
+
32
+ postData = {
33
+ 'api_option' :OPTION_PASTE ,
34
+ 'api_dev_key' :self .api_dev_key
35
+ }
36
+
37
+ localVar = locals ()
38
+
39
+ for k ,v in localVar .items ():
40
+ if re .search ('^api_' , k ) and v != "" :
41
+ postData [k ] = v
42
+
43
+ return self .__processPost (PASTEBIN_API_POST_URL , postData )
44
+
45
+ def createPasteFromFile (self , filename , api_paste_name = '' , api_paste_format = '' , api_paste_private = '' , api_paste_expire_date = '' ):
46
+
47
+ try :
48
+ fileToOpen = open (filename , 'r' )
49
+ fileToPaste = fileToOpen .read ()
50
+ fileToOpen .close ()
51
+
52
+ return self .createPaste (fileToPaste , api_paste_name , api_paste_format , api_paste_private , api_paste_expire_date )
53
+ except IOError as e :
54
+ raise PastebinFileException (str (e ))
55
+
56
+
57
+ def __processPost (self , url , data ):
58
+
59
+ request = urllib2 .urlopen (url , urllib .urlencode (data ))
60
+ response = str (request .read ())
61
+ request .close ()
62
+
63
+ if re .search ('^Bad API request' , response ):
64
+ raise PastebinBadRequestException (response )
65
+ elif re .search ('^No pastes found' , response ):
66
+ raise PastebinNoPastesException
67
+
68
+ return response
69
+
70
+ def getUserKey (self , api_user_name , api_user_password ):
71
+
72
+ postData = {
73
+ 'api_dev_key' :self .api_dev_key ,
74
+ 'api_user_name' :api_user_name ,
75
+ 'api_user_password' :api_user_password
76
+ }
77
+
78
+ self .__api_user_key = self .__processPost (PASTEBIN_API_LOGIN_URL , postData )
79
+ self .__api_user_paste_list = []
80
+ return self .__api_user_key
81
+
82
+ def listUserPastes (self , api_results_limit = 50 ):
83
+
84
+ postData = {
85
+ 'api_dev_key' :self .api_dev_key ,
86
+ 'api_user_key' : self .api_user_key ,
87
+ 'api_results_limit' : api_results_limit ,
88
+ 'api_option' :OPTION_LIST
89
+ }
90
+
91
+ pastesList = self .__processPost (PASTEBIN_API_POST_URL , postData )
92
+ self .__api_user_paste_list = self .__parseXML (pastesList )
93
+
94
+ return self .__api_user_paste_list
95
+
96
+ def listTrendingPastes (self ):
97
+
98
+ postData = {
99
+ 'api_dev_key' :self .api_dev_key ,
100
+ 'api_option' :OPTION_TRENDS
101
+ }
102
+
103
+ trendsList = self .__processPost (PASTEBIN_API_POST_URL , postData )
104
+ trendsList = self .__parseXML (trendsList )
105
+
106
+ return trendsList
107
+
108
+ def __parseUser (self , xmlString ):
109
+ retList = []
110
+ userElements = xmlString .getElementsByTagName ('user' )
111
+
112
+ retList .append ({
113
+ 'user_name' :userElements [0 ].getElementsByTagName ('user_name' )[0 ].childNodes [0 ].nodeValue ,
114
+ 'user_format_short' :userElements [0 ].getElementsByTagName ('user_format_short' )[0 ].childNodes [0 ].nodeValue ,
115
+ 'user_expiration' :userElements [0 ].getElementsByTagName ('user_expiration' )[0 ].childNodes [0 ].nodeValue ,
116
+ 'user_avatar_url' :userElements [0 ].getElementsByTagName ('user_avatar_url' )[0 ].childNodes [0 ].nodeValue ,
117
+ 'user_private' :userElements [0 ].getElementsByTagName ('user_private' )[0 ].childNodes [0 ].nodeValue ,
118
+ 'user_website' :userElements [0 ].getElementsByTagName ('user_website' )[0 ].childNodes [0 ].nodeValue ,
119
+ 'user_email' :userElements [0 ].getElementsByTagName ('user_email' )[0 ].childNodes [0 ].nodeValue ,
120
+ 'user_location' :userElements [0 ].getElementsByTagName ('user_location' )[0 ].childNodes [0 ].nodeValue ,
121
+ 'user_account_type' :userElements [0 ].getElementsByTagName ('user_account_type' )[0 ].childNodes [0 ].nodeValue
122
+ })
123
+
124
+ return retList
125
+
126
+ def __parsePaste (self , xmlString ):
127
+ retList = []
128
+ pasteElements = xmlString .getElementsByTagName ('paste' )
129
+
130
+ for pasteElement in pasteElements :
131
+
132
+ try :
133
+ paste_title = pasteElement .getElementsByTagName ('paste_title' )[0 ].childNodes [0 ].nodeValue
134
+ except IndexError :
135
+ paste_title = ""
136
+
137
+ retList .append ({
138
+ 'paste_title' :paste_title ,
139
+ 'paste_key' :pasteElement .getElementsByTagName ('paste_key' )[0 ].childNodes [0 ].nodeValue ,
140
+ 'paste_date' :pasteElement .getElementsByTagName ('paste_date' )[0 ].childNodes [0 ].nodeValue ,
141
+ 'paste_size' :pasteElement .getElementsByTagName ('paste_size' )[0 ].childNodes [0 ].nodeValue ,
142
+ 'paste_expire_date' :pasteElement .getElementsByTagName ('paste_expire_date' )[0 ].childNodes [0 ].nodeValue ,
143
+ 'paste_private' :pasteElement .getElementsByTagName ('paste_private' )[0 ].childNodes [0 ].nodeValue ,
144
+ 'paste_format_long' :pasteElement .getElementsByTagName ('paste_format_long' )[0 ].childNodes [0 ].nodeValue ,
145
+ 'paste_format_short' :pasteElement .getElementsByTagName ('paste_format_short' )[0 ].childNodes [0 ].nodeValue ,
146
+ 'paste_url' :pasteElement .getElementsByTagName ('paste_url' )[0 ].childNodes [0 ].nodeValue ,
147
+ 'paste_hits' :pasteElement .getElementsByTagName ('paste_hits' )[0 ].childNodes [0 ].nodeValue ,
148
+ })
149
+
150
+ return retList
151
+
152
+
153
+ def __parseXML (self , xml , isPaste = True ):
154
+ retList = []
155
+ xmlString = parseString ("<pasteBin>%s</pasteBin>" % xml )
156
+
157
+ if isPaste :
158
+ retList = self .__parsePaste (xmlString )
159
+ else :
160
+ retList = self .__parseUser (xmlString )
161
+
162
+ return retList
163
+
164
+
165
+ def deletePaste (self , api_paste_key ):
166
+ postData = {
167
+ 'api_dev_key' :self .api_dev_key ,
168
+ 'api_user_key' :self .api_user_key ,
169
+ 'api_paste_key' :api_paste_key ,
170
+ 'api_option' :OPTION_DELETE
171
+ }
172
+
173
+ try :
174
+ retMsg = self .__processPost (PASTEBIN_API_POST_URL , postData )
175
+ except PastebinBadRequestException as e :
176
+ retMsg = str (e )
177
+
178
+ if re .search ('^Paste Removed' , retMsg ):
179
+ return True
180
+
181
+ return False
182
+
183
+
184
+ def getUserInfos (self ):
185
+
186
+ postData = {
187
+ 'api_dev_key' :self .api_dev_key ,
188
+ 'api_user_key' :self .api_user_key ,
189
+ 'api_option' :OPTION_USER_DETAILS
190
+ }
191
+
192
+ retData = self .__processPost (PASTEBIN_API_POST_URL , postData )
193
+ retData = self .__parseXML (retData , False )
194
+
195
+ return retData
0 commit comments