|
| 1 | +from datetime import datetime |
| 2 | +import httpx |
| 3 | +from selectolax.parser import HTMLParser |
| 4 | +from dataclasses import dataclass |
| 5 | +from urllib.parse import urljoin |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class Solution: |
| 10 | + link: str |
| 11 | + code: str |
| 12 | + |
| 13 | + |
| 14 | +def save_code(client, temp): |
| 15 | + resp = client.get(temp.link).text |
| 16 | + html = HTMLParser(resp) |
| 17 | + csrf_token = html.css("head meta[name=csrf-token]")[0].attributes.get("content") |
| 18 | + |
| 19 | + headers = { |
| 20 | + "X-CSRF-TOKEN": csrf_token, |
| 21 | + "X-Requested-With": "XMLHttpRequest", |
| 22 | + } |
| 23 | + body = { |
| 24 | + "code": temp.code, |
| 25 | + "status": "SAVE" |
| 26 | + } |
| 27 | + |
| 28 | + response = client.post( |
| 29 | + temp.link.replace("start-lab", "save-code"), |
| 30 | + data=body, headers=headers) |
| 31 | + |
| 32 | + print(response.json()) |
| 33 | + |
| 34 | + |
| 35 | +def get_url(client): |
| 36 | + response = client.get("https://litcoder.in/candidate/labs").text |
| 37 | + html = HTMLParser(response) |
| 38 | + module_page = html.css("div.owl-carousel.owl-theme")[2].css("a.button-design.continue.items-center") |
| 39 | + url_list = [] |
| 40 | + |
| 41 | + result = [] |
| 42 | + |
| 43 | + for url in module_page: |
| 44 | + data = HTMLParser(client.get(url.attributes.get("href")).text).css("div.lab-practice-detail div.excerciseCard") |
| 45 | + for _ in data: |
| 46 | + url_list.append(urljoin(f'{url.attributes.get("href")}/exercises', |
| 47 | + f'{_.attributes.get("data-excercise-id")}/start-lab')) |
| 48 | + |
| 49 | + for url in url_list: |
| 50 | + data = HTMLParser(client.get(url).text).css("div#editor")[0] |
| 51 | + result.append(Solution( |
| 52 | + link=url, |
| 53 | + code=data.text() |
| 54 | + )) |
| 55 | + |
| 56 | + return result |
| 57 | + |
| 58 | + |
| 59 | +def login(client, email, password): |
| 60 | + response = client.get("https://litcoder.in/login").content |
| 61 | + html = HTMLParser(response) |
| 62 | + token = html.css( |
| 63 | + "#kt_app_content_container > div > div > div.col-md-5.left-container > div > div > form > input[type=hidden]")[ |
| 64 | + 0].attributes.get("value") |
| 65 | + |
| 66 | + data = { |
| 67 | + "_token": token, |
| 68 | + "email": email, |
| 69 | + "password": password, |
| 70 | + } |
| 71 | + |
| 72 | + client.post("https://litcoder.in/login", data=data, follow_redirects=True) |
| 73 | + |
| 74 | + |
| 75 | +def run(): |
| 76 | + client = httpx.Client() |
| 77 | + login(client, "Account credentials of a user who has already completed the Litcoder lab. (university email)", "password") |
| 78 | + |
| 79 | + data = get_url(client) |
| 80 | + newClient = httpx.Client() |
| 81 | + login(newClient, "Account credentials of a user to whom you want to copy the lab code. (university email)", "password") |
| 82 | + for temp in data: |
| 83 | + save_code(newClient, temp) |
| 84 | + |
| 85 | + |
| 86 | +def main(): |
| 87 | + run() |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments