-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
91 lines (67 loc) · 2.43 KB
/
main.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
from datetime import datetime
import httpx
from selectolax.parser import HTMLParser
from dataclasses import dataclass
from urllib.parse import urljoin
@dataclass
class Solution:
link: str
code: str
def save_code(client, temp):
resp = client.get(temp.link).text
html = HTMLParser(resp)
csrf_token = html.css("head meta[name=csrf-token]")[0].attributes.get("content")
headers = {
"X-CSRF-TOKEN": csrf_token,
"X-Requested-With": "XMLHttpRequest",
}
body = {
"code": temp.code,
"status": "SAVE"
}
response = client.post(
temp.link.replace("start-lab", "save-code"),
data=body, headers=headers)
print(response.json())
def get_url(client):
response = client.get("https://litcoder.in/candidate/labs").text
html = HTMLParser(response)
module_page = html.css("div.owl-carousel.owl-theme")[2].css("a.button-design.continue.items-center")
url_list = []
result = []
for url in module_page:
data = HTMLParser(client.get(url.attributes.get("href")).text).css("div.lab-practice-detail div.excerciseCard")
for _ in data:
url_list.append(urljoin(f'{url.attributes.get("href")}/exercises',
f'{_.attributes.get("data-excercise-id")}/start-lab'))
for url in url_list:
data = HTMLParser(client.get(url).text).css("div#editor")[0]
result.append(Solution(
link=url,
code=data.text()
))
return result
def login(client, email, password):
response = client.get("https://litcoder.in/login").content
html = HTMLParser(response)
token = html.css(
"#kt_app_content_container > div > div > div.col-md-5.left-container > div > div > form > input[type=hidden]")[
0].attributes.get("value")
data = {
"_token": token,
"email": email,
"password": password,
}
client.post("https://litcoder.in/login", data=data, follow_redirects=True)
def run():
client = httpx.Client()
login(client, "Account credentials of a user who has already completed the Litcoder lab. (university email)", "password")
data = get_url(client)
newClient = httpx.Client()
login(newClient, "Account credentials of a user to whom you want to copy the lab code. (university email)", "password")
for temp in data:
save_code(newClient, temp)
def main():
run()
if __name__ == "__main__":
main()