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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"id": "169c0692-3e33-47f6-9658-4a7364f59bda",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import re\n",
|
11 |
+
"import json\n",
|
12 |
+
"import pickle\n",
|
13 |
+
"from pathlib import Path\n",
|
14 |
+
"from collections import OrderedDict, defaultdict\n",
|
15 |
+
"from tqdm import tqdm\n",
|
16 |
+
"from utils_sel import *\n",
|
17 |
+
"from bs4 import BeautifulSoup, NavigableString, Tag, Comment\n",
|
18 |
+
"from selenium.webdriver.common.by import By\n",
|
19 |
+
"from selenium.webdriver.support.ui import WebDriverWait\n",
|
20 |
+
"from selenium.webdriver.support import expected_conditions as EC\n",
|
21 |
+
"from selenium.common.exceptions import NoSuchElementException"
|
22 |
+
]
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"cell_type": "markdown",
|
26 |
+
"id": "90220fd2-e0d8-43db-8f8f-d9da53a58914",
|
27 |
+
"metadata": {},
|
28 |
+
"source": [
|
29 |
+
"## Helper Funcs for this site"
|
30 |
+
]
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"cell_type": "code",
|
34 |
+
"execution_count": null,
|
35 |
+
"id": "d930923b-a101-41fe-8b71-b053efb96279",
|
36 |
+
"metadata": {},
|
37 |
+
"outputs": [],
|
38 |
+
"source": [
|
39 |
+
"def all_strip(finder, css_selector, by_arg=By.CSS_SELECTOR):\n",
|
40 |
+
" \"\"\"Returns a list of stripped texts found by the selector\"\"\"\n",
|
41 |
+
" return [elem.text.strip() for elem in finder.find_elements(by_arg, css_selector)]\n",
|
42 |
+
"\n",
|
43 |
+
"def get_category_num(driver):\n",
|
44 |
+
" \"\"\"In default context\"\"\"\n",
|
45 |
+
" category = ' - '.join(all_strip(driver, 'li.breadcrumb-item'))\n",
|
46 |
+
" if len(category) == 0:\n",
|
47 |
+
" driver.switch_to.default_content()\n",
|
48 |
+
" driver.implicitly_wait(2)\n",
|
49 |
+
" category = ' - '.join(all_strip(driver, 'li.breadcrumb-item'))\n",
|
50 |
+
" question_num = int(re.search(r'/(\\d+)(?=\\?|$)', driver.current_url).group(1))\n",
|
51 |
+
" return category, question_num\n",
|
52 |
+
"\n",
|
53 |
+
"def replace_em(elem, txt):\n",
|
54 |
+
" for em in elem.find_elements(By.CSS_SELECTOR, \"em, i\"):\n",
|
55 |
+
" for emtxt in em.text.split('\\n'):\n",
|
56 |
+
" if len(emtxt) <= 28:\n",
|
57 |
+
" continue\n",
|
58 |
+
" txt = txt.replace(emtxt, \"\")\n",
|
59 |
+
" return txt.strip()\n",
|
60 |
+
"\n",
|
61 |
+
"def no_em_text(elem):\n",
|
62 |
+
" return replace_em(elem, elem.text)\n",
|
63 |
+
"\n",
|
64 |
+
"def get_asblocks(main):\n",
|
65 |
+
" bods = main.find_elements(By.CSS_SELECTOR, 'div.eis-spbody')\n",
|
66 |
+
" return [b for b in bods if len(b.find_elements(By.CSS_SELECTOR, '.asblock'))==1\n",
|
67 |
+
" and len(b.find_elements(By.CSS_SELECTOR, 'div.eis-spbody'))==0] # nested blocks\n",
|
68 |
+
" \n",
|
69 |
+
"def get_non_asblocks(main):\n",
|
70 |
+
" bods = main.find_elements(By.CSS_SELECTOR, 'div.eis-spbody')\n",
|
71 |
+
" return [b for b in bods if len(b.find_elements(By.CSS_SELECTOR, '.asblock'))==0]\n",
|
72 |
+
"\n",
|
73 |
+
"def top_context(main):\n",
|
74 |
+
" bods = [no_em_text(b) for b in get_non_asblocks(main)]\n",
|
75 |
+
" return '\\n\\n'.join(bods).strip()\n",
|
76 |
+
"\n",
|
77 |
+
"def question_spbody(spbody):\n",
|
78 |
+
" quests = spbody.find_elements(By.CSS_SELECTOR, '.asblock')\n",
|
79 |
+
" assert len(quests) == 1\n",
|
80 |
+
" quest = quests[0]\n",
|
81 |
+
" txt = spbody.text\n",
|
82 |
+
" txt = txt.replace(quest.text, \"\")\n",
|
83 |
+
" return replace_em(spbody, txt)\n",
|
84 |
+
"\n",
|
85 |
+
"def goto_questions(driver):\n",
|
86 |
+
" \"\"\"Go from default context to questions iframe\"\"\"\n",
|
87 |
+
" WebDriverWait(driver, 10).until(\n",
|
88 |
+
" EC.frame_to_be_available_and_switch_to_it(0)\n",
|
89 |
+
" )\n",
|
90 |
+
" # mains = driver.find_elements(By.CSS_SELECTOR, 'div.eis-spbody')\n",
|
91 |
+
" # html = get_html_with_images(driver, driver.find_element(By.CSS_SELECTOR, 'div.ylesanne'))\n",
|
92 |
+
" #question = '\\n'.join([no_em_text(elem) for elem in main.find_elements(By.XPATH, 'p')]).strip()\n",
|
93 |
+
" # question = main.find_element(By.XPATH, 'p').text.strip() \n",
|
94 |
+
" main = driver.find_element(By.CSS_SELECTOR, 'div.ylesanne')\n",
|
95 |
+
" html = get_html(driver, main)\n",
|
96 |
+
" question = top_context(main)\n",
|
97 |
+
" return main, html, question\n",
|
98 |
+
"\n",
|
99 |
+
"def get_func_quest(spbody):\n",
|
100 |
+
" \"\"\"On the question context\"\"\"\n",
|
101 |
+
" if len(spbody.find_elements(By.CSS_SELECTOR, \"div.sortables, input[type='text']\")) > 0:\n",
|
102 |
+
" return None, None\n",
|
103 |
+
" func_type = None\n",
|
104 |
+
" main = spbody.find_element(By.CSS_SELECTOR, '.asblock')\n",
|
105 |
+
" drags = main.find_elements(By.CSS_SELECTOR, 'div.draggables')\n",
|
106 |
+
" radios = main.find_elements(By.CSS_SELECTOR, \"input[type='radio']\")\n",
|
107 |
+
" checkboxes = main.find_elements(By.CSS_SELECTOR, \"input[type='checkbox']\")\n",
|
108 |
+
" if len(radios) > 0:\n",
|
109 |
+
" func_type = radio_answer\n",
|
110 |
+
" if len(trs := main.find_elements(By.CSS_SELECTOR, 'tr')) > 0:\n",
|
111 |
+
" func_type = radio_answer_tr\n",
|
112 |
+
" elif len(drags) > 0 and len(main.find_elements(By.CSS_SELECTOR, '.droppable-gap')) == 0:\n",
|
113 |
+
" func_type = None # drag_answer, There are many-to-one also\n",
|
114 |
+
" elif len(checkboxes) > 0:\n",
|
115 |
+
" func_type = checkbox\n",
|
116 |
+
" if len(main.find_elements(By.CSS_SELECTOR, 'th')) > 0:\n",
|
117 |
+
" func_type = checkbox_tr # checkbox_tr multiple\n",
|
118 |
+
" return func_type, question_spbody(spbody)\n",
|
119 |
+
"\n",
|
120 |
+
"def get_coords(d_attribute):\n",
|
121 |
+
" \"\"\"Gets draggable answer's coords\"\"\"\n",
|
122 |
+
" pattern = r\"M(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?)L(\\d+(?:\\.\\d+)?),(\\d+(?:\\.\\d+)?)\"\n",
|
123 |
+
" match = re.match(pattern, d_attribute)\n",
|
124 |
+
" start_x, start_y, end_x, end_y = map(float, match.groups())\n",
|
125 |
+
" assert start_x == 0 and end_x == 100\n",
|
126 |
+
" return start_y, end_y\n",
|
127 |
+
"\n",
|
128 |
+
"def drag_answer(main):\n",
|
129 |
+
" \"\"\"From the main answer\"\"\"\n",
|
130 |
+
" drags = main.find_elements(By.CSS_SELECTOR, 'div.draggables')\n",
|
131 |
+
" assert len(drags) > 0\n",
|
132 |
+
" opts = all_strip(drags[1], 'div.draggable')\n",
|
133 |
+
" sub_quests = all_strip(drags[0], 'div.draggable')\n",
|
134 |
+
" matches = [get_coords(elem.get_attribute('d')) for elem in main.find_elements(By.TAG_NAME, 'path')]\n",
|
135 |
+
" assert len(matches) > 0\n",
|
136 |
+
" start_coords = [start for start, _ in matches]\n",
|
137 |
+
" assert sorted(start_coords) == start_coords\n",
|
138 |
+
" assert len(start_coords) == len(set(start_coords))\n",
|
139 |
+
" sorted_matches = sorted((int(start), int(end)) for start, end in matches)\n",
|
140 |
+
" unique_ends = sorted(set(end for _, end in sorted_matches))\n",
|
141 |
+
" end_to_index = {end: i + 1 for i, end in enumerate(unique_ends)}\n",
|
142 |
+
" answers = [str(end_to_index[end]) for _, end in sorted_matches]\n",
|
143 |
+
" return sub_quests, opts, answers\n",
|
144 |
+
"\n",
|
145 |
+
"def radio_answer(main):\n",
|
146 |
+
" radios = main.find_elements(By.CSS_SELECTOR, \"input[type='radio']\")\n",
|
147 |
+
" assert len(radios) > 0\n",
|
148 |
+
" opts = all_strip(main, \"label\")\n",
|
149 |
+
" if len(opts) == 0 or len(opts) != len(radios) or any(o=='' for o in opts): # checkbox in table\n",
|
150 |
+
" return [], [], []\n",
|
151 |
+
" answers = [str([radio.get_property('checked') for radio in radios].index(True) + 1)]\n",
|
152 |
+
" return [''], opts, answers\n",
|
153 |
+
"\n",
|
154 |
+
"def remove_leading_number(txt):\n",
|
155 |
+
" return re.sub(r'^\\d+\\.\\s*', '', txt)\n",
|
156 |
+
"\n",
|
157 |
+
"def radio_answer_tr(main):\n",
|
158 |
+
" trs = main.find_elements(By.CSS_SELECTOR, 'tr')\n",
|
159 |
+
" assert len(trs) > 0\n",
|
160 |
+
" # sub_quests = [e.find_elements(By.CSS_SELECTOR, 'td')[1].text.strip() for e in trs[1:]]\n",
|
161 |
+
" sub_quests = [remove_leading_number(e.text.strip()) for e in trs[1:]]\n",
|
162 |
+
" radss = [e.find_elements(By.CSS_SELECTOR, \"input[type='radio']\") for e in trs[1:]]\n",
|
163 |
+
" opts_elems = trs[0].find_elements(By.CSS_SELECTOR, 'th')[-len(radss[0]):]\n",
|
164 |
+
" opts = [e.text.strip() for e in opts_elems]\n",
|
165 |
+
" if len(opts) == 0 or len(opts) != len(radss[0]) or any(o=='' for o in opts): # checkbox in table\n",
|
166 |
+
" return [], [], []\n",
|
167 |
+
" answers = [str([rad.get_property('checked') for rad in rads].index(True) + 1) for rads in radss]\n",
|
168 |
+
" return sub_quests, opts, answers\n",
|
169 |
+
"\n",
|
170 |
+
"def checkbox(main):\n",
|
171 |
+
" checks = main.find_elements(By.CSS_SELECTOR, \"input[type='checkbox']\")\n",
|
172 |
+
" assert len(checks) > 0\n",
|
173 |
+
" opts = all_strip(main, \"span[uitype='checkbox']\")\n",
|
174 |
+
" if len(opts) == 0:\n",
|
175 |
+
" opts = [c.find_element(By.XPATH, \"..\").text.strip() for c in checks]\n",
|
176 |
+
" if any(len(o)==0 for o in opts):\n",
|
177 |
+
" opts = [c.find_element(By.XPATH, \"../..\").text.strip() for c in checks]\n",
|
178 |
+
" if any(len(o) == 0 for o in opts):\n",
|
179 |
+
" opts = [o for o in all_strip(main, \"td\") if o]\n",
|
180 |
+
" if any(len(o) == 0 for o in opts) or len(opts) != len(checks): # maybe in another td\n",
|
181 |
+
" return [], [], []\n",
|
182 |
+
" answers = [','.join([str(i+1) for i, c in enumerate(checks) if c.get_property('checked')])]\n",
|
183 |
+
" return [''], opts, answers\n",
|
184 |
+
"\n",
|
185 |
+
"def checkbox_tr(main):\n",
|
186 |
+
" trs = main.find_elements(By.CSS_SELECTOR, 'tr')\n",
|
187 |
+
" assert len(trs) > 0\n",
|
188 |
+
" # sub_quests = [e.find_elements(By.CSS_SELECTOR, 'td')[1].text.strip() for e in trs[1:]]\n",
|
189 |
+
" sub_quests = [remove_leading_number(e.text.strip()) for e in trs[1:]]\n",
|
190 |
+
" radss = [e.find_elements(By.CSS_SELECTOR, \"input[type='checkbox']\") for e in trs[1:]]\n",
|
191 |
+
" opts_elems = trs[0].find_elements(By.CSS_SELECTOR, 'th')[-len(radss[0]):]\n",
|
192 |
+
" opts = [e.text.strip() for e in opts_elems]\n",
|
193 |
+
" if len(opts) == 0 or len(opts) != len(radss[0]) or any(o=='' for o in opts): # checkbox in table\n",
|
194 |
+
" return [], [], []\n",
|
195 |
+
" answers = [','.join([str(i+1) for i, c in enumerate(rads) if c.get_property('checked')]) for rads in radss]\n",
|
196 |
+
" return sub_quests, opts, answers\n",
|
197 |
+
"\n",
|
198 |
+
"def goto_answers(driver):\n",
|
199 |
+
" \"\"\"Go from default context to answers iframe\"\"\"\n",
|
200 |
+
" submit_button = driver.find_element(By.ID, 'salvesta_ja_kinnita')\n",
|
201 |
+
" try:\n",
|
202 |
+
" submit_button.click()\n",
|
203 |
+
" except:\n",
|
204 |
+
" pass\n",
|
205 |
+
" WebDriverWait(driver, 10).until(\n",
|
206 |
+
" EC.frame_to_be_available_and_switch_to_it(1)\n",
|
207 |
+
" )\n",
|
208 |
+
" show_button = driver.find_elements(By.ID, 'show_correct')\n",
|
209 |
+
" if len(show_button) == 0:\n",
|
210 |
+
" return None, None, None\n",
|
211 |
+
" show_button[0].click()\n",
|
212 |
+
" driver.switch_to.default_content()\n",
|
213 |
+
" popup = WebDriverWait(driver, 10).until(\n",
|
214 |
+
" EC.presence_of_element_located((By.ID, 'correct'))\n",
|
215 |
+
" )\n",
|
216 |
+
" popup_iframe = popup.find_element(By.TAG_NAME, 'iframe')\n",
|
217 |
+
" driver.switch_to.frame(popup_iframe)\n",
|
218 |
+
" main = driver.find_element(By.CSS_SELECTOR, 'div.ylesanne')\n",
|
219 |
+
" html = get_html(driver, main)\n",
|
220 |
+
" image = main.screenshot_as_png\n",
|
221 |
+
" return main, html, image\n",
|
222 |
+
"\n",
|
223 |
+
"def goto_next_page(driver):\n",
|
224 |
+
" driver.switch_to.default_content()\n",
|
225 |
+
" close = driver.find_elements(By.CSS_SELECTOR, 'button.close[data-dismiss=\"modal\"]')\n",
|
226 |
+
" if close:\n",
|
227 |
+
" close[0].click()\n",
|
228 |
+
" url = driver.current_url\n",
|
229 |
+
" next_button = driver.find_element(By.ID, 'järgmine')\n",
|
230 |
+
" next_button.click()\n",
|
231 |
+
" WebDriverWait(driver, 10).until(EC.url_changes(url))"
|
232 |
+
]
|
233 |
+
},
|
234 |
+
{
|
235 |
+
"cell_type": "markdown",
|
236 |
+
"id": "9627517a-2316-474b-8a86-d9bf1f128fa4",
|
237 |
+
"metadata": {},
|
238 |
+
"source": [
|
239 |
+
"## Need to solve captcha to initialize"
|
240 |
+
]
|
241 |
+
},
|
242 |
+
{
|
243 |
+
"cell_type": "code",
|
244 |
+
"execution_count": null,
|
245 |
+
"id": "55e75505-b0dc-4046-8bbc-f2bd6de9e17c",
|
246 |
+
"metadata": {},
|
247 |
+
"outputs": [],
|
248 |
+
"source": [
|
249 |
+
"HOME_URL = \"https://eis.ekk.edu.ee/eis/lahendamine\"\n",
|
250 |
+
"SAVE_PATH = Path('./estonian_varied_exams/')"
|
251 |
+
]
|
252 |
+
},
|
253 |
+
{
|
254 |
+
"cell_type": "code",
|
255 |
+
"execution_count": null,
|
256 |
+
"id": "176239e7-58c4-46e1-b837-5f167eaf657a",
|
257 |
+
"metadata": {},
|
258 |
+
"outputs": [],
|
259 |
+
"source": [
|
260 |
+
"driver = get_driver(production=False)\n",
|
261 |
+
"driver.get(HOME_URL) # Needs Manual Recaptcha Challenge, then click the accept cookies"
|
262 |
+
]
|
263 |
+
},
|
264 |
+
{
|
265 |
+
"cell_type": "code",
|
266 |
+
"execution_count": null,
|
267 |
+
"id": "50044044-504c-4095-a7db-7d873ba85943",
|
268 |
+
"metadata": {},
|
269 |
+
"outputs": [],
|
270 |
+
"source": [
|
271 |
+
"first_link = driver.find_element(By.CSS_SELECTOR, 'a.LISTPOST')\n",
|
272 |
+
"first_link.click()"
|
273 |
+
]
|
274 |
+
},
|
275 |
+
{
|
276 |
+
"cell_type": "code",
|
277 |
+
"execution_count": null,
|
278 |
+
"id": "1f0de212-2e8c-4f2d-a512-06e9e818a2ad",
|
279 |
+
"metadata": {},
|
280 |
+
"outputs": [],
|
281 |
+
"source": [
|
282 |
+
"data_list = []\n",
|
283 |
+
"raw_data = OrderedDict()"
|
284 |
+
]
|
285 |
+
},
|
286 |
+
{
|
287 |
+
"cell_type": "markdown",
|
288 |
+
"id": "4aecce6a-0da7-4b2c-846d-618794e07f3d",
|
289 |
+
"metadata": {},
|
290 |
+
"source": [
|
291 |
+
"## Main-Loop"
|
292 |
+
]
|
293 |
+
},
|
294 |
+
{
|
295 |
+
"cell_type": "code",
|
296 |
+
"execution_count": null,
|
297 |
+
"id": "5547abf2-27c6-47a4-b830-a80a97900ed7",
|
298 |
+
"metadata": {
|
299 |
+
"scrolled": true
|
300 |
+
},
|
301 |
+
"outputs": [],
|
302 |
+
"source": [
|
303 |
+
"for i in range(871): # 871\n",
|
304 |
+
" category, num = get_category_num(driver)\n",
|
305 |
+
" print(f'------------{num:05}------------')\n",
|
306 |
+
" main_q, html_q, question = goto_questions(driver)\n",
|
307 |
+
" # if any(len(b.find_elements(By.CSS_SELECTOR, 'img, svg')) > 0 for b in get_non_asblocks(main_q)): # There is img on draggable\n",
|
308 |
+
" if len(main_q.find_elements(By.CSS_SELECTOR, 'img, svg, .eis-mediaitem')) > 0 or len(main_q.find_elements(By.XPATH, \"//iframe[contains(@src, 'youtube.com/embed/')]\")) > 0:\n",
|
309 |
+
" raw_data[num] = {'html_q': html_q}\n",
|
310 |
+
" goto_next_page(driver)\n",
|
311 |
+
" continue\n",
|
312 |
+
" funcs_qs = [get_func_quest(b) for b in get_asblocks(main_q)]\n",
|
313 |
+
" driver.switch_to.default_content()\n",
|
314 |
+
" main, html, image = goto_answers(driver)\n",
|
315 |
+
" if main is None:\n",
|
316 |
+
" goto_next_page(driver)\n",
|
317 |
+
" continue\n",
|
318 |
+
" raw_data[num] = {'html_q': html_q, 'html': html, 'image': image}\n",
|
319 |
+
" for (func_type, que), blocks in zip(funcs_qs, get_asblocks(main), strict=True):\n",
|
320 |
+
" que = f'{question}\\n\\n{que}'.strip()\n",
|
321 |
+
" if func_type:\n",
|
322 |
+
" sub_quests, opts, answers = func_type(blocks)\n",
|
323 |
+
" for sub_quest, answer in zip(sub_quests, answers, strict=True):\n",
|
324 |
+
" data = {\n",
|
325 |
+
" \"category_original_lang\": category,\n",
|
326 |
+
" \"original_question_num\": num,\n",
|
327 |
+
" \"question\": (f'{que}\\n\\n{sub_quest}').strip(),\n",
|
328 |
+
" \"options\": opts,\n",
|
329 |
+
" \"answer\": answer\n",
|
330 |
+
" }\n",
|
331 |
+
" data_list.append(data)\n",
|
332 |
+
" print(data, end='\\n----\\n')\n",
|
333 |
+
" goto_next_page(driver)"
|
334 |
+
]
|
335 |
+
},
|
336 |
+
{
|
337 |
+
"cell_type": "code",
|
338 |
+
"execution_count": null,
|
339 |
+
"id": "5ba1209a-d6f4-4b82-8f07-61c431301271",
|
340 |
+
"metadata": {},
|
341 |
+
"outputs": [],
|
342 |
+
"source": [
|
343 |
+
"sd_pkl_path = SAVE_PATH / 'raw_and_processed.pkl'\n",
|
344 |
+
"if sd_pkl_path.exists():\n",
|
345 |
+
" with sd_pkl_path.open('rb') as file:\n",
|
346 |
+
" raw_data, data_list = pickle.load(file)\n",
|
347 |
+
"else:\n",
|
348 |
+
" with sd_pkl_path.open('wb') as file:\n",
|
349 |
+
" pickle.dump((raw_data, data_list), file)"
|
350 |
+
]
|
351 |
+
},
|
352 |
+
{
|
353 |
+
"cell_type": "code",
|
354 |
+
"execution_count": null,
|
355 |
+
"id": "f80085d6-f37c-4a57-95bf-1a2e1e8703e3",
|
356 |
+
"metadata": {},
|
357 |
+
"outputs": [],
|
358 |
+
"source": [
|
359 |
+
"last = None\n",
|
360 |
+
"imgs = []\n",
|
361 |
+
"for data in data_list:\n",
|
362 |
+
" if (n:=data['original_question_num']) != last:\n",
|
363 |
+
" last = n\n",
|
364 |
+
" imgs.append(raw_data[n]['image'])\n",
|
365 |
+
"create_pdf_from_images(imgs, SAVE_PATH / 'Eksamite.pdf')"
|
366 |
+
]
|
367 |
+
},
|
368 |
+
{
|
369 |
+
"cell_type": "markdown",
|
370 |
+
"id": "213fb422-c362-48c5-8b27-827cdd348112",
|
371 |
+
"metadata": {},
|
372 |
+
"source": [
|
373 |
+
"## Saving JSON"
|
374 |
+
]
|
375 |
+
},
|
376 |
+
{
|
377 |
+
"cell_type": "code",
|
378 |
+
"execution_count": null,
|
379 |
+
"id": "2b20fe9f-c475-4215-be54-7cf594cb36c2",
|
380 |
+
"metadata": {},
|
381 |
+
"outputs": [],
|
382 |
+
"source": [
|
383 |
+
"for data in data_list:\n",
|
384 |
+
" data[\"language\"] = \"et\"\n",
|
385 |
+
" data[\"country\"] = \"Estonia\"\n",
|
386 |
+
" data[\"source\"] = \"https://eis.ekk.edu.ee/eis/lahendamine\"\n",
|
387 |
+
" data[\"license\"] = \"open\"\n",
|
388 |
+
" data[\"level\"] = \"High-School\"\n",
|
389 |
+
" data[\"category_en\"] = \"Baseline Tests\"\n",
|
390 |
+
" data['file_name'] = 'Eksamite.pdf'\n",
|
391 |
+
" data['question'] = remove_leading_number(data['question'])\n",
|
392 |
+
"\n",
|
393 |
+
"data_list_clean = []\n",
|
394 |
+
"question_set = set()\n",
|
395 |
+
"for data in data_list:\n",
|
396 |
+
" if data['question'] and data['question'] not in question_set:\n",
|
397 |
+
" data_list_clean.append(data)\n",
|
398 |
+
" question_set.add(data['question'])\n",
|
399 |
+
"\n",
|
400 |
+
"with (SAVE_PATH / 'Eksamite.json').open('w') as file:\n",
|
401 |
+
" json.dump(data_list_clean, file)"
|
402 |
+
]
|
403 |
+
},
|
404 |
+
{
|
405 |
+
"cell_type": "code",
|
406 |
+
"execution_count": null,
|
407 |
+
"id": "996246e2-ab5d-4a72-8659-c24d8498e628",
|
408 |
+
"metadata": {},
|
409 |
+
"outputs": [],
|
410 |
+
"source": [
|
411 |
+
"# func_type, opts, sub_quests = get_opts(main_q)\n",
|
412 |
+
"if func_type:\n",
|
413 |
+
" data[\"answer\"] = func_type(main.find_element(By.CSS_SELECTOR, '.asblock'))\n",
|
414 |
+
" if len(data[\"answer\"]) < len(data[\"sub_quests\"]):\n",
|
415 |
+
" data[\"bad\"] = True\n",
|
416 |
+
"for k in [\"category_original_lang\", \"original_question_num\", \"question\", \"sub_quests\", \"options\", \"answer\", \"bad\"]:\n",
|
417 |
+
" print(f'{k}: {data.get(k)}')\n",
|
418 |
+
" "
|
419 |
+
]
|
420 |
+
},
|
421 |
+
{
|
422 |
+
"cell_type": "code",
|
423 |
+
"execution_count": null,
|
424 |
+
"id": "0f06093f-bd8d-4c5a-9c47-fb1b8ccabb96",
|
425 |
+
"metadata": {
|
426 |
+
"scrolled": true
|
427 |
+
},
|
428 |
+
"outputs": [],
|
429 |
+
"source": [
|
430 |
+
"# iframe = WebDriverWait(driver, 10).until(\n",
|
431 |
+
"# EC.presence_of_element_located((By.CSS_SELECTOR, \"iframe.ylesanne\"))\n",
|
432 |
+
"# )\n",
|
433 |
+
"# driver.switch_to.frame(iframe)\n",
|
434 |
+
"WebDriverWait(driver, 10).until(\n",
|
435 |
+
" EC.frame_to_be_available_and_switch_to_it(0)\n",
|
436 |
+
")\n",
|
437 |
+
"main = driver.find_element(By.CSS_SELECTOR, 'div.eis-spbody')\n",
|
438 |
+
"html = get_html_with_images(driver, main)\n",
|
439 |
+
"question = '\\n'.join(all_strip(main, 'p', By.XPATH))"
|
440 |
+
]
|
441 |
+
},
|
442 |
+
{
|
443 |
+
"cell_type": "code",
|
444 |
+
"execution_count": null,
|
445 |
+
"id": "785104aa-9cc6-48b9-bad3-0ca2ed018ee7",
|
446 |
+
"metadata": {},
|
447 |
+
"outputs": [],
|
448 |
+
"source": [
|
449 |
+
"interactive = len(main.find_elements(By.CSS_SELECTOR, 'div.sortables')) > 0\n",
|
450 |
+
"drags = main.find_elements(By.CSS_SELECTOR, 'div.draggables')\n",
|
451 |
+
"radios = main.find_elements(By.CSS_SELECTOR, \"input[type='radio']\")\n",
|
452 |
+
"if len(radios) > 0:\n",
|
453 |
+
" opts = all_strip(main, \"label\")\n",
|
454 |
+
"if len(drags) > 0:\n",
|
455 |
+
" opts = all_strip(drags[1], 'div.draggable')\n",
|
456 |
+
" sub_quests = all_strip(drags[0], 'div.draggable')"
|
457 |
+
]
|
458 |
+
},
|
459 |
+
{
|
460 |
+
"cell_type": "code",
|
461 |
+
"execution_count": null,
|
462 |
+
"id": "f566c0fd-b274-4bcf-8b36-e714977d34ed",
|
463 |
+
"metadata": {},
|
464 |
+
"outputs": [],
|
465 |
+
"source": [
|
466 |
+
"radios = main.find_elements(By.CSS_SELECTOR, \"input[type='radio']\")\n",
|
467 |
+
"if len(radios) > 0:\n",
|
468 |
+
" # opts_abc = [radio.get_attribute('value') for radio in radios]\n",
|
469 |
+
" opts = all_strip(main, \"label\")\n",
|
470 |
+
"opts"
|
471 |
+
]
|
472 |
+
},
|
473 |
+
{
|
474 |
+
"cell_type": "code",
|
475 |
+
"execution_count": null,
|
476 |
+
"id": "de372f46-9967-46d9-8bfd-57da831df88c",
|
477 |
+
"metadata": {},
|
478 |
+
"outputs": [],
|
479 |
+
"source": []
|
480 |
+
},
|
481 |
+
{
|
482 |
+
"cell_type": "code",
|
483 |
+
"execution_count": null,
|
484 |
+
"id": "7a035f26-b5a0-4f37-9fb9-ec094ad63f30",
|
485 |
+
"metadata": {
|
486 |
+
"scrolled": true
|
487 |
+
},
|
488 |
+
"outputs": [],
|
489 |
+
"source": [
|
490 |
+
"\n",
|
491 |
+
"radios = main.find_elements(By.CSS_SELECTOR, \"input[type='radio']\")\n",
|
492 |
+
"if len(radios) > 0:\n",
|
493 |
+
" answer = str([radio.get_property('checked') for radio in radios].index(True) + 1)\n",
|
494 |
+
" print(answer)"
|
495 |
+
]
|
496 |
+
},
|
497 |
+
{
|
498 |
+
"cell_type": "code",
|
499 |
+
"execution_count": null,
|
500 |
+
"id": "ab3d7865-f4d0-497e-a43b-a6f0465a9197",
|
501 |
+
"metadata": {},
|
502 |
+
"outputs": [],
|
503 |
+
"source": [
|
504 |
+
"drags = main.find_elements(By.CSS_SELECTOR, 'div.draggables')\n",
|
505 |
+
"sub_questions = all_strip(drags[0], 'div.draggable')\n",
|
506 |
+
"options = all_strip(drags[1], 'div.draggable')"
|
507 |
+
]
|
508 |
+
},
|
509 |
+
{
|
510 |
+
"cell_type": "code",
|
511 |
+
"execution_count": null,
|
512 |
+
"id": "357f274d-33cc-4c5c-aa8e-5035818a9ddc",
|
513 |
+
"metadata": {},
|
514 |
+
"outputs": [],
|
515 |
+
"source": []
|
516 |
+
},
|
517 |
+
{
|
518 |
+
"cell_type": "code",
|
519 |
+
"execution_count": null,
|
520 |
+
"id": "38a5ad4d-62cf-4e1d-b925-db9011e119ea",
|
521 |
+
"metadata": {},
|
522 |
+
"outputs": [],
|
523 |
+
"source": [
|
524 |
+
"\n"
|
525 |
+
]
|
526 |
+
},
|
527 |
+
{
|
528 |
+
"cell_type": "code",
|
529 |
+
"execution_count": null,
|
530 |
+
"id": "d71e4719-4b11-43cd-a2fe-e36d3319e741",
|
531 |
+
"metadata": {},
|
532 |
+
"outputs": [],
|
533 |
+
"source": [
|
534 |
+
"sorted_end, end_mapping"
|
535 |
+
]
|
536 |
+
},
|
537 |
+
{
|
538 |
+
"cell_type": "code",
|
539 |
+
"execution_count": null,
|
540 |
+
"id": "b847a3b1-c825-4d84-974b-7389861a6a64",
|
541 |
+
"metadata": {},
|
542 |
+
"outputs": [],
|
543 |
+
"source": [
|
544 |
+
"\n"
|
545 |
+
]
|
546 |
+
},
|
547 |
+
{
|
548 |
+
"cell_type": "code",
|
549 |
+
"execution_count": null,
|
550 |
+
"id": "72912044-3fde-4ced-9eff-d514c2849d34",
|
551 |
+
"metadata": {},
|
552 |
+
"outputs": [],
|
553 |
+
"source": [
|
554 |
+
"# p p last_one"
|
555 |
+
]
|
556 |
+
},
|
557 |
+
{
|
558 |
+
"cell_type": "code",
|
559 |
+
"execution_count": null,
|
560 |
+
"id": "98e387f2-bbab-408f-92be-18413bcf2c8f",
|
561 |
+
"metadata": {},
|
562 |
+
"outputs": [],
|
563 |
+
"source": [
|
564 |
+
"bytes_to_image(main.screenshot_as_png)"
|
565 |
+
]
|
566 |
+
},
|
567 |
+
{
|
568 |
+
"cell_type": "code",
|
569 |
+
"execution_count": null,
|
570 |
+
"id": "66cbf00e-c52f-4566-953f-09edd6da8549",
|
571 |
+
"metadata": {},
|
572 |
+
"outputs": [],
|
573 |
+
"source": [
|
574 |
+
"bytes_to_image(data['image'])"
|
575 |
+
]
|
576 |
+
},
|
577 |
+
{
|
578 |
+
"cell_type": "code",
|
579 |
+
"execution_count": null,
|
580 |
+
"id": "adb6cf11-7967-4d25-95c0-8897676e520c",
|
581 |
+
"metadata": {},
|
582 |
+
"outputs": [],
|
583 |
+
"source": [
|
584 |
+
"bytes_to_image(main.find_elements(By.CSS_SELECTOR, \"span[uitype='checkbox']\")[0].screenshot_as_png)"
|
585 |
+
]
|
586 |
+
},
|
587 |
+
{
|
588 |
+
"cell_type": "code",
|
589 |
+
"execution_count": null,
|
590 |
+
"id": "aa729929-deee-497f-ba41-0e266135de2f",
|
591 |
+
"metadata": {},
|
592 |
+
"outputs": [],
|
593 |
+
"source": []
|
594 |
+
}
|
595 |
+
],
|
596 |
+
"metadata": {
|
597 |
+
"kernelspec": {
|
598 |
+
"display_name": "Python 3 (ipykernel)",
|
599 |
+
"language": "python",
|
600 |
+
"name": "python3"
|
601 |
+
},
|
602 |
+
"language_info": {
|
603 |
+
"codemirror_mode": {
|
604 |
+
"name": "ipython",
|
605 |
+
"version": 3
|
606 |
+
},
|
607 |
+
"file_extension": ".py",
|
608 |
+
"mimetype": "text/x-python",
|
609 |
+
"name": "python",
|
610 |
+
"nbconvert_exporter": "python",
|
611 |
+
"pygments_lexer": "ipython3",
|
612 |
+
"version": "3.11.7"
|
613 |
+
}
|
614 |
+
},
|
615 |
+
"nbformat": 4,
|
616 |
+
"nbformat_minor": 5
|
617 |
+
}
|
estonian_varied_exams/Eksamite.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
estonian_varied_exams/Eksamite.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a0bb4df2910060922dfc3bd338f203f9dc526cd6abd297a1ff43e2faf61fd4c1
|
3 |
+
size 7100018
|
estonian_varied_exams/raw_and_processed.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:49bec663ff8720ede8d4ca4c6621013c8369d4296fe3886e27fc3308d1bf7aa9
|
3 |
+
size 21363814
|
utils_sel.py
CHANGED
@@ -37,6 +37,15 @@ def click_link(driver, partial_text):
|
|
37 |
link.click()
|
38 |
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
def get_html_with_images(driver, element):
|
41 |
"""Get an string of the innerHTML of the elements with embedded images"""
|
42 |
script = """
|
|
|
37 |
link.click()
|
38 |
|
39 |
|
40 |
+
def get_html(driver, element):
|
41 |
+
"""Get an string of the innerHTML"""
|
42 |
+
script = """
|
43 |
+
var div = arguments[0];
|
44 |
+
return div.innerHTML;
|
45 |
+
"""
|
46 |
+
return driver.execute_script(script, element)
|
47 |
+
|
48 |
+
|
49 |
def get_html_with_images(driver, element):
|
50 |
"""Get an string of the innerHTML of the elements with embedded images"""
|
51 |
script = """
|