AnonSean

Untitled

Mar 31st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.93 KB | None | 0 0
  1. """Contains a wrapper for the Pastebin API for easier usage."""
  2. import os
  3.  
  4. import requests
  5.  
  6. import pbwrap.formatter as formatter
  7. from pbwrap.constants import API_OPTIONS
  8. from pbwrap.models import Paste
  9.  
  10.  
  11. class Pastebin(object):
  12.     """Pastebin class represents your communication with the Pastebin API through its functions
  13.       you can use every API endpoint avalaible.
  14.  
  15.       Most functions require at least an api_dev_key parameter.
  16.       Functions for manipulating your pastes through the API require an api_user_key.
  17.    """
  18.  
  19.     def __init__(self, api_dev_key=None):
  20.         """Instantiate a Pastebin Object
  21.  
  22.           :param api_dev_key: Your API Pastebin key
  23.           :type api_dev_key: string
  24.        """
  25.         self.api_dev_key = api_dev_key
  26.         self.api_user_key = None   
  27.  
  28.     def authenticate(self, username, password):
  29.         """Authenticate through the API login endpoint
  30.           Your api_user_key attribute is set automatically
  31.  
  32.           :type username: string
  33.           :param username: Your username
  34.  
  35.           :type password: string
  36.           :param password: Your password
  37.  
  38.           :returns: your user_id key
  39.           :rtype: string
  40.        """
  41.         data = {
  42.             "api_dev_key": self.api_dev_key,
  43.             "api_user_name": username,
  44.             "api_user_password": password,
  45.         }
  46.  
  47.         r = requests.post("https://pastebin.com/api/api_login.php", data)
  48.  
  49.         self.api_user_key = r.text
  50.  
  51.         return self.api_user_key
  52.  
  53.     def get_user_details(self):
  54.         """Return user details in a dictionary.
  55.           Can only be user after authenticating with get_user_id(username, password).
  56.  
  57.           :returns: dictionary containing user details
  58.           :rtype: dictionary
  59.        """
  60.         data = {"api_dev_key": self.api_dev_key, "api_user_key": self.api_user_key}
  61.  
  62.         r = requests.post("https://pastebin.com/api/api_post.php", data)
  63.  
  64.         return formatter.user_from_xml(r.text)
  65.  
  66.     def get_trending(self):
  67.         """Return a list of paste objects created from the most trending pastes
  68.  
  69.           :returns: a list of Paste objects
  70.           :rtype: list
  71.        """
  72.         data = {"api_dev_key": self.api_dev_key, "api_option": API_OPTIONS["TREND"]} #i did an edit here
  73.  
  74.         r = requests.post("https://pastebin.com/api/api_post.php", data)
  75.  
  76.         return formatter.paste_list_from_xml(r.text)
  77.  
  78.     @staticmethod
  79.     def get_archive():
  80.         """Return archive paste link list.Archive contains 25 most recent pastes.
  81.  
  82.           :returns: a list of url strings
  83.           :rtype: list
  84.        """
  85.         r = requests.get("https://pastebin.com/archive")
  86.  
  87.         return formatter.archive_url_format(r.text)
  88.  
  89.     @staticmethod
  90.     def get_raw_paste(paste_id):
  91.         """Return raw string of given paste_id.
  92.  
  93.           get_raw_paste(pasted_id)
  94.  
  95.           :type paste_id: string
  96.           :param paste_id: The ID key of the paste
  97.  
  98.           :returns: the text of the paste
  99.           :rtype: string
  100.        """
  101.         r = requests.get("https://pastebin.com/raw/" + paste_id)
  102.         return r.text
  103.  
  104.     def create_paste(
  105.         self,
  106.         api_paste_code,
  107.         api_paste_private=0,
  108.         api_paste_name=None,
  109.         api_paste_expire_date=None,
  110.         api_paste_format=None,
  111.     ):
  112.         """Create a new paste if succesfull return it's url.
  113.  
  114.           :type api_paste_code: string
  115.           :param api_paste_code: your paste text
  116.  
  117.           :type api_paste_private: int
  118.           :param api_paste_private: valid values=0(public),1(unlisted),2(private)
  119.  
  120.           :type api_paste_name: string
  121.           :param api_user_name: your paste name
  122.  
  123.           :type api_paste_expire_date: string
  124.           :param api_paste_expire_date: check documentation for valid values
  125.  
  126.           :type api_paste_format: string
  127.           :param api_paste_format: check documentation for valid values
  128.  
  129.           :returns: new paste url
  130.           :rtype: string
  131.        """
  132.         data = {
  133.             "api_dev_key": self.api_dev_key,
  134.             "api_user_key": self.api_user_key,
  135.             "api_paste_code": api_paste_code,
  136.             "api_paste_private": api_paste_private,
  137.             "api_paste_name": api_paste_name,
  138.             "api_paste_expire_date": api_paste_expire_date,
  139.             "api_paste_format": api_paste_format,
  140.             "api_option": API_OPTIONS["PASTE"],
  141.         }
  142.  
  143.         # Filter data and remove dictionary None keys.
  144.         filtered_data = {k: v for k, v in data.items() if v is not None}
  145.  
  146.         r = requests.post("https://pastebin.com/api/api_post.php", filtered_data)
  147.  
  148.         return r.text
  149.  
  150.     def create_paste_from_file(
  151.         self,
  152.         filepath,
  153.         api_paste_private=0,
  154.         api_paste_name=None,
  155.         api_paste_expire_date=None,
  156.         api_paste_format=None,
  157.     ):
  158.         """Create a new paste from file if succesfull return it's url.
  159.  
  160.            :type filepath: string
  161.            :param filepath: the path of the file
  162.  
  163.            :type api_paste_private: int
  164.            :param api_paste_private: valid values=0(public),1(unlisted),2(private)
  165.  
  166.            :type api_paste_name: string
  167.            :param api_user_name: your paste name
  168.  
  169.            :type api_paste_expire_date: string
  170.            :param api_paste_expire_date: check documentation for valid values
  171.  
  172.            :type api_paste_format: string
  173.            :param api_paste_format: check documentation for valid values
  174.  
  175.            :returns: new paste url
  176.            :rtype: string
  177.            """
  178.         if os.path.exists(filepath):
  179.             api_paste_code = open(filepath).read()
  180.             return self.create_paste(
  181.                 api_paste_code,
  182.                 api_paste_private,
  183.                 api_paste_name,
  184.                 api_paste_expire_date,
  185.                 api_paste_format,
  186.             )
  187.         print("File not found")
  188.         return None
Add Comment
Please, Sign In to add comment