Skip to content

Commit 5ab0b9b

Browse files
committed
add functionality to get the raw output of the paste using the api_paste_key, encode the api_paste_code to utf-8 and decode the response from getPasteRawOutput
1 parent 4d41c59 commit 5ab0b9b

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

pastebin_python/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66
from pastebin import PastebinPython
77

8-
__version__ = "1.1"
8+
__version__ = "1.2"
99
__app_name__ = "pastebin_python"
1010
__description__ = "A complete pastebin.com API wrapper for Python"
1111
__author__ = "Ferdinand Silva"

pastebin_python/pastebin.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import urllib
1111
from xml.dom.minidom import parseString
1212
from pastebin_options import OPTION_PASTE, OPTION_LIST, OPTION_TRENDS, OPTION_DELETE, OPTION_USER_DETAILS
13-
from pastebin_constants import PASTEBIN_API_POST_URL, PASTEBIN_API_LOGIN_URL
14-
from pastebin_exceptions import PastebinBadRequestException, PastebinNoPastesException, PastebinFileException
13+
from pastebin_constants import PASTEBIN_API_POST_URL, PASTEBIN_API_LOGIN_URL, PASTEBIN_RAW_URL
14+
from pastebin_exceptions import PastebinBadRequestException, PastebinNoPastesException, PastebinFileException, PastebinHTTPErrorException
1515

1616
class PastebinPython(object):
1717
"""This is the main class to be instantiated to use pastebin.com functionality
@@ -77,6 +77,7 @@ def createPaste(self, api_paste_code, api_paste_name='', api_paste_format='', ap
7777
7878
"""
7979
api_user_key = self.api_user_key if self.api_user_key else ""
80+
api_paste_code = api_paste_code.encode('utf-8') if api_paste_code else ""
8081

8182
postData = {
8283
'api_option':OPTION_PASTE,
@@ -348,4 +349,23 @@ def getUserInfos(self):
348349
retData = self.__processPost(PASTEBIN_API_POST_URL, postData)
349350
retData = self.__parseXML(retData, False)
350351

351-
return retData
352+
return retData
353+
354+
def getPasteRawOutput(self, api_paste_key):
355+
"""This will get the raw output of the paste
356+
357+
:param api_paste_key: this is the paste key that which you can get in the :meth:`pastebin_python.pastebin.PastebinPython.listUserPastes` function
358+
:type api_paste_key: str
359+
:returns: str -- raw output of the paste
360+
:raises: :exc:`pastebin_python.pastebin_exceptions.PastebinHTTPErrorException`
361+
362+
"""
363+
364+
try:
365+
request = urllib2.urlopen("%s%s" % (PASTEBIN_RAW_URL, api_paste_key))
366+
response = request.read()
367+
request.close()
368+
except urllib2.HTTPError as e:
369+
raise PastebinHTTPErrorException(str(e))
370+
371+
return response.decode('utf-8')

pastebin_python/pastebin_constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
.. moduleauthor:: Ferdinand Silva <[email protected]>
66
77
"""
8-
PASTEBIN_API_URL = "http://pastebin.com/api/" #: The pastebin.com API base URL
8+
PASTEBIN_URL = "http://pastebin.com/" #: The pastebin.com base url
9+
PASTEBIN_RAW_URL = "%s%s" % (PASTEBIN_URL, "raw.php?i=")
10+
PASTEBIN_API_URL = "%s%s" % (PASTEBIN_URL, "api/") #: The pastebin.com API base URL
911
PASTEBIN_API_POST_URL = "%s%s" % (PASTEBIN_API_URL, "api_post.php") #: The pastebin.com API POST URL
1012
PASTEBIN_API_LOGIN_URL = "%s%s" % (PASTEBIN_API_URL, "api_login.php") #: The pastebin.com API login URL
1113

pastebin_python/pastebin_exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ class PastebinNoPastesException(Exception):
1212
pass
1313

1414
class PastebinFileException(Exception):
15+
pass
16+
17+
class PastebinHTTPErrorException(Exception):
1518
pass

0 commit comments

Comments
 (0)