Skip to content

Commit a3dae4f

Browse files
committed
project init
Signed-off-by: Sahil Suman <sahilsuman933@gmail.com>
0 parents  commit a3dae4f

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__pycache__
2+
.idea
3+
.venv

‎README.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Litcoder Solution Copy Script
2+
3+
This script automates the retrieval and copying of submitted code from the Litcoder website to another user's account.
4+
5+
## Requirements
6+
7+
### Dependency Installation
8+
9+
Before running the script, ensure you've installed the necessary dependencies using the following command:
10+
11+
\```bash
12+
pip install -r requirements.txt
13+
\```
14+
15+
## Dependencies
16+
17+
The script relies on specific Python packages:
18+
19+
- `selectolax`: An HTML parser
20+
- `httpx`: Enables asynchronous HTTP requests.
21+
22+
## Running the Script
23+
24+
### Adding Valid Credentials
25+
26+
1. n the main.py file, input the account credentials of a user who has completed the Litcoder lab and the credentials of
27+
the user to whom you intend to copy the lab code.
28+
29+
### Executing the Script
30+
31+
2. Run the script using the following command:
32+
33+
\```bash
34+
python main.py
35+
\```
36+
37+
This action will retrieve all submitted code from the provided credentials and replicate it in your account.
38+
39+
### Output
40+
41+
- Upon successful execution, you will find completed lab assignments in your recent lab section. You can access these
42+
labs, observe the pre-written solutions, and simply submit your code afterward.
43+
44+
### Working Screenshot
45+
46+
![img.png](img.png)

‎main.py‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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()

‎requirements.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
httpx
2+
selectolax

0 commit comments

Comments
 (0)