File size: 3,350 Bytes
4365c47
 
 
94a6b38
 
4365c47
94a6b38
 
 
 
 
 
 
 
 
 
 
 
 
4365c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94a6b38
 
 
 
4365c47
94a6b38
 
 
 
4365c47
94a6b38
 
 
 
 
 
 
 
 
 
 
 
 
4365c47
94a6b38
 
 
 
 
 
 
 
 
 
 
 
 
 
4365c47
 
 
 
 
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
from playwright.sync_api import sync_playwright
import os
from dotenv import load_dotenv
import time
import base64

# def load_credentials()-> dict:
#     load_dotenv()
#     URL = os.getenv('FAKE_APP_URL')
#     USERNAME = os.getenv('FAKE_APP_USERNAME')
#     PASSWORD = os.getenv('FAKE_APP_PASSWORD')
#     return (URL,USERNAME,PASSWORD)

def decode_token(base64_token) -> str:
    base64_bytes = base64_token.encode("ascii")
    token_bytes = base64.b64decode(base64_bytes)
    token = token_bytes.decode("ascii")
    # print(token)
    return token

def extract_homework_text(page) -> str:
    card = page.get_by_title("homework")
    content = card.locator("[data-slot='card-content'] section > div").all()

    output = ["Homework:\n"]
    for section in content:
        heading = section.locator("h3").inner_text()
        output.append(heading)
        items = section.locator("ul > li").all()
        for item in items:
            # Extract full inner text including formatting
            inner = item.inner_text().strip()
            output.append(f"  {inner}")
        output.append("")  # Add a blank line between sections
        
    return "\n".join(output).strip()

def extract_timetable_text(page):
    card = page.get_by_title("timetable")
    items = card.locator("[data-slot='card-content'] ul > li").all()

    output = ["Timetable:\n"]
    for item in items:
        # Check if it's a plain text item like "Lunch break"
        if item.locator("span").count() == 0:
            output.append(item.inner_text().strip())
        else:
            parts = item.locator("span").all()
            line = " ".join([part.inner_text().strip() for part in parts])
            output.append(line)

    return "\n".join(output).strip()


# print(URL,USERNAME,PASSWORD)
def get_homework(base64_token) -> str:
    token=decode_token(base64_token)
    # print(token)
    URL,USERNAME,PASSWORD=token.split("<sep>")
    
    with sync_playwright() as playwright:
        browser = playwright.firefox.launch(headless=True)
        page = browser.new_page()
        page.goto(URL,wait_until="domcontentloaded")
        
        page.get_by_role('textbox',name='username').fill(USERNAME)
        page.get_by_role('textbox',name='password').fill(PASSWORD)
        page.get_by_role('button',name='login').click()
        page.wait_for_url("**/dashboard")
        # page.wait_for_timeout(1000)         
        homework = extract_homework_text(page)
        browser.close()        
    return homework

def get_timetable(base64_token) -> str:
    token=decode_token(base64_token)
    URL,USERNAME,PASSWORD=token.split("<sep>")
    # URL,USERNAME,PASSWORD=load_credentials()
    
    with sync_playwright() as playwright:
        browser = playwright.firefox.launch(headless=True)
        page = browser.new_page()
        page.goto(URL,wait_until="domcontentloaded")
        
        page.get_by_role('textbox',name='username').fill(USERNAME)
        page.get_by_role('textbox',name='password').fill(PASSWORD)
        page.get_by_role('button',name='login').click()
        page.wait_for_url("**/dashboard")
        # page.wait_for_timeout(1000)        
        timetable = extract_timetable_text(page)
        browser.close()
        
    return timetable

if __name__=="__main__":
    print(get_homework())
    print()
    print(get_timetable())