-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff.py
66 lines (41 loc) · 1.69 KB
/
diff.py
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
from typing import NamedTuple
from yandex_music import Track
from base_client import null_client
from common import TrackType, get_parent_save_date, get_tracks_by_date
class DiffResult(NamedTuple):
added_tracks: list[Track]
deleted_tracks: list[Track]
def empty(self) -> bool:
return len(self.added_tracks) == 0 and len(self.deleted_tracks) == 0
def diff_tracks(tracks_from: list[Track], tracks_to: list[Track]) -> DiffResult:
return DiffResult(
added_tracks=list(set(tracks_to) - set(tracks_from)),
deleted_tracks=list(set(tracks_from) - set(tracks_to)),
)
def diff_object(object_type: str) -> DiffResult:
this_track = get_tracks_by_date(object_type)
parent_track = get_tracks_by_date(object_type, get_parent_save_date())
return diff_tracks(parent_track, this_track)
def get_track_info(track: Track) -> str:
artists = ", ".join(sorted([artist.name for artist in track.artists]))
return f"'{track.title}' by '{artists}' ({track.track_id})"
def show_diff(result: DiffResult) -> None:
print("Новые добавленные треки:")
for new_track in result.added_tracks:
print(get_track_info(new_track))
print()
print("Удалённые треки:")
for del_track in result.deleted_tracks:
print(get_track_info(del_track))
print()
def show_diff_object(object_type: str) -> None:
result = diff_object(object_type)
if not result.empty():
print(f"Разница по объекту {object_type}")
show_diff(result)
def main() -> None:
_ = null_client()
show_diff_object(TrackType.LIKES)
show_diff_object(TrackType.DISLIKES)
if __name__ == "__main__":
main()