-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_event_table.py
130 lines (103 loc) · 4.62 KB
/
get_event_table.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
import pandas as pd
import requests
# from datetime import datetime
from functools import partial
import sys
pd.options.mode.copy_on_write = True
def unit_name_convert(in_str):
if in_str == "0_VS":
return "vs"
elif in_str == "1_L/n":
return "l/n"
elif in_str == "2_MMJ":
return "mmj"
elif in_str == "3_VBS":
return "vbs"
elif in_str == "4_WxS":
return "wxs"
elif in_str == "5_25":
return "n25"
elif in_str == "混合":
return "mix"
else:
print("unrecognized event type", in_str)
sys.exit(1)
def make_date_str(in_str, hour):
datetime_str = in_str.split("*")[0].strip()
return pd.to_datetime(datetime_str + f"T{hour}", format="%Y/%m/%dT%H")
def date_convert(in_df, start=False, end=False):
# res_sr = pd.Series(index = in_df.index)
make_date_str_T20 = partial(make_date_str, hour=20)
make_date_str_T15 = partial(make_date_str, hour=15)
make_date_str_T21 = partial(make_date_str, hour=21)
if start:
res_sr = in_df["開始日"]
# [special attend] World Link starts and ends at different time
world_link_sr = in_df["形式"] == "ワールドリンク"
res_sr[world_link_sr] = in_df[world_link_sr]["開始日"].apply(make_date_str_T20)
# [special attend] one event started with delay due to extended maintenance
res_sr[120] = pd.Timestamp(2024, 1, 31, 20)
res_sr[145] = pd.Timestamp(2024, 10, 12, 18)
# normal event start time
remains_sr = res_sr.apply(lambda x: type(x) == str)
res_sr[remains_sr] = in_df[remains_sr]["開始日"].apply(make_date_str_T15)
elif end:
res_sr = in_df["終了日"]
# [special attend] World Link starts and ends at different time
world_link_sr = in_df["形式"] == "ワールドリンク"
res_sr[world_link_sr] = in_df[world_link_sr]["終了日"].apply(make_date_str_T20)
# normal event end time
remains_sr = res_sr.apply(lambda x: type(x) == str)
res_sr[remains_sr] = in_df[remains_sr]["終了日"].apply(make_date_str_T21)
return res_sr
def get_event_table():
try:
pjsekai_res = requests.get("https://pjsekai.com/?2d384281f1")
if pjsekai_res.ok:
a = pd.read_html(pjsekai_res.content, index_col='No', encoding="utf-8",
attrs={"id": "sortable_table1"})[0]
# default columns belown
# No, 週目, イベント名, 形式, ユニット, タイプ, 書き下ろし楽曲, 開始日, 終了日, 日数, 参加人数
# a["ユニット"] = a["ユニット"].apply(unit_name_convert)
a["開始日"] = date_convert(a, start=True)
a["終了日"] = date_convert(a, end=True)
# a.to_csv("./event_data.csv", index=False, header=False)
return a
else:
raise ValueError("status code: " + str(pjsekai_res.status_code))
except Exception as e:
print("ERROR at fetchig event table")
print(e)
return pd.DataFrame(columns=["開始日", "終了日", "イベント名"])
def get_stream_table():
try:
pjsekai_res = requests.get("https://pjsekai.com/?1c5f55649f")
if pjsekai_res.ok:
a = pd.read_html(pjsekai_res.content,
encoding="utf-8",
attrs={"border": "0", "cellspacing": "1", "class": "style_table"})
# print(a[0])
a_temp = a[0][["No", "配信日時"]]
a_temp.columns = ["No", "配信日時"]
# print(a_temp)
a_temp = a_temp.drop_duplicates(ignore_index=True)
# convert Japanese datetime string to datetime object
a_temp["配信日時"] = a_temp["配信日時"].apply(
lambda x: pd.to_datetime(x[:x.index("(")] + x[x.index(")")+1:], format="%Y/%m/%d %H時%M分"))
a_temp.loc[:, "No"] = a_temp["No"].apply(lambda x: "プロセカ放送局 " + x)
# Be careful that No column includes the description of the stream
return a_temp
else:
raise ValueError("status code: " + str(pjsekai_res.status_code))
except Exception as e:
print("ERROR at fetchig stream table")
print(e)
return pd.DataFrame(columns=["No", "配信日時"])
test_table = get_stream_table()
# print(test_table)
if __name__ == "__main__":
print("##### stream table #####")
# print(get_stream_table().to_markdown())
print()
print("##### event table #####")
# print(get_event_table().to_markdown())