This repository has been archived by the owner on Jun 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.py
49 lines (35 loc) · 1.5 KB
/
library.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
import requests
from bs4 import BeautifulSoup
class LibraryError(Exception):
pass
def fetch_opening_info(libraryType, year=None, month=None, day=None):
if year and month:
url = f"https://opac.dl.itc.u-tokyo.ac.jp/opac/calendar/?lang=0&countercd={libraryType}&date={year}-{month:02}"
else:
url = f"https://opac.dl.itc.u-tokyo.ac.jp/opac/calendar/?lang=0&countercd={libraryType}"
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
style = None
calendar_table = soup.find('table', {'class': 'lib_calendar'})
for elem in calendar_table.find_all('td'):
if elem.get_text().strip() == str(day):
outer_style = elem.get('style')
calendar_today_a = elem.find('a')
inner_style = calendar_today_a.get('style')
style = f"{outer_style} {inner_style}"
break
if not style:
raise LibraryError("開館時間の style が見つかりません")
opening_type = None
opening_info = None
remark_table = soup.find('table', {'class': 'remarks'})
for elem in remark_table.find_all('tr'):
td_list = elem.find_all('td')
if td_list[0].get('style') == style:
opening_type = td_list[0].get_text().strip()
opening_info = td_list[1].get_text().strip()
break
if opening_type and opening_info:
return (opening_type, opening_info)
else:
raise LibraryError("開館時間の style に対応する情報が見つかりません")