-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (54 loc) · 1.95 KB
/
Copy pathmain.py
File metadata and controls
69 lines (54 loc) · 1.95 KB
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
import requests
import json
import csv
import re
import os
from dotenv import load_dotenv
load_dotenv()
subjectid = os.getenv("SUBJECT_ID")
cookies = {
"JOSSO_SESSIONID": os.getenv("JOSSO_SESSIONID"),
"GDPR": "required"
}
headers = {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json",
"Origin": "https://lernen.phase-6.de",
"Referer": "https://lernen.phase-6.de/v2/",
"User-Agent": "Mozilla/5.0",
"X-CLIENTID": os.getenv("X_CLIENTID"),
"X-JAUTHTOKEN": os.getenv("X_JAUTHTOKEN"),
"X-LBTOKEN": os.getenv("X_LBTOKEN")
}
txt_file = f"{subjectid}.txt"
csv_file = f"{subjectid}.csv"
url = "https://lernen.phase-6.de/server.integration/cardList"
payload = {
"cards": [],
"subjectId": subjectid,
"units": []
}
r = requests.post(url, headers=headers, cookies=cookies, json=payload)
r.raise_for_status()
data = r.json()
with open(txt_file, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
print(f"Response saved: {txt_file}")
audio_regex = re.compile(r"\[\{\~.*?:audio~\}\]")
cards_list = data.get("replyContent", {}).get("cards", [])
with open(csv_file, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(["Deutsch", "Spanisch", "Beispiel", "LearningDirection", "UnitID"])
for c in cards_list:
if not isinstance(c, dict):
continue
content = c.get("cardContent")
if not content:
continue
de = content.get("question", "")
es = audio_regex.sub("", content.get("answer", ""))
example = content.get("answerExample", "")
learning_direction = c.get("learningDirection", "")
unit_id = c.get("unitIdString", "")
writer.writerow([de, es, example, learning_direction, unit_id])
print(f"CSV created: {csv_file}")