Skip to content

Commit

Permalink
refactor: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
cdnninja committed Apr 20, 2024
1 parent acb2c45 commit 93991ab
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
53 changes: 30 additions & 23 deletions yoto_api/YotoAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,26 @@ def login(self, username: str, password: str) -> Token:
# pass='audience=https%3A//api.yotoplay.com&client_id=FILL_THIS_IN&grant_type=password&password=FILL_THIS_IN&scope=openid%20email%20profile%20offline_access&username=FILL_THIS_IN%40gmail.com'
# curl -d "$pass" https://api.yotoplay.com/auth/token | jq '.access_token'

def get_devices(self, token: Token) -> dict[YotoPlayer]:
response = self._get_devices(token)
result = {}
for device in response["devices"]:
player: YotoPlayer = YotoPlayer(
id=device["deviceId"],
name=device["name"],
deviceType=device["deviceType"],
online=device["online"],
last_updated_at=datetime.datetime.now(pytz.utc),
)
result[player.id] = player

return result

def update_devices(self, token: Token, players: list[YotoPlayer]) -> None:
def update_players(self, token: Token, players: list[YotoPlayer]) -> None:
response = self._get_devices(token)
for player in players.values():
print(player)
player.last_updated_at = datetime.datetime.now(pytz.utc)
for device in response["devices"]:
if device["deviceId"] == player.id:
player.online = device["online"]
for item in response["devices"]:
if self.get_child_value(item,"deviceId") not in players:
player: YotoPlayer = YotoPlayer(
id=self.get_child_value(item,"deviceId"),
)
players[player.id] = player
deviceId = self.get_child_value(item,"deviceId")
players[deviceId].name=self.get_child_value(item,"name")
players[deviceId].deviceType=self.get_child_value(item,"deviceType")
players[deviceId].online=self.get_child_value(item,"online")
players[deviceId].last_updated_at=datetime.datetime.now(pytz.utc)

def get_library(self, token: Token) -> list[Card]:

def update_library(self, token: Token, library: dict[Card]) -> list[Card]:
response = self._get_cards(token)
cards = {}
for item in response["cards"]:
<<<<<<< Updated upstream
card: Card = Card(
id=self.get_child_value(item, "cardId"),
title=self.get_child_value(item, "card.title"),
Expand All @@ -113,6 +105,21 @@ def get_library(self, token: Token) -> list[Card]:
cards[card.id] = card
return cards
# TODO: parse the data and return a list of cards.
=======
if self.get_child_value(item,"cardId") not in library:
card: Card = Card(
id=self.get_child_value(item,"cardId"),
)
library[card.id] = card
library[self.get_child_value(item,"cardId")].title=self.get_child_value(item,"card.title")
library[self.get_child_value(item,"cardId")].description=self.get_child_value(item,"card.metadata.description")
library[self.get_child_value(item,"cardId")].author=self.get_child_value(item,"card.metadata.author")
library[self.get_child_value(item,"cardId")].category=self.get_child_value(item,"card.metadata.stories")
library[self.get_child_value(item,"cardId")].coverImageL=self.get_child_value(item,"card.metadata.cover.imageL")
library[self.get_child_value(item,"cardId")].seriesOrder=self.get_child_value(item,"card.metadata.cover.seriesorder")
library[self.get_child_value(item,"cardId")].seriesTitle=self.get_child_value(item,"card.metadata.cover.seriestitle")

>>>>>>> Stashed changes

def refresh_token(self, token: Token) -> Token:
# audience=https%3A//api.yotoplay.com&client_id=FILL_THIS_IN&grant_type=refresh_token&refresh_token=FILL_THIS_IN&scope=openid%20email%20profile%20offline_access
Expand Down
13 changes: 6 additions & 7 deletions yoto_api/YotoManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,21 @@ def __init__(self, username: str, password: str) -> None:
self.api: YotoAPI = YotoAPI()
self.players: dict = {}
self.token: Token = None
self.players: list = None
self.library: list = None
self.library: list = {}

def initialize(self) -> None:
self.token: Token = self.api.login(self.username, self.password)
self.players = self.api.get_devices(self.token)
self.library = self.api.get_library(self.token)
self.update_players_status()
self.update_cards()

def update_player_status(self) -> None:
def update_players_status(self) -> None:
# Updates the data with current player data.
self.api.update_devices(self.token, self.players)
self.api.update_players(self.token, self.players)

def update_cards(self) -> None:
# Updates library and all card data. Typically only required on startup.
# TODO: Should update the self.library object with a current dict of players. Should it do details for all cards too or separate?
self.library = self.api.update_library(self.token)
self.api.update_library(self.token, self.library)

def check_and_refresh_token(self) -> bool:
if self.token is None:
Expand Down

0 comments on commit 93991ab

Please sign in to comment.