Fix Lychee upload

master
Quentin Duchemin 2021-07-17 11:28:46 +02:00
parent d84507bb43
commit 0c4f8d97f2
Signed by: Chosto
GPG Key ID: 0547178FEEDE7D6B
1 changed files with 10 additions and 8 deletions

View File

@ -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