Skip to content

Commit c9e8a3a

Browse files
D-VRRomern
authored andcommitted
Update __main__.py
Update the session key parsing to search in the script cfg
1 parent ba65e57 commit c9e8a3a

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

syncmymoodle/__main__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,16 @@ def __init__(self, config):
179179

180180
def login(self):
181181
def get_session_key(soup):
182-
try:
183-
session_key = soup.find("input", {"name": "sesskey"})["value"]
184-
except TypeError:
185-
logger.critical("Can't retrieve session key")
186-
logger.info(soup)
187-
sys.exit(1)
188-
return session_key
182+
script = soup.find("script", string=lambda text: text and 'sesskey' in text)
183+
js_text = script.text
184+
match = re.search(r'"sesskey":"(.*?)"', js_text)
185+
if match:
186+
return match.group(1)
187+
else:
188+
logger.critical("Can't retrieve session key from JavaScript config")
189+
exit(1)
190+
191+
189192

190193
self.session = requests.Session()
191194
cookie_file = Path(self.config.get("cookie_file", "./session"))

0 commit comments

Comments
 (0)