From 56c4612c1ed28152f41ed4f570e9316e1796dd15 Mon Sep 17 00:00:00 2001 From: Abhi <85984486+AbhiTheModder@users.noreply.github.com> Date: Tue, 10 Dec 2024 11:03:57 +0530 Subject: [PATCH] address dependabot issues --- utils/rentry.py | 54 +++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/utils/rentry.py b/utils/rentry.py index b3a3e5c..f38d811 100644 --- a/utils/rentry.py +++ b/utils/rentry.py @@ -8,8 +8,8 @@ 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 @@ def post(self, url, data=None, headers={}): 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 + )