-
Notifications
You must be signed in to change notification settings - Fork 15
/
bilibili_calendar.py
140 lines (124 loc) · 4.88 KB
/
bilibili_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
from os import truncate
import re
import ast
from html.parser import HTMLParser
import datetime
keyword_list = [
'year',
'month',
'day',
'qdhd', # 庆典活动
'tdz', # 团队战
'tbhd', # 特别活动
'jqhd', # 剧情活动
'jssr', # 角色生日
]
event_keyword_list = [
'qdhd', #庆典活动 04:59
'tbhd', #特别活动 23:59
'jqhd', #剧情活动 23:59
'tdz', #团队战 23:59
]
class ContentParse(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.data = []
self.is_title = False
def handle_starttag(self, tag, attrs):
if len(attrs) != 0 and 'cl-t' in attrs[0]:
self.is_title = True
else:
self.is_title = False
def handle_data(self, data):
if self.is_title:
self.data.append(data)
def parse_content(day_content):
content_html = ''
data = {}
for keyword in event_keyword_list:
content_html = day_content[keyword]
parser = ContentParse()
parser.feed(content_html)
data[keyword] = parser.data
return data
def extract_calendar_data(js_text):
# 提取js的data部分转换为python对象
data_str = re.search(r'\[.*?\]', js_text, re.S).group(0)
data_str = data_str.replace('//', '#')
for keyword in keyword_list:
data_str = data_str.replace(keyword, f'"{keyword}"')
data = ast.literal_eval(data_str)
# 解析活动内容html
for i in range(len(data)):
for day in data[i]['day']:
content = parse_content(data[i]['day'][day])
data[i]['day'][day] = content
return data
def transform_calendar_data(data):
event_cache = {}
event_list = []
today = datetime.date(datetime.date.today().year, datetime.date.today().month, datetime.date.today().day)
for i in range(len(data)):
for day_str in data[i]['day']:
#print(data[i]['year'], data[i]['month'], day_str, data[i]['day'][day_str])
# 遍历本日活动
year = int(data[i]['year'])
month = int(data[i]['month'])
day = int(day_str)
event_number = 0
for keyword in event_keyword_list:
end_time = '23:59'
if keyword == 'qdhd':
end_time = '04:59'
for event_name in data[i]['day'][day_str][keyword]:
event_number = event_number + 1
if event_name not in event_cache.keys():
#event_cache[event_name] = {'year': year, 'month': month, 'day': int(day)}
event_cache[event_name] = {
'start_year': year,
'start_month': month,
'start_day': day,
'end_year': year,
'end_month': month,
'end_day': day,
'end_time': end_time,
}
try:
diff = (datetime.date(year, month, day) - today) / datetime.timedelta(1)
except:
continue
if diff == 0 and event_number == 0: #无今日数据
return []
for event_name in list(event_cache.keys()):
is_active = False
for keyword in event_keyword_list:
if event_name in data[i]['day'][day_str][keyword]:
is_active = True
if is_active:
event_cache[event_name]['end_year'] = year
event_cache[event_name]['end_month'] = month
event_cache[event_name]['end_day'] = day
else:
event_list.append({
'title': event_name,
'start': f'{event_cache[event_name]["start_year"]}/{event_cache[event_name]["start_month"]}/{event_cache[event_name]["start_day"]} 05:00',
'end': f'{event_cache[event_name]["end_year"]}/{event_cache[event_name]["end_month"]}/{event_cache[event_name]["end_day"]} {event_cache[event_name]["end_time"]}',
})
event_cache.pop(event_name)
for event_name in list(event_cache.keys()):
event_list.append({
'title': event_name,
'start': f'{event_cache[event_name]["start_year"]}/{event_cache[event_name]["start_month"]}/{event_cache[event_name]["start_day"]} 05:00',
'end': f'{event_cache[event_name]["end_year"]}/{event_cache[event_name]["end_month"]}/{event_cache[event_name]["end_day"]} {event_cache[event_name]["end_time"]}',
})
return event_list
def transform_bilibili_calendar(data):
data = extract_calendar_data(data)
data = transform_calendar_data(data)
return data
'''
fp = open('calendar.js', encoding='utf-8')
data = fp.read()
data = transform_bilibili_calendar(data)
print(data)
'''