-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathpastebin3_requests.py
186 lines (131 loc) · 3.75 KB
/
pastebin3_requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
import requests
class PastebinError(Exception):
pass
class PastebinRequestError(PastebinError):
pass
# TODO: docstring
# TODO: correct comments and strings: my bad english :(
PASTEBIN_API_VERSION = "3.1"
__API_LOGIN_URL = "http://pastebin.com/api/api_login.php"
__API_POST_URL = "http://pastebin.com/api/api_post.php"
# We have 3 valid values available which you can use with the 'api_paste_private' parameter:
__PRIVATE_VARIANTS = {"public": 0, "unlisted": 1, "private": 2}
def __send_post_request_by_pastebin(url, params):
"""
:param url:
:param params:
:return:
"""
rs = requests.post(url, data=params)
if not rs.ok:
raise PastebinRequestError("HTTP status code: " + str(rs.status_code))
if rs.text.startswith("Bad API request"):
raise PastebinRequestError(rs.text)
return rs
def __send_api_post_request(params):
return __send_post_request_by_pastebin(__API_POST_URL, params)
def api_user_key(dev_key, user_name, user_password):
"""
:param dev_key:
:param user_name:
:param user_password:
:return:
"""
params = {
"api_dev_key": dev_key,
"api_user_name": user_name,
"api_user_password": user_password,
}
rs = __send_post_request_by_pastebin(__API_LOGIN_URL, params)
return rs.text
def user_pastes(dev_key, user_key, results_limit=50):
"""Listing Pastes Created By A User
:param dev_key:
:param user_key:
:param results_limit:
:return:
"""
params = {
"api_dev_key": dev_key,
"api_user_key": user_key,
"api_results_limit": results_limit,
"api_option": "list",
}
rs = __send_api_post_request(params)
return rs.text
def trending(dev_key):
"""Listing Trending Pastes
:param dev_key:
:return:
"""
params = {
"api_dev_key": dev_key,
"api_option": "trends",
}
rs = __send_api_post_request(params)
return rs.text
def paste(
dev_key, code, user_key=None, name=None, format=None, private=None, expire_date=None
):
"""Creating A New Paste
:param dev_key:
:param code:
:param user_key:
:param name:
:param format:
:param private:
:param expire_date:
:return:
"""
if private:
private = __PRIVATE_VARIANTS.get(private.lower())
if private == 2 and user_key is None:
raise PastebinError(
"Private paste only allowed in combination with api_user_key, "
"as you have to be logged into your account to access the paste"
)
params = {
"api_dev_key": dev_key,
"api_option": "paste",
"api_paste_code": code,
"api_user_key": user_key,
"api_paste_name": name,
"api_paste_format": format,
"api_paste_private": private,
"api_paste_expire_date": expire_date,
}
rs = __send_api_post_request(params)
return rs.text
def delete_paste(dev_key, user_key, paste_key):
"""Deleting A Paste Created By A User
:param dev_key:
:param user_key:
:param paste_key:
:return:
"""
params = {
"api_dev_key": dev_key,
"api_user_key": user_key,
"api_paste_key": paste_key,
"api_option": "delete",
}
rs = __send_api_post_request(params)
if rs.text.startswith("Paste Removed"):
return True
return False
def user_details(dev_key, user_key):
"""Getting A Users Information And Settings
:param dev_key:
:param user_key:
:return:
"""
params = {
"api_dev_key": dev_key,
"api_user_key": user_key,
"api_option": "userdetails",
}
rs = __send_api_post_request(params)
return rs.text