-
Notifications
You must be signed in to change notification settings - Fork 0
/
changelog.py
307 lines (248 loc) · 12 KB
/
changelog.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from enum import Enum
from pathlib import Path
from sovietscloset import SovietsCloset, SovietsClosetType
from utils import log
class ChangeType(Enum):
ADD = 1
DELETE = 2
MODIFY = 3
def __str__(self):
if self == ChangeType.ADD:
return "Added"
elif self == ChangeType.DELETE:
return "Deleted"
elif self == ChangeType.MODIFY:
return "Modified"
else:
raise RuntimeError()
def update_changelog(old_sovietscloset: SovietsCloset, new_sovietscloset: SovietsCloset):
log("update_changelog", "start")
changelog_timestamp = new_sovietscloset.timestamp.isoformat(timespec="seconds")
changelog_games: list[
tuple[
ChangeType,
SovietsCloset.Game,
SovietsCloset.Game,
]
] = list()
changelog_playlists: list[
tuple[
ChangeType,
SovietsCloset.Playlist,
SovietsCloset.Playlist,
]
] = list()
changelog_videos: list[
tuple[
ChangeType,
SovietsCloset.Video,
SovietsCloset.Video,
]
] = list()
def find_element(
elements: list[SovietsClosetType],
comparison_element: SovietsClosetType,
key: str,
) -> SovietsClosetType | None:
return next(
(
element
for element in elements
if element.__getattribute__(key) == comparison_element.__getattribute__(key)
),
None,
)
# find added games
for new_game in new_sovietscloset:
is_modified_game = False
found_old_game = find_element(old_sovietscloset.games, new_game, "name")
if not found_old_game:
# added game with new playlists and new videos
changelog_games.append((ChangeType.ADD, new_game, new_game))
for new_playlist in new_game:
changelog_playlists.append((ChangeType.ADD, new_playlist, new_playlist))
for new_video in new_playlist:
changelog_videos.append((ChangeType.ADD, new_video, new_video))
else:
# find added playlists
for new_playlist in new_game:
is_modified_playlist = False
found_old_playlist = find_element(found_old_game.playlists, new_playlist, "name")
if not found_old_playlist:
# added playlist with added videos in modified game
is_modified_game = True
changelog_playlists.append((ChangeType.ADD, new_playlist, new_playlist))
for new_video in new_playlist:
changelog_videos.append((ChangeType.ADD, new_video, new_video))
else:
# find added videos
for new_video in new_playlist:
found_old_video = find_element(found_old_playlist.videos, new_video, "id")
if not found_old_video:
# added video in modified playlist in modified game
is_modified_game = True
is_modified_playlist = True
changelog_videos.append((ChangeType.ADD, new_video, new_video))
if is_modified_playlist:
playlist_tuple = (ChangeType.MODIFY, found_old_playlist, new_playlist)
if playlist_tuple not in changelog_playlists:
changelog_playlists.append(playlist_tuple)
if is_modified_game:
game_tuple = (ChangeType.MODIFY, found_old_game, new_game)
if game_tuple not in changelog_games:
changelog_games.append(game_tuple)
# find deleted and modified games
for old_game in old_sovietscloset:
is_modified_game = False
# find deleted games
found_new_game = find_element(new_sovietscloset.games, old_game, "name")
if not found_new_game:
# deleted game with deleted playlists and deleted videos
changelog_games.append((ChangeType.DELETE, old_game, old_game))
for old_playlist in old_game:
changelog_playlists.append((ChangeType.DELETE, old_playlist, old_playlist))
for old_video in old_playlist:
changelog_videos.append((ChangeType.DELETE, old_video, old_video))
else:
# find modified games
if found_new_game != old_game:
is_modified_game = True
# find deleted and modified playlists
for old_playlist in old_game:
is_modified_playlist = False
# find deleted playlists
found_new_playlist = find_element(found_new_game.playlists, old_playlist, "name")
if not found_new_playlist:
# deleted playlist with deleted videos in modified game
is_modified_game = True
changelog_playlists.append((ChangeType.DELETE, old_playlist, old_playlist))
for old_video in old_playlist:
changelog_videos.append((ChangeType.DELETE, old_video, old_video))
else:
# find modified playlists
if found_new_playlist != old_playlist:
is_modified_game = True
is_modified_playlist = True
# find deleted and modified videos
for old_video in old_playlist:
# find deleted videos
found_new_video = find_element(found_new_playlist.videos, old_video, "id")
if not found_new_video:
# deleted video in modified playlist in modified game
is_modified_game = True
is_modified_playlist = True
changelog_videos.append((ChangeType.DELETE, old_video, old_video))
else:
if found_new_video != old_video:
# modified video in modified playlist in modified game
is_modified_game = True
is_modified_playlist = True
changelog_videos.append(
(ChangeType.MODIFY, old_video, found_new_video)
)
if is_modified_playlist:
playlist_tuple = (ChangeType.MODIFY, old_playlist, found_new_playlist)
if playlist_tuple not in changelog_playlists:
changelog_playlists.append(playlist_tuple)
if is_modified_game:
game_tuple = (ChangeType.MODIFY, old_game, found_new_game)
if game_tuple not in changelog_games:
changelog_games.append(game_tuple)
# sort by game date and stream date
changelog_games.sort(key=lambda g: g[2].name)
changelog_playlists.sort(key=lambda p: p[2].videos[0].date)
changelog_videos.sort(key=lambda v: v[2].date)
log(f"update_changelog", f"changelog timestamp {changelog_timestamp}")
log(f"update_changelog", f"found {len(changelog_games)=}")
log(f"update_changelog", f"found {len(changelog_playlists)=}")
log(f"update_changelog", f"found {len(changelog_videos)=}")
# write to file
changelog_not_empty = bool(changelog_games or changelog_playlists or changelog_videos)
if changelog_not_empty:
def badges(element: SovietsClosetType):
if isinstance(element, SovietsCloset.Category):
if element.enabled and not element.recentlyUpdated:
return ""
result = "("
if not element.enabled:
result += "disabled"
if element.recentlyUpdated:
result += ", "
if element.recentlyUpdated:
result += "recently updated"
result += ")"
return result
else:
if element.new:
return "(new)"
else:
return ""
def link(element: SovietsClosetType):
return f"[{element.title}]({element.url})"
def link_with_badges(element: SovietsClosetType):
return f"{link(element)} {badges(element)}".strip()
def header(
hn: int, change_type: ChangeType, element_type: str, element: SovietsClosetType
):
return f"{'#' * hn} {change_type} {element_type} {link(element)}\n\n"
def game_header(change_type: ChangeType, game: SovietsCloset.Game):
return header(3, change_type, "Game", game)
def playlist_header(change_type: ChangeType, playlist: SovietsCloset.Playlist):
return header(4, change_type, "Playlist", playlist)
# def video_header(change_type: ChangeType, video: SovietsCloset.Video):
# return header(5, change_type, "Video", video)
def category_changes(
category_old: SovietsCloset.Category,
category_new: SovietsCloset.Category,
):
result = ""
if category_old.enabled != category_new.enabled:
if category_new.enabled:
result += f"- Is now enabled.\n"
else:
result += f"- Is now disabled.\n"
if category_old.recentlyUpdated != category_new.recentlyUpdated:
if category_new.recentlyUpdated:
result += f"- Is now marked as recently updated.\n"
else:
result += f"- Is no longer marked as recently updated.\n"
if result:
result += "\n"
return result
def video_changes(video_old: SovietsCloset.Video, video_new: SovietsCloset.Video):
result = ""
for key in ["title", "date", "number", "bunnyId"]:
old_value = video_old.__getattribute__(key)
new_value = video_new.__getattribute__(key)
if old_value != new_value:
result += f" - Changed {key} from `{old_value}` to `{new_value}`\n"
if video_old.new != video_new.new:
if video_new.new:
result += f" - Is now marked as new.\n"
else:
result += f" - Is no longer marked as new.\n"
return result
changelog_md = "# Changelog\n\n"
changelog_md += f"## {changelog_timestamp}Z\n\n"
for game_change_type, game_one, game_two in changelog_games:
changelog_md += game_header(game_change_type, game_two)
changelog_md += category_changes(game_one, game_two)
for playlist_change_type, playlist_one, playlist_two in changelog_playlists:
if playlist_two.game == game_two:
changelog_md += playlist_header(playlist_change_type, playlist_two)
changelog_md += category_changes(playlist_one, playlist_two)
has_video_header = False
for video_change_type, video_one, video_two in changelog_videos:
if video_two.playlist == playlist_two:
if not has_video_header:
# videos need a heading to not mess up rendering of lists
changelog_md += f"##### Videos\n\n"
has_video_header = True
changelog_md += f"- {video_change_type} `v{video_two.id}` {link_with_badges(video_two)}\n"
changelog_md += video_changes(video_one, video_two)
changelog_md += "\n"
changelog_path = Path("CHANGELOG.md")
changelog_md = changelog_path.read_text().replace("# Changelog\n\n", changelog_md)
changelog_path.write_text(changelog_md)
log(f"update_changelog", "written CHANGELOG.md")
log(f"update_changelog", "done")