-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.py
161 lines (131 loc) · 4.4 KB
/
script.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
from pathlib import Path
from os import system
from os.path import exists
import re
import csv
import requests
from bs4 import BeautifulSoup
CSV_PATH = "biglist.csv"
ANKI_MEDIA = ".local/share/Anki2/User 1/collection.media/"
class Note:
def __init__(self, *args):
args = args[0]
self.headword = args[0]
self.definition = args[1]
self.example = args[2]
self.video_url = args[3]
self.video_title = args[4]
self.url = args[5]
self.tags = [normalize_tag(x) for x in args[6]]
def __str__(self):
tag_str = " ".join(self.tags)
joined = ";".join(
[
normalize_csv(self.headword),
normalize_csv(self.definition),
normalize_csv(self.example),
f"[sound:{video_filename(self.video_url)}]",
normalize_csv(self.video_url),
normalize_csv(self.video_title),
normalize_csv(self.url),
normalize_csv(tag_str),
]
)
return joined
def get_page(url):
html = requests.get(url).text
page = BeautifulSoup(html, "lxml")
return page
def get_definitions(url, tags):
page = get_page(url)
notes = []
headings = [page.find("h1")] + page.find_all("h2")
if not page.find_all(itemprop="video"):
return notes
for heading in headings:
headword = heading.text
tags += [x.text for x in heading.find_next_siblings("span")]
first_p = heading.find_next_sibling("p")
def_string = re.findall("</b> (.*?)<br/>", str(first_p))
if def_string:
definition = def_string[0]
else:
definition = ""
italics = first_p.find("i")
if italics is not None:
example = italics.text
else:
example = ""
video_div = first_p.find_next(itemprop="video")
video_url = video_div.find(itemprop="contentURL")["content"]
video_title = re.findall("(<i>.*?) <br/>", str(video_div))[0]
notes.append(
Note([headword, definition, example, video_url, video_title, url, tags])
)
return notes
def normalize_tag(string):
return string.replace(" ", "_")
def normalize_csv(string):
doubled_quotes = string.replace('"', '""')
return f'"{doubled_quotes}"'
def video_filename(url):
end = re.findall("signbsl.com/(.*)", url)[0]
return end.replace("/", "_")
def frequency(word):
word_file = "frequency.txt"
reg = re.compile(f"^(\\d+) {word}$", re.MULTILINE)
with open(word_file, "r") as file:
filetext = file.read()
results = reg.findall(filetext)
if results:
return int(results[0])
return 10000
def word_list():
page = get_page("https://www.signbsl.com/gcse-vocabulary")
notes = []
for category in page.find_all("h3"):
pages = [
"https://signbsl.com" + x["href"]
for x in category.find_parent().find_all("a")
]
for page in pages:
print("Contacting ", page)
notes += get_definitions(page, [category.text])
write_csv(CSV_PATH, notes)
def write_csv(filename, notes):
notes.sort(key=lambda note: frequency(note.headword))
with open(filename, "w") as file:
file.writelines([str(note) + "\n" for note in notes])
def convert_video(url):
temp = Path("/tmp/")
filename = video_filename(url)
dest = Path.home() / Path(ANKI_MEDIA)
if exists(dest / filename):
print("Already converted", dest / filename)
return
response = requests.get(url)
with open(f"{temp / filename}", mode="wb") as file:
file.write(response.content)
command = (
f'ffmpeg -i "{temp / filename}" -vcodec libx265 -crf 32 "{dest / filename}"'
)
system(command)
def download_videos(csv_path):
notes = read_csv(csv_path)
for note in notes:
convert_video(note.video_url)
def read_csv(path):
notes = []
with open(path) as file:
reader = csv.reader(file, delimiter=";")
for row in reader:
tags = row[7].split(" ")
# cut out the fourth item (see Note __str__ function)
row = row[0:3] + row[4:7] + [tags]
notes.append(Note(row))
return notes
def add_signs(signs, tags, output):
notes = []
for sign in signs:
notes += get_definitions("https://www.signbsl.com/sign/" + sign, tags)
write_csv(output, notes)