address dependabot issues
This commit is contained in:
@@ -8,8 +8,8 @@ import urllib.request
|
||||
from http.cookies import SimpleCookie
|
||||
from json import loads as json_loads
|
||||
|
||||
BASE_PROTOCOL="https://"
|
||||
BASE_URL="rentry.co"
|
||||
BASE_PROTOCOL = "https://"
|
||||
BASE_URL = "rentry.co"
|
||||
|
||||
_headers = {"Referer": f"{BASE_PROTOCOL}{BASE_URL}"}
|
||||
|
||||
@@ -19,14 +19,20 @@ class UrllibClient:
|
||||
|
||||
def __init__(self):
|
||||
self.cookie_jar = http.cookiejar.CookieJar()
|
||||
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cookie_jar))
|
||||
self.opener = urllib.request.build_opener(
|
||||
urllib.request.HTTPCookieProcessor(self.cookie_jar)
|
||||
)
|
||||
urllib.request.install_opener(self.opener)
|
||||
|
||||
def get(self, url, headers={}):
|
||||
def get(self, url, headers=None):
|
||||
if headers is None:
|
||||
headers = {}
|
||||
request = urllib.request.Request(url, headers=headers)
|
||||
return self._request(request)
|
||||
|
||||
def post(self, url, data=None, headers={}):
|
||||
def post(self, url, data=None, headers=None):
|
||||
if headers is None:
|
||||
headers = {}
|
||||
postdata = urllib.parse.urlencode(data).encode()
|
||||
request = urllib.request.Request(url, postdata, headers)
|
||||
return self._request(request)
|
||||
@@ -34,41 +40,45 @@ class UrllibClient:
|
||||
def _request(self, request):
|
||||
response = self.opener.open(request)
|
||||
response.status_code = response.getcode()
|
||||
response.data = response.read().decode('utf-8')
|
||||
response.data = response.read().decode("utf-8")
|
||||
return response
|
||||
|
||||
|
||||
def raw(url):
|
||||
client = UrllibClient()
|
||||
return json_loads(client.get(f"{BASE_PROTOCOL}{BASE_URL}" + '/api/raw/{}'.format(url)).data)
|
||||
return json_loads(client.get(f"{BASE_PROTOCOL}{BASE_URL}/api/raw/{url}").data)
|
||||
|
||||
|
||||
def new(url, edit_code, text):
|
||||
client, cookie = UrllibClient(), SimpleCookie()
|
||||
|
||||
cookie.load(vars(client.get(f"{BASE_PROTOCOL}{BASE_URL}"))['headers']['Set-Cookie'])
|
||||
csrftoken = cookie['csrftoken'].value
|
||||
cookie.load(vars(client.get(f"{BASE_PROTOCOL}{BASE_URL}"))["headers"]["Set-Cookie"])
|
||||
csrftoken = cookie["csrftoken"].value
|
||||
|
||||
payload = {
|
||||
'csrfmiddlewaretoken': csrftoken,
|
||||
'url': url,
|
||||
'edit_code': edit_code,
|
||||
'text': text
|
||||
"csrfmiddlewaretoken": csrftoken,
|
||||
"url": url,
|
||||
"edit_code": edit_code,
|
||||
"text": text,
|
||||
}
|
||||
|
||||
return json_loads(client.post(f"{BASE_PROTOCOL}{BASE_URL}" + '/api/new', payload, headers=_headers).data)
|
||||
return json_loads(
|
||||
client.post(
|
||||
f"{BASE_PROTOCOL}{BASE_URL}" + "/api/new", payload, headers=_headers
|
||||
).data
|
||||
)
|
||||
|
||||
|
||||
def edit(url, edit_code, text):
|
||||
client, cookie = UrllibClient(), SimpleCookie()
|
||||
|
||||
cookie.load(vars(client.get(f"{BASE_PROTOCOL}{BASE_URL}"))['headers']['Set-Cookie'])
|
||||
csrftoken = cookie['csrftoken'].value
|
||||
cookie.load(vars(client.get(f"{BASE_PROTOCOL}{BASE_URL}"))["headers"]["Set-Cookie"])
|
||||
csrftoken = cookie["csrftoken"].value
|
||||
|
||||
payload = {
|
||||
'csrfmiddlewaretoken': csrftoken,
|
||||
'edit_code': edit_code,
|
||||
'text': text
|
||||
}
|
||||
payload = {"csrfmiddlewaretoken": csrftoken, "edit_code": edit_code, "text": text}
|
||||
|
||||
return json_loads(client.post(f"{BASE_PROTOCOL}{BASE_URL}" + '/api/edit/{}'.format(url), payload, headers=_headers).data)
|
||||
return json_loads(
|
||||
client.post(
|
||||
f"{BASE_PROTOCOL}{BASE_URL}/api/edit/{url}", payload, headers=_headers
|
||||
).data
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user