-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspotify.py
More file actions
111 lines (72 loc) · 2.37 KB
/
spotify.py
File metadata and controls
111 lines (72 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# To use this code, make sure you
#
# import json
#
# and then, to convert JSON from a string, do
#
# result = album_from_dict(json.loads(json_string))
# result = artist_from_dict(json.loads(json_string))
# result = playlist_from_dict(json.loads(json_string))
# result = profile_from_dict(json.loads(json_string))
# result = track_from_dict(json.loads(json_string))
from typing import Any, TypeVar, Type, cast
T = TypeVar("T")
def from_int(x: Any) -> int:
assert isinstance(x, int) and not isinstance(x, bool)
return x
def from_str(x: Any) -> str:
assert isinstance(x, str)
return x
def to_class(c: Type[T], x: Any) -> dict:
assert isinstance(x, c)
return cast(Any, x).to_dict()
class Error:
status: int
message: str
def __init__(self, status: int, message: str) -> None:
self.status = status
self.message = message
@staticmethod
def from_dict(obj: Any) -> 'Error':
assert isinstance(obj, dict)
status = from_int(obj.get("status"))
message = from_str(obj.get("message"))
return Error(status, message)
def to_dict(self) -> dict:
result: dict = {}
result["status"] = from_int(self.status)
result["message"] = from_str(self.message)
return result
class Track:
error: Error
def __init__(self, error: Error) -> None:
self.error = error
@staticmethod
def from_dict(obj: Any) -> 'Track':
assert isinstance(obj, dict)
error = Error.from_dict(obj.get("error"))
return Track(error)
def to_dict(self) -> dict:
result: dict = {}
result["error"] = to_class(Error, self.error)
return result
def album_from_dict(s: Any) -> Track:
return Track.from_dict(s)
def album_to_dict(x: Track) -> Any:
return to_class(Track, x)
def artist_from_dict(s: Any) -> Track:
return Track.from_dict(s)
def artist_to_dict(x: Track) -> Any:
return to_class(Track, x)
def playlist_from_dict(s: Any) -> Track:
return Track.from_dict(s)
def playlist_to_dict(x: Track) -> Any:
return to_class(Track, x)
def profile_from_dict(s: Any) -> Track:
return Track.from_dict(s)
def profile_to_dict(x: Track) -> Any:
return to_class(Track, x)
def track_from_dict(s: Any) -> Track:
return Track.from_dict(s)
def track_to_dict(x: Track) -> Any:
return to_class(Track, x)