Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update _Store_Token for api auth changes, and add several MDList functions #8

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions MangaDexPy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .cover import Cover
from .network import NetworkChapter
from .search import SearchMapping

from .mdlist import MDList
INCLUDE_ALL = ["cover_art", "manga", "chapter", "scanlation_group", "author", "artist", "user", "leader", "member"]


Expand Down Expand Up @@ -104,7 +104,7 @@ def _store_token(self, post):
self.login_success = True
self.session_token = resp["token"]["session"]
self.refresh_token = resp["token"]["refresh"]
self.session.headers["Authorization"] = resp["token"]["session"]
self.session.headers["Authorization"] = "Bearer " + resp["token"]["session"]
return True

def get_manga(self, uuid: str, includes: list = None) -> Manga:
Expand Down Expand Up @@ -174,6 +174,14 @@ def get_manga_covers(self, mg: Manga, params: dict = None) -> List[Cover]:
params["manga[]"] = mg.id
return self._retrieve_pages(f"{self.api}/cover", Cover, call_limit=100, params=params)

def get_manga_feed(self, mg: Manga, limit: int = 100, params: dict = None, includes: list = None) -> List[Chapter]:
"""Gets the feed of a specific Manga."""
includes = INCLUDE_ALL if not includes else includes
params = params or {}
if includes:
params["includes[]"] = includes
return self._retrieve_pages(f"{self.api}/manga/{mg.id}/feed", Chapter, call_limit=100, params=params, limit=limit)

def get_cover(self, uuid: str) -> Cover:
"""Gets a cover with a specific uuid."""
req = self.session.get(f"{self.api}/cover/{uuid}")
Expand Down Expand Up @@ -239,6 +247,52 @@ def get_user_list(self, limit: int = 100) -> List[Manga]:
raise NotLoggedInError
return self._retrieve_pages(f"{self.api}/user/follows/manga", Manga, limit=limit, call_limit=100)

def get_MDList(self, uuid: str) -> MDList:
"""Gets an MDList with a specific uuid."""
req = self.session.get(f"{self.api}/list/{uuid}")
if req.status_code == 200:
resp = req.json()
return MDList(resp["data"], self)
elif req.status_code == 404:
raise NoContentError(req)
else:
return APIError(req)

def get_MDList_feed(self, uuid: str, limit: int = 100, params: dict = None) -> List[Chapter]:
"""Gets the feed of the specified MDList"""
params = params or {}
return self._retrieve_pages(f"{self.api}/list/{uuid}/feed", Chapter, call_limit=100, limit=limit, params=params)

def get_user_MDList(self, uuid: str = None, limit:int = 100) -> List[MDList]:
"""Gets the currently logged user's MDLists, or a specified user's created MDLists."""
url = ""
if uuid is not None:
url = f"{self.api}/{uuid}/list"
else:
if not self.login_success:
raise NotLoggedInError
url = f"{self.api}/user/list"
return self._retrieve_pages(url, MDList, limit)

def get_user_followed_MDList(self, limit:int = 100) -> List[MDList]:
"""Gets the currently logged user's followed MDLists"""
if not self.login_success:
raise NotLoggedInError
return self._retrieve_pages(f"{self.api}/user/follows/list", MDList, limit=limit)

def is_following_MDList(self, uuid:str) -> bool:
"""Returns whether the currently logged user is following an MDList"""
if not self.login_success:
raise NotLoggedInError
req = self.session.get(f"{self.api}/user/follows/list/{uuid}")
if req.status_code == 200:
return True
elif req.status_code == 404: # Two things could actually happen here, uuid is invalid or user isn't following, both return 404
return False
else:
raise APIError


def get_user_updates(self, limit: int = 100, params: dict = None) -> List[Chapter]:
"""Gets the currently logged user's manga feed."""
if not self.login_success:
Expand Down Expand Up @@ -279,9 +333,9 @@ def search(self, obj: str, params: dict,
m = SearchMapping(obj)
return self._retrieve_pages(f"{self.api}{m.path}", m.object, limit=limit, call_limit=100, params=params)

def _retrieve_pages(self, url: str, obj: Type[Union[Manga, Chapter, Group, Author, Cover]],
def _retrieve_pages(self, url: str, obj: Type[Union[Manga, Chapter, Group, Author, Cover, MDList]],
limit: int = 0, call_limit: int = 500,
params: dict = None) -> List[Union[Manga, Chapter, Group, Author, Cover]]:
params: dict = None) -> List[Union[Manga, Chapter, Group, Author, Cover, MDList]]:
params = params or {}
data = []
offset = 0
Expand Down
13 changes: 13 additions & 0 deletions MangaDexPy/mdlist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class MDList:
"""Represents a user created MDList"""
__slots__ = ("id", "name", "visibility", "titles", "creator","client")

def __init__(self, data, client):
self.id = data.get("id")
_attrs = data.get("attributes")
_rel = data.get("relationships", [])
self.name = _attrs.get("name")
self.visibility = _attrs.get("visibility")
self.titles = [x["id"] for x in _rel if x["type"] == "manga"]
self.creator = next((x["id"] for x in _rel if x["type"] == "user"), None)
self.client = client