· 5 years ago · Jul 02, 2020, 11:00 AM
1from typing import Dict, Final, Union
2from dataclasses import dataclass
3
4import aiohttp
5
6
7@dataclass
8class Pastebin:
9 """Class to pastebin API (see https://pastebin.com/api/)."""
10
11 api_user_key: str
12 api_dev_key: str
13
14 paste_expire_date: str
15 paste_private: int
16 paste_name: str
17
18 def __post_init__(self):
19 self.post_url: Final[str] = "https://pastebin.com/api/api_post.php"
20 self.raw_private: Final[str] = "https://pastebin.com/api/api_raw.php"
21 self.raw_public: Final[str] = "https://pastebin.com/raw/"
22 self.api_option: str = ""
23
24 async def get_post_response(self, data: Dict[str, Union[str, int]],
25 url: str) -> Union[str, tuple]:
26 """Get response from server.
27
28 Args:
29 data (Dict[str, Union[str, int]]): data to post requests
30 url (str): url to requests
31
32 Returns:
33 Union[str, tuple]: response`s text
34
35 """
36 async with aiohttp.ClientSession() as ses:
37 res = await ses.post(url, data=data)
38 if res.status == 200:
39 return await res.text()
40 return res.status, res.text(), "Error!"
41
42 async def create_paste(self, code: str, paste_format: str):
43 """Create new paste on pastebin.
44
45 Args:
46 code (str): code to paste
47 paste_format (str): paste format # python
48
49 """
50 self.api_option = "paste"
51 data: Dict[str, Union[str, int]] = {
52 "api_option": self.api_option,
53 "api_dev_key": self.api_dev_key,
54 "api_user_key": self.api_user_key,
55 "api_paste_private": self.paste_private,
56 "api_paste_name": self.paste_name,
57 "api_paste_format": paste_format,
58 "api_paste_code": code,
59 "api_paste_expire_date": self.paste_expire_date
60 }
61 self.get_post_response(data, self.post_url)
62
63 async def get_user_publications(self, results_limit: int = 50):
64 """Return user publications on p-bin.
65
66 Args:
67 results_limit (int, optional): limit of count results user`s publications.
68 Defaults to 50.
69
70 """
71 self.api_option = "list"
72 data: Dict[str, Union[str, int]] = {
73 "api_option": self.api_option,
74 "api_dev_key": self.api_dev_key,
75 "api_user_key": self.api_user_key,
76 "api_results_limit": results_limit
77 }
78 self.get_post_response(data, self.post_url)
79
80 async def delete_paste(self, paste_key: str):
81 """Delete user`s paste.
82
83 Args:
84 paste_key (str): paste to delete key
85
86 """
87 self.api_option = "delete"
88 data: Dict[str, str] = {
89 "api_option": self.api_option,
90 "api_dev_key": self.api_dev_key,
91 "api_user_key": self.api_user_key,
92 "api_paste_key": paste_key
93 }
94 self.get_post_response(data, self.post_url)
95
96 async def get_user_settings(self):
97 """Return user info and settings."""
98 self.api_option = "userdetails"
99 data: Dict[str, str] = {
100 "api_option": self.api_option,
101 "api_dev_key": self.api_dev_key,
102 "api_user_key": self.api_user_key,
103 }
104 self.get_post_response(data, self.post_url)
105
106 async def get_private_paste_rawdata(self, paste_key: str):
107 """Get raw paste output of users pastes including 'private' pastes.
108
109 Args:
110 paste_key (str): key of paste to get content
111
112 """
113 self.api_option = "show_paste"
114 data: Dict[str, str] = {
115 "api_option": self.api_option,
116 "api_dev_key": self.api_dev_key,
117 "api_user_key": self.api_user_key,
118 "api_paste_key": paste_key
119 }
120 self.get_post_response(data, self.raw_private)
121
122 async def get_public_paste_rawdata(self, paste_key: str):
123 """Get raw paste output of any 'public' & 'unlisted' pastes.
124
125 Args:
126 paste_key (str): key of paste to get content
127
128 """
129 async with aiohttp.ClientSession() as ses:
130 res = await ses.post(self.raw_public + paste_key)
131 if res.status == 200:
132 return await res.text()
133 return res.status, res.text(), "Error!"