fix: correct typing issues

This commit is contained in:
Benex254
2024-08-05 09:47:05 +03:00
parent 93a6d6cfc9
commit d458085a4d
10 changed files with 90 additions and 30 deletions

View File

@@ -15,7 +15,7 @@ def completed(config: Config):
print("Please run: fastanime anilist loggin")
exit_app()
anime_list = AniList.get_anime_list("COMPLETED")
if not anime_list:
if not anime_list or not anime_list[1]:
return
if not anime_list[0]:
return

View File

@@ -17,7 +17,7 @@ def dropped(config: Config):
anime_list = AniList.get_anime_list("DROPPED")
if not anime_list:
return
if not anime_list[0]:
if not anime_list[0] or not anime_list[1]:
return
media = [
mediaListItem["media"]

View File

@@ -12,6 +12,7 @@ from ....constants import (
APP_CACHE_DIR,
APP_DATA_DIR,
APP_NAME,
ICON_PATH,
NOTIFICATION_BELL,
PLATFORM,
)
@@ -27,6 +28,7 @@ def notifier(config: Config):
notified = os.path.join(APP_DATA_DIR, "last_notification.json")
anime_image = os.path.join(APP_CACHE_DIR, "notification_image")
notification_duration = config.notification_duration * 60
app_icon = ""
if not config.user:
print("Not Authenticated")
@@ -55,6 +57,15 @@ def notifier(config: Config):
time.sleep(timeout * 60)
continue
data = result[1]
if not data:
print(result)
logger.warning(
"Something went wrong this could mean anilist is down or you have lost internet connection"
)
logger.info("sleeping...")
time.sleep(timeout * 60)
continue
# pyright:ignore
notifications = data["data"]["Page"]["notifications"]
if not notifications:
@@ -86,7 +97,9 @@ def notifier(config: Config):
if resp.status_code == 200:
with open(anime_image, "wb") as f:
f.write(resp.content)
ICON_PATH = anime_image
app_icon = anime_image
else:
app_icon = ICON_PATH
past_notifications[f"{id}"] = notification_["episode"]
with open(notified, "w") as f:
@@ -96,9 +109,9 @@ def notifier(config: Config):
title=title,
message=message,
app_name=APP_NAME,
app_icon=ICON_PATH,
app_icon=app_icon,
hints={
"image-path": ICON_PATH,
"image-path": app_icon,
"sound-file": NOTIFICATION_BELL,
},
timeout=notification_duration,

View File

@@ -17,7 +17,7 @@ def paused(config: Config):
anime_list = AniList.get_anime_list("PAUSED")
if not anime_list:
return
if not anime_list[0]:
if not anime_list[0] or not anime_list[1]:
return
media = [
mediaListItem["media"]

View File

@@ -17,7 +17,7 @@ def planning(config: Config):
anime_list = AniList.get_anime_list("PLANNING")
if not anime_list:
return
if not anime_list[0]:
if not anime_list[0] or not anime_list[1]:
return
media = [
mediaListItem["media"]

View File

@@ -17,7 +17,7 @@ def rewatching(config: Config):
anime_list = AniList.get_anime_list("REPEATING")
if not anime_list:
return
if not anime_list[0]:
if not anime_list[0] or not anime_list[1]:
return
media = [
mediaListItem["media"]

View File

@@ -17,7 +17,7 @@ def watching(config: Config):
anime_list = AniList.get_anime_list("CURRENT")
if not anime_list:
return
if not anime_list[0]:
if not anime_list[0] or not anime_list[1]:
return
media = [
mediaListItem["media"]

View File

@@ -586,7 +586,7 @@ def anilist_options(config, anilist_config: QueryDict):
default=False,
):
success, data = AniList.delete_medialist_entry(selected_anime["id"])
if not success:
if not success or not data:
print("Failed to delete", data)
elif not data.get("deleted"):
print("Failed to delete", data)
@@ -800,7 +800,7 @@ def handle_animelist(anilist_config, config: Config, list_type: str):
exit(1)
anilist(config, anilist_config)
return
if not anime_list[0]:
if not anime_list[0] or not anime_list[1]:
print("Sth went wrong", anime_list)
if not config.use_rofi:
input("Enter to continue")

View File

@@ -165,3 +165,50 @@ class AnilistPages(TypedDict):
class AnilistDataSchema(TypedDict):
data: AnilistPages
Error: str
class AnilistNotification(TypedDict):
id: int
type: str
episode: int
context: str
createdAt: str
media: AnilistBaseMediaDataSchema
class AnilistNotificationPage(TypedDict):
pageInfo: AnilistPageInfo
notifications: list[AnilistNotification]
class AnilistNotificationPages(TypedDict):
Page: AnilistNotificationPage
class AnilistNotifications(TypedDict):
data: AnilistNotificationPages
class AnilistMediaList(TypedDict):
media: AnilistBaseMediaDataSchema
status: str
progress: int
score: int
repeat: int
notes: str
startDate: AnilistDateObject
completedAt: AnilistDateObject
createdAt: str
class AnilistMediaListPage(TypedDict):
pageInfo: AnilistPageInfo
mediaList: list[AnilistMediaList]
class AnilistMediaListPages(TypedDict):
Page: AnilistMediaListPage
class AnilistMediaLists(TypedDict):
data: AnilistMediaListPages

View File

@@ -7,7 +7,12 @@ from typing import Literal
import requests
from .anilist_data_schema import AnilistDataSchema, AnilistUser
from .anilist_data_schema import (
AnilistDataSchema,
AnilistMediaLists,
AnilistNotifications,
AnilistUser,
)
from .queries_graphql import (
airing_schedule_query,
anime_characters_query,
@@ -52,7 +57,9 @@ class AniListApi:
self.user_id = user_info["id"] # pyright:ignore
return user_info
def get_notification(self):
def get_notification(
self,
) -> tuple[bool, AnilistNotifications] | tuple[bool, None]:
return self._make_authenticated_request(notification_query)
def reset_notification_count(self):
@@ -77,19 +84,22 @@ class AniListApi:
status: Literal[
"CURRENT", "PLANNING", "COMPLETED", "DROPPED", "PAUSED", "REPEATING"
],
):
) -> tuple[bool, AnilistMediaLists] | tuple[bool, None]:
variables = {"status": status, "userId": self.user_id}
return self._make_authenticated_request(media_list_query, variables)
def get_medialist_entry(self, mediaId: int):
def get_medialist_entry(
self, mediaId: int
) -> tuple[bool, dict] | tuple[bool, None]:
variables = {"mediaId": mediaId}
return self._make_authenticated_request(get_medialist_item_query, variables)
def delete_medialist_entry(self, mediaId: int):
result = self.get_medialist_entry(mediaId)
if not result[0]:
data = result[1]
if not result[0] or not data:
return result
id = result[1]["data"]["MediaList"]["id"]
id = data["data"]["MediaList"]["id"]
variables = {"id": id}
return self._make_authenticated_request(delete_list_entry_query, variables)
@@ -139,26 +149,16 @@ class AniListApi:
logger.warning(
"Timeout has been exceeded this could mean anilist is down or you have lost internet connection"
)
return (
False,
{
"Error": "Timeout Exceeded for connection there might be a problem with your internet or anilist is down."
},
) # type: ignore
return (False, None)
except requests.exceptions.ConnectionError:
logger.warning(
"ConnectionError this could mean anilist is down or you have lost internet connection"
)
return (False, None)
return (
False,
{
"Error": "There might be a problem with your internet or anilist is down."
},
) # type: ignore
except Exception as e:
logger.error(f"Something unexpected occured {e}")
return (False, {"Error": f"{e}"}) # type: ignore
return (False, None) # type: ignore
def get_watchlist(self):
variables = {"status": "CURRENT", "userId": self.user_id}