forked from data-8/data-8.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_website
124 lines (91 loc) · 3.05 KB
/
update_website
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
#!/usr/bin/env python3
"""
This script is used to quickly make updates to the course webpage, such as
adding a link to lecture slides / video.
Usage:
./update_website [-d | --dry] demo <lec_num>
./update_website [-d | --dry] <command> <lec_num> <url>
Where <command> is one of:
video
slides
demo
Options:
-h --help Show this screen.
-d --dry Prints resulting HTML instead of modifying file
"""
from docopt import docopt
from bs4 import BeautifulSoup
# Name of HTML file
INDEX = 'index.html'
# HTML class of the calendar div
CALENDAR_CLASS = 'calendar'
# Column that contains the lecture information
LECTURE_COL = 2
# Text of the links to add
DEMO_TEXT = '(demo)'
VIDEO_TEXT = '(video)'
SLIDES_TEXT = '(slides)'
DEMO_FORMAT = 'http://data8.haas.berkeley.edu/user-redirect/interact?repo=data8assets&path=lec/su17/lec{:02d}_live.ipynb'
def parse_index() -> BeautifulSoup:
with open(INDEX, 'r') as fp:
return BeautifulSoup(fp, 'html5lib')
def write_index(soup: BeautifulSoup):
with open(INDEX, 'w') as fp:
fp.write(soup.prettify())
def get_calendar(soup: BeautifulSoup) -> [BeautifulSoup]:
return soup.find('div', 'calendar').table.find_all('tr')
def make_link(soup: BeautifulSoup, text: str, url: str) -> BeautifulSoup:
link = soup.new_tag('a', href=url)
link.string = text
return link
def lec_num_to_index(lec_num):
"""
Since every Friday has 2 lectures, we need to decrement the lec_num by 1
each week.
And then I add three since July 4 was a holiday and the midterm technically
stands in place of 2 lectures.
"""
lec_num += 3
return lec_num - lec_num // 6
def _add_link(soup: BeautifulSoup,
lec_num: int,
url: str,
text: str) -> BeautifulSoup:
calendar = get_calendar(soup)
cell = calendar[lec_num_to_index(lec_num)].select('td')[LECTURE_COL]
link = make_link(soup, text, url)
cell.append('\n')
cell.append(link)
return soup
def add_video_link(soup: BeautifulSoup,
lec_num: int,
url: str) -> BeautifulSoup:
return _add_link(soup, lec_num, url, VIDEO_TEXT)
def add_slides_link(soup: BeautifulSoup,
lec_num: int,
url: str) -> BeautifulSoup:
return _add_link(soup, lec_num, url, SLIDES_TEXT)
def add_demo_link(soup: BeautifulSoup,
lec_num: int,
url: 'unused') -> BeautifulSoup:
url = DEMO_FORMAT.format(lec_num)
return _add_link(soup, lec_num, url, DEMO_TEXT)
ARG_TO_COMMAND = {
'video': add_video_link,
'slides': add_slides_link,
'demo': add_demo_link,
}
if __name__ == '__main__':
arguments = docopt(__doc__)
soup = parse_index()
if arguments['demo']:
command = ARG_TO_COMMAND['demo']
else:
command = ARG_TO_COMMAND[arguments['<command>']]
lec_num = int(arguments['<lec_num>'])
url = arguments['<url>']
result = command(soup, lec_num, url)
if arguments['--dry']:
print(soup.prettify())
else:
write_index(soup)