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