Skip to content

Commit

Permalink
feat: card data mapping (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
cdnninja and pre-commit-ci[bot] authored Apr 20, 2024
1 parent ec65d0b commit 5fb72e6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
4 changes: 2 additions & 2 deletions yoto_api/Card.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Card:
category: str = None # $.card.metadata.category e.g. "stories"
author: str = None # $.card.metadata.author e.g. "Ladybird Audio Adventures"
coverImageL: str = None # $.card.metadata.cover.imageL e.g. "https://card-content.yotoplay.com/yoto/pub/WgoJMZ..."
seriestitle: str = (
seriesTitle: str = (
None # $.card.metadata.seriestitle e.g. "Ladybird Audio Adventures Volume 1"
)
seriesorder: int = None # $.card.metadata.seriesorder e.g. 4
seriesOrder: int = None # $.card.metadata.seriesorder e.g. 4
chapters: list[Chapter] = None # $.card.content.chapters
25 changes: 20 additions & 5 deletions yoto_api/YotoAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def login(self, username: str, password: str) -> Token:
# Authorization: Bearer access_token
# User-Agent: Yoto/2.73 (com.yotoplay.Yoto; build:10405; iOS 17.4.0) Alamofire/5.6.4


# 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'

Expand Down Expand Up @@ -94,7 +93,24 @@ def update_devices(self, token: Token, players: list[YotoPlayer]) -> None:
player.online = device["online"]

def get_library(self, token: Token) -> list[Card]:
cards = self._get_cards(token)
response = self._get_cards(token)
cards = {}
for item in response["cards"]:
card: Card = Card(
id=self.get_child_value(item, "cardId"),
title=self.get_child_value(item, "card.title"),
description=self.get_child_value(item, "card.metadata.description"),
author=self.get_child_value(item, "card.metadata.author"),
category=self.get_child_value(item, "card.metadata.stories"),
coverImageL=self.get_child_value(item, "card.metadata.cover.imageL"),
seriesOrder=self.get_child_value(
item, "card.metadata.cover.seriesorder"
),
seriesTitle=self.get_child_value(
item, "card.metadata.cover.seriestitle"
),
)
cards[card.id] = card
return cards
# TODO: parse the data and return a list of cards.

Expand Down Expand Up @@ -122,7 +138,6 @@ def refresh_token(self, token: Token) -> Token:
scope=response["scope"],
valid_until=valid_until,
)


def _get_devices(self, token: Token) -> None:
url = self.BASE_URL + "/device-v2/devices/mine"
Expand All @@ -140,7 +155,7 @@ def _get_cards(self, token: Token) -> dict:
headers = self._get_authenticated_headers(token)

response = requests.get(url, headers=headers).json()
_LOGGER.debug(f"{DOMAIN} - Get Card Library: {response}")
# _LOGGER.debug(f"{DOMAIN} - Get Card Library: {response}")
return response

# {
Expand Down Expand Up @@ -414,7 +429,7 @@ def _get_authenticated_headers(self, token: Token) -> dict:
"Authorization": token.token_type + " " + token.access_token,
}

def get_child_value(data, key):
def get_child_value(self, data, key):
value = data
for x in key.split("."):
try:
Expand Down
2 changes: 1 addition & 1 deletion yoto_api/YotoManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, username: str, password: str) -> None:
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.library = self.api.get_library(self.token)

def update_player_status(self) -> None:
# Updates the data with current player data.
Expand Down

0 comments on commit 5fb72e6

Please sign in to comment.