forked from anilabhadatta/educative.io_scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheducative_scraper.py
1360 lines (1138 loc) · 48.5 KB
/
educative_scraper.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from time import sleep
from selenium.webdriver.common.by import By
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from slugify import slugify
import glob
import zipfile
import json
import sys
import base64
import re
OS_ROOT = os.path.expanduser('~')
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
def load_config():
config_path = os.path.join(OS_ROOT, ".educative")
if ".educative" not in os.listdir(OS_ROOT) or f"config_{selected_config}.json" not in os.listdir(config_path):
raise Exception("No config found, Please create one")
with open(os.path.join(config_path, f"config_{selected_config}.json"), "r") as config_file:
config = json.load(config_file)
if "url_file_path" not in config or "save_path" not in config or "headless" not in config:
raise Exception("Config is corrupted, Please recreate the config")
url_text_file = config["url_file_path"]
save_path = config["save_path"]
headless = config["headless"]
return url_text_file, save_path, headless
def load_chrome_driver(headless):
chrome_path = get_binary_path()
user_data_dir = os.path.join(
OS_ROOT, ".educative", f"User Data_{selected_config}")
options = webdriver.ChromeOptions()
if headless:
options.add_argument('headless')
options.add_argument(f'user-data-dir={user_data_dir}')
options.add_argument('--profile-directory=Default')
options.add_argument("--start-maximized")
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument("--disable-dev-shm-usage")
options.add_argument('--log-level=3')
userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.56 Safari/537.36"
options.add_argument(f'user-agent={userAgent}')
options.binary_location = os.path.join(ROOT_DIR, "Chrome-bin", chrome_path)
driver = webdriver.Remote(
command_executor='http://127.0.0.1:9515', options=options)
driver.set_window_size(1920, 1080)
driver.command_executor._commands["send_command"] = (
"POST", '/session/$sessionId/chromium/send_command')
print("Driver Loaded")
return driver
def create_course_folder(driver, url):
print("Create Course Folder Function")
course_name = get_file_name(driver, True)
create_folder(course_name)
print("Inside Course Folder")
def next_page(driver):
print("Next Page Function")
next_page_selector = "button[class*='outlined-primary m-0']"
if not driver.find_elements(By.CSS_SELECTOR, next_page_selector):
return False
base_js_cmd = f'''document.querySelectorAll("{next_page_selector}")[0]'''
check_next_module = driver.execute_script(
"return " + base_js_cmd + ".innerHTML.slice(0,11);")
if "Next Module" in check_next_module:
return False
driver.execute_script(base_js_cmd + ".click()")
print("Going Next Page")
return True
def open_slides(driver):
print("Finding Slides Function")
svg_label = "svg[aria-label*='view all slides']"
action = ActionChains(driver)
slides = driver.find_elements(
By.CSS_SELECTOR, svg_label)
if slides:
while slides:
action.move_to_element(slides[0]).click().perform()
sleep(1)
print("Slides opened")
slides = driver.find_elements(By.CSS_SELECTOR, svg_label)
sleep(10)
else:
print("No Slides Found")
def get_file_name_standard(driver, course_folder=False):
print("Get File name standard")
meta_script_selector = "script[type='application/ld+json']"
title_selector = "meta[property='og:title']"
metadata_elements = driver.find_elements(
By.CSS_SELECTOR, meta_script_selector)
for meta_element in metadata_elements:
meta_content = json.loads(meta_element.get_attribute('innerHTML'))
if "name" in meta_content:
break
course_name = meta_content['name']
title = driver.find_element(
By.CSS_SELECTOR, title_selector).get_attribute('content')
if course_name in title:
page_name = title[:len(title)-len(course_name)]
else:
page_name = title
return course_name if course_folder else page_name
def get_file_name_from_module(driver, course_folder=False):
print("Get File name module")
title_selector = "meta[property='og:title']"
article_selector = "div[id='handleArticleScroll']"
title_els = driver.find_elements(By.CSS_SELECTOR, title_selector)
article_ele = driver.find_elements(By.CSS_SELECTOR, article_selector)
assert len(
title_els) == 2 and len(article_ele) > 0, "Expected to find two og:title elements."
title = title_els[1].get_attribute('content')
h1_title = article_ele[0].find_element(By.TAG_NAME, 'h1').text
course_name, page_name = title[len(h1_title):], h1_title
return course_name if course_folder else page_name
def replace_filename(str):
numDict = {':': ' ', '?': ' ', '|': ' ', '>': ' ', '<': ' ', '/': ' '}
return numDict[str.group()]
def get_file_name(driver, course_folder=False):
print("Getting File Name")
# is that a standard page or module page?
# TODO: is there a proper/better way to differentiate?
canonical_selector = "link[rel='canonical']"
els = driver.find_elements(By.CSS_SELECTOR, canonical_selector)
if len(els) > 0:
print("> This is a module page")
file_name = get_file_name_from_module(driver, course_folder)
else:
print("> This is a standard page")
try:
file_name = get_file_name_standard(driver, course_folder)
except:
file_name = get_file_name_from_module(driver, course_folder)
file_name = slugify(file_name, replacements=[
['+', 'plus']]).replace("-", " ")
return re.sub(r'[:?|></]', replace_filename, file_name)
def delete_node(driver, node, xpath=False):
print("Node deleted", node)
if xpath:
driver.execute_script(f"""
var element = document.evaluate("{node}", document, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
if (element)
element.parentNode.removeChild(element);
""")
else:
driver.execute_script(f"""
var element = document.querySelectorAll("{node}");
if (element.length > 0)
element[element.length - 1].parentNode.removeChild(element[element.length - 1]);
""")
sleep(1)
def remove_nav_tags(driver):
print("Removing Nav tags from page")
nav_node = f"div[class*='ed-grid'] > nav"
ask_a_question_xpath = "//*[@id='ReaderPreferencesButton']/parent::div"
delete_node(driver, nav_node)
delete_node(driver, nav_node)
# delete_node(driver, ask_a_question_xpath, True)
sleep(2)
def get_current_height(driver):
return driver.execute_script('return document.body.parentNode.scrollHeight')
def create_html_with_image(file_name, base_64_png, quiz_html):
with open(file_name + ".html", "w+", encoding="utf-8") as fh:
fh.write(f'''
<!DOCTYPE html>
<html lang="en">
<head>
<title>{file_name}</title>
</head>
<body style="zoom: 80%">
<div style="text-align: center">
<img style="display: block;margin-left: auto; margin-right: auto;" src="data:image/png;base64,{base_64_png}" alt="">
{quiz_html}
</div>
</body>
</html>
''')
sleep(1)
def create_html_with_singleFile(file_name, page_content, quiz_html):
print("Creating HTML File")
with open(file_name + ".html", "w+", encoding="utf-8") as fh:
fh.write(f'''
{page_content}
{quiz_html}
''')
sleep(1)
print("HTML File Created")
def send_command(driver, cmd, params={}):
resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
url = driver.command_executor._url + resource
body = json.dumps({'cmd': cmd, 'params': params})
response = driver.command_executor._request('POST', url, body)
return response.get('value')
def screenshot_as_cdp(driver, ele_to_screenshot):
sleep(1)
size, location = ele_to_screenshot.size, ele_to_screenshot.location
width, height = size['width'], size['height']
x, y = location['x'], location['y']
params = {
"format": "png",
"captureBeyondViewport": True,
"clip": {
"width": width,
"height": height,
"x": x,
"y": y,
"scale": 1
}
}
screenshot = send_command(driver, "Page.captureScreenshot", params)
return screenshot['data']
def take_screenshot(driver, file_name, quiz_html):
print("Take Screenshot Function")
article_page_selector = "//*[@id='handleArticleScroll']/div/div/div/div"
project_page_selector = "div[class*='Page']"
general_page_selector = "div[class*='PageContent']"
ele_to_screenshot = driver.find_elements(
By.CSS_SELECTOR, general_page_selector) or driver.find_elements(
By.XPATH, article_page_selector)
base_64_png = screenshot_as_cdp(driver, ele_to_screenshot[0])
sleep(2)
create_html_with_image(file_name, base_64_png, quiz_html)
print("Screenshot taken and HTML File generated")
def fix_all_svg_tags_inside_object_tags(driver):
print("Fixing SVG Tags inside Object Tags")
driver.execute_script('''
iframes = document.querySelectorAll("object")
for (var i = 0; i < iframes.length; i++) {
try{
svg_element = iframes[i].contentDocument.documentElement;
iframes[i].parentNode.append(svg_element);
cls_name = iframes[i].className;
svg_element.classList.add(cls_name);
}
catch(error){
console.log(error);
}
}''')
def make_code_selectable(driver):
print("make_code_selectable function")
driver.execute_script('''
let code_containers = document.getElementsByClassName('monaco-editor');
for(let i=0;i<code_containers.length;i++){
if ( code_containers[i].classList.contains('no-user-select') )
code_containers[i].classList.remove('no-user-select');
}
''')
print("make_code_selectable function executed")
def single_file_js_executer(driver):
return driver.execute_script('''
const { content, title, filename } = await singlefile.getPageData({
removeImports: true,
removeScripts: true,
removeAudioSrc: true,
removeVideoSrc: true,
removeHiddenElements: true,
removeUnusedStyles: true,
removeUnusedFonts: true,
compressHTML: true,
blockVideos: true,
blockScripts: true,
networkTimeout: 60000
});
return content;
''')
def get_pagecontent_using_singleFile(driver, file_name, quiz_html):
print("Get HTML Page Content Using Single File Function")
# Inject SingleFile JS script
driver.execute_script('''
inject = document.createElement('script');
inject.src = "https://anilabhadatta.github.io/SingleFile/lib/single-file.js";
document.getElementsByTagName('head')[0].appendChild(inject);
''')
sleep(2)
make_code_selectable(driver)
try:
try:
page_content = single_file_js_executer(driver)
except Exception:
page_content = single_file_js_executer(driver)
create_html_with_singleFile(file_name, page_content, quiz_html)
except Exception:
take_screenshot(driver, file_name, quiz_html)
print("HTML Page content taken.")
def show_hints_answer(driver):
print("Show Hints Function")
hints_g_id_selector = "g[id*='noun_lightbulb']"
action = ActionChains(driver)
hints_list = driver.find_elements(
By.CSS_SELECTOR, hints_g_id_selector)
if hints_list:
for idx in range(len(hints_list)):
hints_list = driver.find_elements(
By.CSS_SELECTOR, hints_g_id_selector)
action.move_to_element(hints_list[idx]).click().perform()
sleep(1)
print("Show Hints Complete")
else:
print("No hints found")
def click_using_driver_js(driver, selector):
driver.execute_script(f'''
document.querySelector("{selector}").click();
''')
def show_code_box_answer(driver):
print("Show Codebox Answers Function")
solution_button_selectors = ["button[aria-label*='olution']",
"button[arialabel*='olution']"]
show_solution_button = "button[aria-label*='confirm']"
action = ActionChains(driver)
answer_list = []
for selectors in solution_button_selectors:
answer_list += driver.find_elements(By.CSS_SELECTOR, selectors)
if answer_list:
for answer_button in answer_list:
answer_button.location_once_scrolled_into_view
action.move_to_element(answer_button).click().perform()
sleep(1)
click_using_driver_js(driver, show_solution_button)
sleep(1)
print("Show Codebox Answers Complete")
else:
print("No Codebox answers found")
def create_temp_textarea(driver):
driver.execute_script('''
var div = document.querySelector('div[class*="ed-grid-main"]');
var input = document.createElement("textarea");
input.name = "temptextarea";
input.className = "temptextarea";
input.maxLength = "10000";
input.cols = "50";
input.rows = "10";
div.prepend(input);
''')
sleep(1)
def create_folder(folder_name):
if folder_name not in os.listdir():
os.mkdir(folder_name)
os.chdir(folder_name)
def download_parameters_for_chrome_headless(driver):
params = {'cmd': 'Page.setDownloadBehavior', 'params': {
'behavior': 'allow', 'downloadPath': os.getcwd()}}
driver.execute("send_command", params)
def click_on_sidebar(driver, code):
file_div_selector = "div[class*='ml-2']"
action = ActionChains(driver)
try:
elements = code.find_element(
By.CSS_SELECTOR, file_div_selector).find_elements(By.CSS_SELECTOR, "svg")
for element in elements:
try:
action.move_to_element(element).click().perform()
except Exception:
pass
except Exception:
pass
def check_file_in_dir(original_file_name):
file_name_splitted = original_file_name.split(".")
if len(file_name_splitted) == 1 and original_file_name in os.listdir():
return original_file_name
elif len(file_name_splitted) > 1 and file_name_splitted[0] == "":
for file in os.listdir():
if file_name_splitted[1] in file:
return file
else:
return original_file_name
def download_file(driver, element):
print("Download File Function")
action = ActionChains(driver)
file_name_selector = "div[class*='tooltip-inner']"
original_file_name = element.find_element(
By.XPATH, "../..").find_element(By.CSS_SELECTOR, "span").get_attribute('innerHTML')
try:
hover_file_name = driver.find_element(
By.CSS_SELECTOR, file_name_selector).get_attribute('innerHTML')
except Exception:
hover_file_name = original_file_name
action.move_to_element(element).click().perform()
sleep(1)
parent_div = element.find_element(By.XPATH, "../../..")
svg_button = parent_div.find_elements(By.CSS_SELECTOR, "svg")
if len(svg_button) == 1:
action.move_to_element(element).click().perform()
sleep(1)
parent_div = element.find_element(By.XPATH, "../../..")
svg_button = parent_div.find_elements(By.CSS_SELECTOR, "svg")
if len(svg_button) == 2:
action.move_to_element(svg_button[-1]).click().perform()
sleep(1)
parent_div = element.find_element(By.XPATH, "../../..")
svg_button = parent_div.find_elements(By.CSS_SELECTOR, "svg")
if len(svg_button) == 3:
action.move_to_element(svg_button[-1]).click().perform()
sleep(2)
original_file_name = check_file_in_dir(original_file_name)
os.rename(original_file_name, slugify(hover_file_name,
replacements=[['+', 'plus']]).replace("-", "."))
def download_code_manually(driver, code):
print("Download Code Manually Function")
file_div_selector = "div[class*='ml-2']"
svg_path_1 = "path[d*='M6 12l4']"
svg_path_2 = "path[d*='M4 6l4']"
action = ActionChains(driver)
elements = code.find_element(
By.CSS_SELECTOR, file_div_selector).find_elements(By.CSS_SELECTOR, "svg")
for element in elements:
try:
element.location_once_scrolled_into_view
if element.find_elements(By.CSS_SELECTOR, svg_path_1):
action.move_to_element(element).click().perform()
elif element.find_elements(By.CSS_SELECTOR, svg_path_2):
continue
else:
action.move_to_element(element).perform()
sleep(2)
download_file(driver, element)
sleep(1)
except Exception as e:
print(e)
pass
def code_container_download_type(driver):
print("Code Container Download Type Function")
code_container_selector = "div[class*='code-tabs-code-wrapper']"
div_class_selector = "div[aria-label*='webpackbin-tabs']"
download_svg_selector = "svg:not([title])"
action = ActionChains(driver)
code_containers = driver.find_elements(
By.CSS_SELECTOR, code_container_selector)
if code_containers:
code_directory_path = os.getcwd()
for folder_index, code in enumerate(code_containers):
buttons = code.find_elements(
By.CSS_SELECTOR, div_class_selector)
if not buttons:
click_on_sidebar(driver, code)
buttons = code.find_elements(
By.CSS_SELECTOR, div_class_selector)
if buttons:
download_button = code.find_elements(
By.CSS_SELECTOR, download_svg_selector)
if download_button:
create_folder("code_downloaded" + str(folder_index))
download_parameters_for_chrome_headless(driver)
sleep(1)
action.move_to_element(
download_button[-1]).click().perform()
sleep(2)
if os.listdir() == []:
try:
# download_code_manually(driver, code)
pass
except Exception:
pass
else:
print("Downloaded Zip File")
else:
print("Zip File not Downloaded")
os.chdir(code_directory_path)
else:
print("No Code Container Downloadable Type found")
def copy_code(container, driver, use_svg=True):
print("Copy Code Function")
clipboard_title_svg = "svg[title='Copy To Clipboard']"
clipboard_title_button = "button[title='Copy To Clipboard']"
action = ActionChains(driver)
sleep(1)
if use_svg:
svg_button = container.find_elements(
By.CSS_SELECTOR, clipboard_title_svg)[0]
else:
svg_button = container.find_elements(
By.CSS_SELECTOR, clipboard_title_button)[0]
print("Clicked on Clipboard")
action.move_to_element(svg_button).click().perform()
sleep(1)
textbox = click_on_textbox(driver)
if current_os == "darwin":
textbox.send_keys(Keys.COMMAND, "a")
textbox.send_keys(Keys.COMMAND, "v")
else:
textbox.send_keys(Keys.CONTROL, "a")
textbox.send_keys(Keys.CONTROL, "v")
print("Paste complete")
sleep(1)
return textbox.get_attribute('value')
def write_code(file_name, content):
print("Write Code Function")
file_name = slugify(file_name, replacements=[['+', 'plus']])
with open(file_name + ".txt", 'w', encoding='utf-8') as f:
f.write(content)
def iterate_top_nav_bar(code, top_nav_bar_buttons, side_nav_bar_butttons, driver, folder_index):
print('Inside iterate_top_nav_bar function')
nav_bar_tab_title = "span[id*='tab-title']"
side_nav_bar_butttons_selector = "div[class*='Widget_multi-files'] > div > div > div > :not(div[class*='monaco-editor'])"
for idx in range(len(top_nav_bar_buttons)):
nav_bar_tab = find_nav_bar_buttons(driver, folder_index)
nav_bar_file_name = nav_bar_tab[idx].find_element(
By.CSS_SELECTOR, nav_bar_tab_title).text
driver.execute_script("arguments[0].click();", nav_bar_tab[idx])
sleep(1)
print("Clicked on Tab")
side_nav_bar_butttons = code.find_elements(
By.CSS_SELECTOR, side_nav_bar_butttons_selector)
if side_nav_bar_butttons:
iterate_side_nav_bar(code, side_nav_bar_butttons,
driver, nav_bar_file_name)
else:
try:
returned_code = copy_code(code, driver)
write_code(nav_bar_file_name, returned_code)
except Exception as e:
print(e)
print("Failed to write")
find_and_write_code_solutions(
driver, code, nav_bar_file_name + str(file_index))
def iterate_side_nav_bar(code, side_nav_bar_butttons, driver, nav_bar_file_name=""):
print("Inside iterate_side_nav_bar function")
side_nav_bar_butttons_selector = "div[class*='Widget_multi-files'] > div > div > div > :not(div[class*='monaco-editor'])"
action = ActionChains(driver)
for idx in range(len(side_nav_bar_butttons)):
side_button = code.find_elements(
By.CSS_SELECTOR, side_nav_bar_butttons_selector)[idx]
side_button_text = side_button.text
action.move_to_element(side_button).click().perform()
sleep(1)
try:
returned_code = copy_code(code, driver)
file_name = nav_bar_file_name + side_button_text
write_code(file_name, returned_code)
except Exception as e:
print(e)
print("Failed to write")
if nav_bar_file_name == "":
find_and_write_code_solutions(
driver, code, "")
def find_and_write_code_solutions(driver, code, file_index):
print("Solution copying Function")
solution_code_selector = "div[class*='runnable-enter-done']"
code = code.find_element(By.XPATH, "../..").find_elements(
By.CSS_SELECTOR, solution_code_selector)
if code:
try:
returned_code = copy_code(code[0], driver, False)
write_code(f"{file_index}_Solution", returned_code)
except Exception as e:
print(e)
print("Failed to write")
else:
print("No solution found")
def iterate_general_code(code, driver, file_index):
print("Iterate General Code Function")
try:
returned_code = copy_code(code, driver, file_index)
write_code(f"Code_{file_index}", returned_code)
except Exception as e:
print(e)
print("Failed to write")
find_and_write_code_solutions(driver, code, file_index)
def find_nav_bar_buttons(driver, folder_index):
print("Execute Nav Bar Code Function")
nav_bar_tab_title = "span[id*='tab-title']"
code_container_selector = "div[class*='code-container']"
return driver.find_elements(By.CSS_SELECTOR, code_container_selector)[
folder_index].find_element(By.XPATH, "../..").find_element(By.CSS_SELECTOR, nav_bar_tab_title).find_element(By.XPATH, "../../../../..").find_elements(By.CSS_SELECTOR, "button")
def code_container_clipboard_type(driver):
print("Code Container Clipboard Type Function")
code_container_selector = "div[class*='code-container']"
side_nav_bar_butttons_selector = "div[class*='Widget_multi-files'] > div > div > div > :not(div[class*='monaco-editor'])"
textbox_selector = "textarea[class*='temptextarea']"
code_containers = driver.find_elements(
By.CSS_SELECTOR, code_container_selector)
if code_containers:
create_temp_textarea(driver)
code_directory_path = os.getcwd()
for folder_index, code in enumerate(code_containers):
create_folder("code_clipboard" + str(folder_index))
try:
top_nav_bar_buttons = find_nav_bar_buttons(
driver, folder_index)
except Exception as e:
top_nav_bar_buttons = []
side_nav_bar_butttons = code.find_elements(
By.CSS_SELECTOR, side_nav_bar_butttons_selector)
if top_nav_bar_buttons:
print("Nav Bar Found in code container")
iterate_top_nav_bar(
code, top_nav_bar_buttons, side_nav_bar_butttons, driver, folder_index)
elif not top_nav_bar_buttons and side_nav_bar_butttons:
print("Side Nav Bar Found in code container")
iterate_side_nav_bar(code, side_nav_bar_butttons, driver)
elif not top_nav_bar_buttons and not side_nav_bar_butttons:
print("General Code Container Found")
iterate_general_code(code, driver, str(folder_index))
os.chdir(code_directory_path)
delete_node(driver, textbox_selector)
else:
print("No code containers found")
def widget_tab_container_function(container, driver):
print("Widget Tab Container Function")
widget_inner_container = "div[class*='Widget__WidgetTabs']"
widget_code_selector = "div[class*='styles__CodeEditorStyled']"
li_selector = "ul > li[class*='Widget__WidgetTab']"
output_selector = "div[class*='HtmlResponsiveViewer']"
action = ActionChains(driver)
widget_tabs = container.find_elements(
By.CSS_SELECTOR, widget_inner_container)
if widget_tabs:
widget_tabs = widget_tabs[0].find_elements(
By.CSS_SELECTOR, li_selector)
for idx in range(len(widget_tabs)):
tab = container.find_element(By.CSS_SELECTOR, widget_inner_container).find_elements(
By.CSS_SELECTOR, li_selector)[idx]
try:
file_name = tab.get_attribute('innerHTML')
action.move_to_element(tab).click().perform()
except Exception as e:
pass
sleep(2)
code_container_inside_tab = container.find_element(By.CSS_SELECTOR, widget_inner_container).find_elements(
By.CSS_SELECTOR, widget_code_selector)
output_containers_inside_tab = container.find_element(By.CSS_SELECTOR, widget_inner_container).find_elements(
By.CSS_SELECTOR, output_selector)
try:
if code_container_inside_tab:
copy_code_from_widget_container(
code_container_inside_tab[0], driver, file_name)
elif output_containers_inside_tab:
take_screenshot_widget_tab(
driver, output_containers_inside_tab[0], file_name, idx)
except Exception as e:
pass
def runjs_container_function(container, driver):
print("RunJS Container Function")
output_selector = "div[class*='HtmlResponsiveViewer']"
runjs_code_selector = "div[class*='styles__CodeEditor']"
runjs_selector = "div[class*='runjs-container']"
runjs_containers = container.find_elements(
By.CSS_SELECTOR, runjs_selector)
if runjs_containers:
code_editors_inside_runjs = runjs_containers[0].find_elements(
By.CSS_SELECTOR, runjs_code_selector)
for idx, code_editor in enumerate(code_editors_inside_runjs):
try:
copy_code_from_widget_container(
code_editor, driver, f"code_{idx}")
except Exception as e:
pass
output_containers = runjs_containers[0].find_elements(
By.CSS_SELECTOR, output_selector)
for idx, output_container in enumerate(output_containers):
try:
take_screenshot_widget_tab(
driver, output_container, "Output", idx)
except Exception as e:
pass
def code_widget_type(driver):
print("Inside Widget Container Function")
container_selector = "div[class*='Widget__WidgetStyled']"
textbox_selector = "textarea[class*='temptextarea']"
widget_type_containers = driver.find_elements(
By.CSS_SELECTOR, container_selector)
if widget_type_containers:
code_directory_path = os.getcwd()
create_temp_textarea(driver)
for folder_index, container in enumerate(widget_type_containers):
create_folder("code_widget_type" + str(folder_index))
widget_tab_container_function(container, driver)
runjs_container_function(container, driver)
os.chdir(code_directory_path)
delete_node(driver, textbox_selector)
else:
print("No widget container found")
def copy_code_from_widget_container(code_container_inside_tab, driver, file_name):
print("Copying Code From Widget Container Function")
line_div_selector = "div[class*='lines-content']"
view_line_div_selector = "div[class*='view-line']"
action = ActionChains(driver)
line_div = code_container_inside_tab.find_element(
By.CSS_SELECTOR, line_div_selector).find_element(By.CSS_SELECTOR, view_line_div_selector)
action.move_to_element(line_div).click().perform()
copy_from_container(driver)
textbox = click_on_textbox(driver)
copy_from_container(driver, textbox)
write_code(file_name, textbox.get_attribute('value'))
def take_screenshot_widget_tab(driver, output_container, file_name, idx=0):
print("Taking Screenshot From Widget Container Function")
# output_image = screenshot_as_cdp(driver, output_container)
output_image = output_container.screenshot_as_base64
file_name = slugify(file_name, replacements=[['+', 'plus'], ['span', '']])
with open(f"{file_name}_{idx}.png", "wb") as f:
f.write(base64.urlsafe_b64decode(output_image))
def click_on_textbox(driver):
print("Click On Textbox Function")
action = ActionChains(driver)
textbox_selector = "textarea[class*='temptextarea']"
textbox = driver.find_element(
By.CSS_SELECTOR, textbox_selector)
action.move_to_element(textbox).click().perform()
sleep(1)
return textbox
def copy_from_container(driver, element=""):
print("Copy Code From Container Function")
action = ActionChains(driver)
sleep(1)
if current_os == "darwin":
if element:
element.send_keys(Keys.COMMAND, "a")
element.send_keys(Keys.COMMAND, "v")
else:
action.key_down(Keys.COMMAND).send_keys(
'a').key_up(Keys.COMMAND).perform()
action.key_down(Keys.COMMAND).send_keys(
'c').key_up(Keys.COMMAND).perform()
else:
if element:
element.send_keys(Keys.CONTROL, "a")
element.send_keys(Keys.CONTROL, "v")
else:
action.key_down(Keys.CONTROL).send_keys(
'a').key_up(Keys.CONTROL).perform()
action.key_down(Keys.CONTROL).send_keys(
'c').key_up(Keys.CONTROL).perform()
sleep(1)
def extract_zip_files():
print("Zip File Extraction")
for path in glob.iglob("./**/*educative-code-widget.zip", recursive=True):
zf = os.path.basename(path)
zipfile.ZipFile(path, 'r').extractall(path[:-len(zf)])
os.remove(path)
def demark_as_completed(driver):
print("Remove Mark completed")
div_selector = "div[class*='styles__Checkbox']"
try:
driver.execute_script(f'''
ele = document.querySelectorAll("{div_selector}");
ele[ele.length-1].click();
''')
except Exception:
pass
def click_option_quiz(driver, quiz_container):
print("Click on Option Quiz")
option_selector = "div[class*='question-option-view']"
action = ActionChains(driver)
try:
option = quiz_container.find_element(
By.CSS_SELECTOR, option_selector)
option.location_once_scrolled_into_view
action.move_to_element(option).click().perform()
sleep(1)
except Exception:
pass
def quiz_container_html(driver, quiz_container):
print("Take Quiz Screenshot Function")
# container_screenshot = screenshot_as_cdp(driver, quiz_container)
quiz_container.location_once_scrolled_into_view
container_screenshot = quiz_container.screenshot_as_base64
sleep(1)
return f'''<img style="display: block;margin-left: auto; margin-right: auto;" src="data:image/png;base64,{container_screenshot}" alt="">'''
def click_right_button_quiz(driver, quiz_container):
print("Clicking on Right button in Quiz")
action = ActionChains(driver)
right_button_selector = "button[class*='Button_quiz-widget-controls']:last-child"
quiz_html = ""
right_button = quiz_container.find_elements(
By.CSS_SELECTOR, right_button_selector)
if not right_button:
click_option_quiz(driver, quiz_container)
click_submit_quiz(driver, quiz_container)
click_submit_quiz(driver, quiz_container)
while right_button:
click_option_quiz(driver, quiz_container)
click_submit_quiz(driver, quiz_container)
quiz_html += quiz_container_html(driver, quiz_container)
if check_last_right_button(right_button):
break
action.move_to_element(right_button[0]).click().perform()
print("Clicking on Right button")
sleep(1)
click_on_submit_dialog_if_visible(driver)
right_button = quiz_container.find_elements(
By.CSS_SELECTOR, right_button_selector)
return quiz_html
def check_last_right_button(right_button):
print("Check Last Right Button in Quiz")
if right_button[0].find_elements(By.CSS_SELECTOR, "path"):
return True
return False
def click_on_submit_dialog_if_visible(driver):
print("Clicking on Submit dialog in Quiz")
div_selector = "div[class*='ConfirmationModal']"
button_selector = "button[id*='confirm-button']"
action = ActionChains(driver)
try:
dialog_box = driver.find_elements(
By.CSS_SELECTOR, div_selector)
if dialog_box:
button = dialog_box[0].find_elements(
By.CSS_SELECTOR, button_selector)
action.move_to_element(button[0]).click().perform()
else:
print("No submit dialog found")
except Exception:
pass
def click_submit_quiz(driver, quiz_container):
print("Inside Submit button Quiz Function")
action = ActionChains(driver)
try:
buttons = quiz_container.find_elements(By.CSS_SELECTOR, "button")
action.move_to_element(buttons[-1]).click().perform()
sleep(1)
except Exception:
pass
def take_quiz_screenshot(driver):
print("Inside take_quiz_screenshot function")
quiz_container_selector = "div[class*='quiz-view-mode']"
quiz_html = ""
quiz_containers = driver.find_elements(
By.CSS_SELECTOR, quiz_container_selector)
if quiz_containers:
for quiz_container in quiz_containers:
quiz_html += click_right_button_quiz(
driver, quiz_container)
else:
print("Quiz not found")
return quiz_html
def find_mark_down_quiz_containers(driver):
print("Inside find_mark_down_quiz_containers function")
div_selector = "div[role*='button']"
right_button_selector = "button[class*='Button_circle-button']:last-child"
quiz_html = ""