Skip to content

Commit

Permalink
Add ssl verify option to the asyncpbwrap module
Browse files Browse the repository at this point in the history
This option can be used both when creating a new
instance of the AsyncPastebin class, and when calling
any of its static methods.
The same option was added to the AsyncPaste class
it is propagated during instantiation from the AsyncPastebin
methods

Ref: #9
  • Loading branch information
kostaskol committed Oct 10, 2020
1 parent 7ebe561 commit bc5cc81
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions pbwrap/asyncpbwrap.py
Expand Up @@ -7,7 +7,7 @@
import aiohttp

class AsyncPaste:
"""Defines a Paste from Pastebin paste contains the following fields:
"""Defines a Paste from Pastebin paste which contains the following fields:
key,
date,
title,
Expand All @@ -20,8 +20,9 @@ class AsyncPaste:
hits.
"""

def __init__(self, paste_dict):
def __init__(self, paste_dict, verify_ssl=True):
self.key = None
self.verify_ssl = verify_ssl
for k, v in paste_dict.items():
setattr(self, k, v)

Expand All @@ -34,7 +35,7 @@ async def get_raw_text(self):
:rtype: string, None
"""
if self.key is not None:
r = await aiohttp.get("https://pastebin.com/raw/" + self.key)
r = await aiohttp.get("https://pastebin.com/raw/" + self.key, **self.general_params())
return await r.text
return None

Expand All @@ -46,58 +47,78 @@ async def scrape_raw_text(self):
if self.key is not None:
parameter = {"i": self.key}
r = await aiohttp.get(
"https://scrape.pastebin.com/api_scrape_item.php", params=parameter
"https://scrape.pastebin.com/api_scrape_item.php", params=parameter,
**self.general_params()
)

return await r.text
return None

def general_params(self):
"""Returns parameters that should be included in every request
:returns: The options to be passed to aiohttp.*
:rtype: dictionary
"""
return {
"verify_ssl": self.verify_ssl
}

class AsyncPastebin(Pastebin):
"""Async version of Pastebin class.
Represents your communication with the Pastebin API through its functions
you can use every API endpoint avalaible.
Most functions require at least an api_dev_key parameter.
Functions for manipulating your pastes through the API require an api_user_key.
"""

def __init__(self, dev_key=None):
def __init__(self, dev_key=None, verify_ssl=True):
"""Instantiate a Pastebin Object
:param api_dev_key: Your API Pastebin key
:type api_dev_key: string
:param verify_ssl: If False, skips ssl certificate verification. Default: True
:type verify_ssl: bool
"""
super().__init__(api_dev_key=dev_key)
super().__init__(api_dev_key=dev_key, verify_ssl=verify_ssl)

@staticmethod
async def get_raw_paste(paste_id):
async def get_raw_paste(paste_id, verify_ssl=True):
"""Return raw string of given paste_id.
get_raw_paste(pasted_id)
:type paste_id: string
:param paste_id: The ID key of the paste
:param verify_ssl: If `False`, does not verify ssl certificate. Default: True
:type verify_ssl: bool
:returns: the text of the paste
:rtype: string
"""
r = await aiohttp.get("https://pastebin.com/raw/" + paste_id)
r = await aiohttp.get("https://pastebin.com/raw/" + paste_id, verify_ssl=verify_ssl)
return await r.text

@staticmethod
async def get_archive():
async def get_archive(verify_ssl=True):
"""Return archive paste link list.Archive contains 25 most recent pastes.
:param verify_ssl: If `False`, does not verify ssl certificate. Default: True
:type verify_ssl: bool
:returns: a list of url strings
:rtype: list
"""
r = await aiohttp.get("https://pastebin.com/archive")
r = await aiohttp.get("https://pastebin.com/archive", verify_ssl=verify_ssl)

return formatter.archive_url_format(await r.text)

@staticmethod
async def get_recent_pastes(limit=50, lang=None):
async def get_recent_pastes(limit=50, lang=None, verify_ssl=True):
"""get_recent_pastes(limit=50, lang=None)
Return a list containing dictionaries of paste.
Expand All @@ -108,17 +129,21 @@ async def get_recent_pastes(limit=50, lang=None):
:param lang: return only pastes from certain language defaults to None
:type lang: string
:param verify_ssl: If `False`, does not verify ssl certificate. Default: True
:type verify_ssl: bool
:returns: list of Paste objects.
:rtype: list(Paste)
"""
parameters = {"limit": limit, "lang": lang}

r = await aiohttp.get(
"https://scrape.pastebin.com/api_scraping.php", params=parameters
"https://scrape.pastebin.com/api_scraping.php", params=parameters,
verify_ssl=verify_ssl
)
paste_list = list()
for paste in await r.json():
paste_list.append(AsyncPaste(paste))
paste_list.append(AsyncPaste(paste, verify_ssl=verify_ssl))
return paste_list

except ImportError:
Expand Down

0 comments on commit bc5cc81

Please sign in to comment.