Skip to content

Commit

Permalink
address dependabot issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiTheModder committed Dec 10, 2024
1 parent 27fdc79 commit 56c4612
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions utils/rentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"}

Expand All @@ -19,56 +19,66 @@ 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)

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
)

0 comments on commit 56c4612

Please sign in to comment.