From 0c4f8d97f2921dc92189a29a5cabe0b46d72ad96 Mon Sep 17 00:00:00 2001 From: Quentin Duchemin Date: Sat, 17 Jul 2021 11:28:46 +0200 Subject: [PATCH] Fix Lychee upload --- .config/i3/scripts/screenshot.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/.config/i3/scripts/screenshot.py b/.config/i3/scripts/screenshot.py index 96f47df..5753c14 100755 --- a/.config/i3/scripts/screenshot.py +++ b/.config/i3/scripts/screenshot.py @@ -73,20 +73,22 @@ def save_img(path, img, img_format): return filepath def login(): - auth = {'user': SCREENSHOT_CONFIG['user'], 'password': SCREENSHOT_CONFIG['password']} + data = {'username': SCREENSHOT_CONFIG['user'], 'password': SCREENSHOT_CONFIG['password'], 'function': 'Session::login'} # Get initial cookies s = requests.Session() - r = s.get(SCREENSHOT_CONFIG['base_url']) + r = s.post(SCREENSHOT_CONFIG['base_url'] + '/api/Session::init') # Set the CSRF token for the whole session - # Also replace the ending %3D (=), they stop header from being valid (why ?) - s.headers.update({'X-XSRF-TOKEN': r.cookies['XSRF-TOKEN'].replace('%3D', '')}) + # Also replace base64 padding + s.headers.update({ + 'X-XSRF-TOKEN': r.cookies['XSRF-TOKEN'].replace('%3D', '') + }) - r = s.post(SCREENSHOT_CONFIG['base_url'] + '/api/Session::login', data=auth) + r = s.post(SCREENSHOT_CONFIG['base_url'] + '/api/Session::login', data=data) - if r.status_code != 200: - raise RuntimeError("Cannot login to Lychee!") + if 'false' in r.text: + raise RuntimeError(f"Cannot login to Lychee! [{r.status_code}] {r.text}") return s @@ -116,7 +118,7 @@ def upload_image(session, album_id, binary_img, img_format): dump = session.prepare_request(r) r = session.post(SCREENSHOT_CONFIG['base_url'] + '/api/Photo::add', data=data, files=files) print(r.text) - if "Error" in r.text: + if "false" in r.text: raise RuntimeError("Unknown error while uploading picture") return r.text