-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvndb_calendar.py
421 lines (377 loc) · 13.2 KB
/
vndb_calendar.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Documentation
# VNDB API: https://api.vndb.org/kana#post-release
# VNDB Formatting Codes: https://vndb.org/d9#4
# ICS: https://icspy.readthedocs.io/en/stable/api.html#event
# Argparse: https://docs.python.org/3/library/argparse.html
# Dateparser: https://dateparser.readthedocs.io/en/latest/settings.html#handling-incomplete-dates
import argparse
import csv
import json
import os
import re
import sys
from datetime import datetime, timedelta
import dateparser
import requests
from ics import Calendar, Event
# Output files
_OUTPUT_FOLDER = "output/"
_CSV_FILE = "vndb-release.txt"
_JSON_FILE = "vndb-release.json"
_ICS_FILE = "vndb-calendar.ics"
# Date format
# _SHIFT_TIME = "today"
_SHIFT_TIME = (datetime.now() - timedelta(days=14)).strftime("%Y-%m-%d")
# Mid year
_MID_YEAR = "-09-15"
_YEAR_ONLY_REGEX = "^(\\d{4})$"
_YYYYMM_ONLY_REGEX = "^(\\d{4}-\\d{2})$"
# Block word list full of hacky regex
_TO_REPLACE = [
"(Windows)?( )?パッケージ(特装)?(初回)?版",
"( )?ダウンロード(通常)?(特装)?(豪華)?(カード)?版",
" オナホール同梱版",
" ダブルパック",
" (超)?スタァライト(EDITION)?(版)?",
" (通常)?(DL|DL|パッケージ)(通常)?(カード)?版",
" (通常)?PK版",
# " 通常DL版",
" デラックス(DL)?(PK)?版",
" PK版 デラックス版",
"( )?- .*? Version$",
"( )?- .*? Edition$",
" (Download|Standard|Package) Edition",
# "( )?- .*? Patch$",
# " ~ 追加エピソード",
"Normal Edition",
" 単体版",
" 通常版",
" 特[典|装]版",
" (超)?(特別)?(完全生産)?限定版",
" 抱き枕カバー付き(.*)?",
"( )?豪華(限定)?(特装)?版",
"( )?初回(限定)?(特典)?(特装)?版",
" 数量限定(.*)?版",
# Special cases
# " Episode:[0-9].*?$", # VenusBlood series
" B2タペストリー付き", # v49934
" ボイスドラマ付き限定版", # v51288
" ゆめは初恋BOX", # v50959
" タペストリー付属版", # v51309
" 夢だから", # v51928
" スペシャルボックス", # v50614
" DLsite限定版", # v53297
" ひばり抱き枕カバー付き", # v53124
" 調査員限定スペシャルセット", # v53545
]
# Normalize character (full -> half width)
_TO_REPLACE_WIDTH = (
(" ", " "),
("~", "~"),
("!", "!"),
("?", "?"),
# ("×", " x "),
)
# Query parameters
# FIELDS = "id,title,alttitle,languages.mtl,platforms,media,
# vns.rtype,producers,released,minage,patch,uncensored,official,extlinks"
FIELDS = "id, title, alttitle, released, vns.id"
# To get normalized filters from compact one:
# curl https://api.vndb.org/kana/release --json '{"filters":my_filters,"normalized_filters":true,"results":0}'
# Alternatively, use compact filter to get rid of all weirdness
# Side effect: no time shift
# filters = "0672171_4YsVe132gja2wzh_dHans-2wzh_dHant-N48721gwcomplete-N480281UJ81Xkx"
# fmt: off
_TAG_ID_FILTER = [
7, 83, 117, 153, 161, 358, 897, 937, 988,
1300, 1462, 2051, 2548, 3084, 3105, 3391, 3684
]
_PROD_ID_FILTER = [
# Bad scenario / nukige
215, 918, 1873, 1976, 2107, 2320, 2667, 3337, 4019, 4488, 5321, 5402, 7234,
11860, 12518, 13110, 13155,
# Personal preferences
65, 200, 226, 507, 1463, 1741, 3489, 4680, 5008, 7573, 7812,
10984, 11642, 13454, 13679, 18430, 20086, 20682,
# AIGC / photographic
20359, 20456, 20544, 20602, 22932,
# Otome game
567, 15102
]
# fmt: on
default_filters = [
"and",
# Comment the line below to show all language releases
["or", ["lang", "=", "ja"], ["lang", "=", "zh-Hans"], ["lang", "=", "zh-Hant"]],
# ["olang", "=", "ja"],
["released", "!=", "TBA"],
["released", ">=", _SHIFT_TIME],
["vn", "=", ["and", ["released", ">=", _SHIFT_TIME]]],
["rtype", "=", "complete"],
# Comment filters below to unhide BLG/Otome games
[
"vn",
"=",
[
"and",
["tag", "!=", "g542"],
["tag", "!=", "g2002"],
# This would filter out girl x girl romance as well, find a better way
# ["tag", "!=", ["g134", 2, 1.4]],
],
],
# More unintended tags
[
"vn",
"=",
[
"and",
# Loop through all tag ids in the list
*[["tag", "!=", f"g{i}"] for i in _TAG_ID_FILTER],
],
],
# No Da Capo/NEKOPARA/Sakura series, and much more
[
"producer",
"=",
[
"and",
# Loop through all producer ids in the list
*[["id", "!=", f"p{i}"] for i in _PROD_ID_FILTER],
],
],
]
default_data = {
"filters": default_filters,
"fields": FIELDS,
"sort": "released",
"reverse": False,
"results": 100,
"page": 1,
# "user": "null",
# "count": False,
"compact_filters": False,
"normalized_filters": True,
}
# Arguments for easy customization
parser = argparse.ArgumentParser()
parser.add_argument(
"-f",
"--filter",
required=False,
default=default_filters,
help="custom compact filter, see https://api.vndb.org/kana#filters",
)
# 2 is sufficient enough in most cases
parser.add_argument(
"-p",
"--max-page",
type=int,
required=False,
default=2,
help="max pages of query results",
)
parser.add_argument(
"-t",
"--shift-time",
type=int,
required=False,
default=None,
help='show "new" releases X days ago, it\'s really upcoming release if set to 0',
)
parser.add_argument(
"-d",
"--description",
"--intro",
type=bool,
required=False,
default=False,
help="add VN description to calendar event",
)
args = parser.parse_args()
def get_page(max_page, data):
# Reasons not using /vn
# 1. not working well with "released" filter
# 2. too many alttitles, or no alttitle at all
api_url = "https://api.vndb.org/kana/release"
headers = {"Content-Type": "application/json"}
all_results = []
# Parse parameters
# Use custom time shift, if any
if args.shift_time:
shift_time_new = (datetime.now() - timedelta(days=args.shift_time)).strftime(
"%Y-%m-%d"
)
data["filters"] = args.filter.replace(_SHIFT_TIME, shift_time_new)
# Or use the default value
else:
data["filters"] = args.filter
# Add intro if related parameter is set
if args.description:
data["fields"] = "id, title, alttitle, released, vns.id, vns.description"
for page in range(1, max_page + 1):
data["page"] = page
response = requests.post(
api_url, data=json.dumps(data), headers=headers, timeout=15
)
if response.status_code == 200:
json_data = response.json()
# Combine response
if "results" in json_data:
all_results.extend(json_data["results"])
else:
print("No results found for page ", page)
print(response)
break
else:
print("Post request failed")
print(response)
sys.exit()
print("Post request successful")
return all_results
# Process & Write results to JSON & CSV
def process_json(results):
processed_results = []
for result in results:
# Convert list to single string in case release contains multiple VNs
vns_ids = result.get("vns", [])
first_vns_id = vns_ids[0]["id"] if vns_ids else None
# Build new json
processed_result = {
"vid": first_vns_id,
"id": result["id"],
"released": result["released"],
}
if args.description:
first_vns_intro = vns_ids[0]["description"] if vns_ids else ""
processed_result["intro"] = first_vns_intro
# Prefer alternative title, if available
if result["alttitle"] is not None:
processed_result["title"] = result["alttitle"]
else:
processed_result["title"] = result["title"]
# Replace trailing release variations like `ダウンロード版` and `DLカード版`
for keyword in _TO_REPLACE:
processed_result["title"] = re.sub(keyword, "", processed_result["title"])
# Replace full width characters with normal ones
for pair in _TO_REPLACE_WIDTH:
processed_result["title"] = re.sub(
pair[0], pair[1], processed_result["title"]
)
if args.description and processed_result["intro"]:
processed_result["intro"] = re.sub(
pair[0], pair[1], processed_result["intro"]
)
processed_results.append(processed_result)
# Save results to JSON file
with open(
f"{_OUTPUT_FOLDER + _JSON_FILE}",
"w",
encoding="utf-8",
) as file:
json.dump(processed_results, file, ensure_ascii=False, indent=2)
# Save results to CSV file
with open(
_OUTPUT_FOLDER + _CSV_FILE, mode="w", newline="", encoding="utf-8"
) as csv_file:
# Do not save lengthy intro
fields_to_save = ["vid", "id", "title", "released"]
# Use semi-column seperator to avoid mismatches
writer = csv.DictWriter(csv_file, fieldnames=fields_to_save, delimiter=";")
writer.writeheader()
for result in processed_results:
# Compact fields
selected_data = {field: result[field] for field in fields_to_save}
writer.writerow(selected_data)
return processed_results
# Function borrowed from SteamWishlistCalendar
# https://github.com/icue/SteamWishlistCalendar/blob/166cc44fec28b01771ac39def0a340940d2a5bf3/swc.py#L34-L52
def last_day_of_next_month(dt):
"""
Returns the datetime of the last day of the next month.
Args:
dt: A datetime.datetime object.
Returns:
A datetime.datetime object.
"""
year = dt.year
next_next_month = dt.month + 2
if next_next_month > 12:
next_next_month -= 12
year = dt.year + 1
# Subtracte 1 day from the first day of the next next month, to get the last day of next month.
return datetime(year, next_next_month, 1) - timedelta(days=1)
# Make calendar
def make_calendar(processed_results):
cal = Calendar(creator="VNDBCalendar")
now = datetime.now()
event_dict = {}
for result in processed_results:
description_suffix = ""
vid, rid, title, release_date = (
result["vid"],
result["id"][1:], # TODO: err in game bundle (is a fix even possible?)
result["title"],
result["released"],
)
url = "https:///vndb.org/" + vid
# Parse date to better fit into reality
# Match release date like `2026`
year_only_match = re.match(_YEAR_ONLY_REGEX, release_date)
if year_only_match:
year = year_only_match.group(1)
# If Sep 15 of this year passed, use the end of year
mid_release_date = datetime.strptime(year + _MID_YEAR, "%Y-%m-%d").date()
release_date = year + (
_MID_YEAR if mid_release_date > now.date() else "-12-31"
)
description_suffix = f'\nEstimated on "{result["released"]}"'
# Complete remaining release date like `2024-03`
yyyymm_only_match = re.match(_YYYYMM_ONLY_REGEX, release_date)
if yyyymm_only_match:
release_date = dateparser.parse(
release_date,
settings={
"PREFER_DAY_OF_MONTH": "last",
"PREFER_DATES_FROM": "future",
},
)
while release_date.date() < now.date():
# If the estimated release date has already passed,
# pick the earliest upcoming last-of-a-month date
release_date = last_day_of_next_month(release_date)
# Only show estimation message in above cases
description_suffix = f'\nEstimated on "{result["released"]}"'
# Ensure release_date is a datetime object
if isinstance(release_date, str):
release_date = datetime.strptime(release_date, "%Y-%m-%d")
if args.description and result["intro"]:
description = url + "\n" + result["intro"] + description_suffix
else:
description = url + description_suffix
# TODO: include more info
event = Event(
uid=rid, # release identifier is more unique, but would conflict with event_dict below
summary=title,
description=description,
begin=release_date,
last_modified=now,
dtstamp=now,
categories=["vn_release"],
)
event.make_all_day()
# Only append events if it's a different VN
# Do NOT use release_date as a VN can have multiple releases on different dates
key = (vid, title)
if key not in event_dict:
event_dict[key] = event
for event in event_dict.values():
cal.events.append(event)
with open(_OUTPUT_FOLDER + _ICS_FILE, "w", encoding="utf-8") as f:
f.write(cal.serialize())
os.makedirs(_OUTPUT_FOLDER, exist_ok=True)
j = get_page(args.max_page, default_data)
res = process_json(j)
make_calendar(res)