Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Der-Henning
GitHub Repository: Der-Henning/tgtg
Path: blob/main/tgtg_scanner/models/favorites.py
1494 views
1
import logging
2
from dataclasses import dataclass
3
4
from tgtg_scanner.errors import TgtgAPIError
5
from tgtg_scanner.models.item import Item
6
from tgtg_scanner.tgtg import TgtgClient
7
8
log = logging.getLogger("tgtg")
9
10
11
@dataclass
12
class AddFavoriteRequest:
13
item_id: str
14
item_display_name: str
15
proceed: bool
16
17
18
@dataclass
19
class RemoveFavoriteRequest:
20
item_id: str
21
item_display_name: str
22
proceed: bool
23
24
25
class Favorites:
26
def __init__(self, client: TgtgClient) -> None:
27
self.client = client
28
29
def is_item_favorite(self, item_id: str) -> bool:
30
"""Returns true if the provided item ID is in the favorites.
31
32
Args:
33
item_id (str): Item ID
34
Returns:
35
bool: true, if the provided item ID is in the favorites
36
37
"""
38
return any(item for item in self.client.get_favorites() if Item(item).item_id == item_id)
39
40
def get_item_by_id(self, item_id: str) -> Item:
41
"""Gets an item by the Item ID.
42
43
Args:
44
item_id (str): Item ID
45
Returns:
46
Item: the Item for the Item ID or an empty Item
47
48
"""
49
try:
50
return Item(self.client.get_item(item_id))
51
except TgtgAPIError:
52
return Item({})
53
54
def get_favorites(self) -> list[Item]:
55
"""Get all favorite items.
56
57
Return:
58
List: List of favorite items
59
60
"""
61
return [Item(item) for item in self.client.get_favorites()]
62
63
def add_favorites(self, item_ids: list[str]) -> None:
64
"""Adds all the provided item IDs to the favorites.
65
66
Args:
67
item_ids (str): Item ID list
68
69
"""
70
for item_id in item_ids:
71
self.client.set_favorite(item_id, True)
72
73
def remove_favorite(self, item_ids: list[str]) -> None:
74
"""Removes all the provided item IDs from the favorites.
75
76
Args:
77
item_ids (str): Item ID list
78
79
"""
80
for item_id in item_ids:
81
self.client.set_favorite(item_id, False)
82
83