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