110 lines
3.6 KiB
Python
110 lines
3.6 KiB
Python
import time
|
|
|
|
import requests
|
|
|
|
|
|
class API:
|
|
def __init__(self, url: str, username: str = None, password: str = None):
|
|
self.url = url
|
|
self.auth = (username, password)
|
|
self.connected = self.test_connection()
|
|
|
|
def test_connection(self) -> bool:
|
|
tries = 10
|
|
delay = 1
|
|
for _ in range(tries):
|
|
try:
|
|
requests.get(self.url)
|
|
return True
|
|
except:
|
|
print(f"Connection failed, retrying in {delay} seconds")
|
|
time.sleep(delay)
|
|
delay *= 2
|
|
return False
|
|
|
|
def get(self, url, options: dict = None) -> requests.models.Response | None:
|
|
"""Get the response from the api.
|
|
|
|
Args:
|
|
----
|
|
- url (str): the part after the default api url (e.g. api.mangadex.org/*manga*)
|
|
- options (dict, optional): Options available by the API. Defaults to None.
|
|
|
|
Returns:
|
|
-------
|
|
- Response: the response from the api formatted as a json
|
|
- None: if the response is not 200
|
|
"""
|
|
url = f"{self.url}/{url}"
|
|
# print(url)
|
|
result = requests.get(url, auth=self.auth, params=options if options else None)
|
|
if not result.status_code == 200:
|
|
print(result.text)
|
|
return None
|
|
if "thumbnail" in url:
|
|
return result.content
|
|
return result.json()
|
|
|
|
def put(self, url, data: dict = None) -> requests.models.Response | None:
|
|
"""put the data to the api
|
|
|
|
Args:
|
|
url (str): the part after the default api url (e.g. api.mangadex.org/*manga*)
|
|
data (dict, optional): the data to be put. Defaults to None.
|
|
|
|
Returns:
|
|
Response: the response from the api formatted as a json
|
|
None: if the response is not 200
|
|
"""
|
|
url = f"{self.url}/{url}"
|
|
result = requests.put(url, auth=self.auth, json=data if data else None)
|
|
if not result.status_code == 200:
|
|
print(result.text)
|
|
return None
|
|
return result.json()
|
|
|
|
def patch(self, url, data: dict) -> requests.models.Response | None:
|
|
"""patch the data to the api
|
|
|
|
Args:
|
|
url (str): the part after the default api url (e.g. api.mangadex.org/*manga*)
|
|
data (dict): the data to be patched
|
|
|
|
Returns:
|
|
Response: the response from the api formatted as a json
|
|
None: if the response is not 200
|
|
"""
|
|
url = f"{self.url}/{url}"
|
|
result = requests.patch(url, auth=self.auth, json=data if data else None)
|
|
if not result.status_code == 200:
|
|
print(result.text)
|
|
return None
|
|
return result.json()
|
|
|
|
def post(self, url, data: dict = None) -> requests.models.Response | None:
|
|
"""post the data to the api
|
|
|
|
Args:
|
|
url (str): the part after the default api url (e.g. api.mangadex.org/*manga*)
|
|
data (dict): the data to be posted
|
|
|
|
Returns:
|
|
Response: the response from the api formatted as a json
|
|
None: if the response is not 200
|
|
"""
|
|
url = f"{self.url}/{url}"
|
|
result = requests.post(url, auth=self.auth, json=data if data else None)
|
|
if not result.status_code == 200:
|
|
print(result.text)
|
|
return None
|
|
return result.json()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("This is a module, not a script")
|
|
ap = API("https://api.mangadex.org")
|
|
res = ap.get(
|
|
"/manga?title=My Instant Death Ability Is So Overpowered, No One in This Other World Stands a Chance Against Me! —AΩ—"
|
|
)
|
|
print(res)
|