Use https://pypi.org/project/pychee library for uploading screenshots

master
Quentin Duchemin 2021-12-01 23:49:27 +01:00
parent c332ce3086
commit f214833afb
Signed by: Chosto
GPG Key ID: 0547178FEEDE7D6B
1 changed files with 11 additions and 53 deletions

View File

@ -12,6 +12,7 @@ import urllib.parse
import json
import pyperclip
from config import SCREENSHOT_CONFIG
from pychee import pychee
def usage(path):
print('A tiny wrapper around maim to take screenshots and save or upload them (+ copy link in clipboard).\n')
@ -74,65 +75,22 @@ def save_img(path, img, img_format):
return filepath
def login():
data = {'username': SCREENSHOT_CONFIG['user'], 'password': SCREENSHOT_CONFIG['password'], 'function': 'Session::login'}
client = pychee.LycheeClient(SCREENSHOT_CONFIG['base_url'])
client.login(SCREENSHOT_CONFIG['user'], SCREENSHOT_CONFIG['password'])
return client
# Get initial cookies
s = requests.Session()
r = s.post(SCREENSHOT_CONFIG['base_url'] + '/api/Session::init')
# Set the CSRF token for the whole session
s.headers.update({
'X-XSRF-TOKEN': urllib.parse.unquote(r.cookies['XSRF-TOKEN'])
})
r = s.post(SCREENSHOT_CONFIG['base_url'] + '/api/Session::login', data=data)
if 'false' in r.text:
raise RuntimeError(f"Cannot login to Lychee! [{r.status_code}] {r.text}")
return s
def get_album_id(session, name):
r = session.post(SCREENSHOT_CONFIG['base_url'] + '/api/Albums::get')
if r.headers['content-type'] != 'application/json':
raise RuntimeError("API call didn't return a JSON object!")
albums = r.json()
print(albums)
def get_album_id(client, name):
albums = client.get_albums()
for album in albums['albums']:
if album['title'] == name:
return album['id']
raise RuntimeError("No album " + name + " found!")
def upload_image(session, album_id, binary_img, img_format):
img_mime = 'image/'
if img_format == 'jpg':
img_mime += 'jpeg'
elif img_format == 'png':
img_mime += 'png'
else:
raise ValueError("Invalid image format {0}!".format(img_format))
data = {'albumID': album_id}
files = {'0': (str(uuid.uuid4()) + "." + img_format, binary_img, img_mime)}
r = requests.Request('POST', SCREENSHOT_CONFIG['base_url'] + '/api/Photo::add', data=data, files=files)
dump = session.prepare_request(r)
r = session.post(SCREENSHOT_CONFIG['base_url'] + '/api/Photo::add', data=data, files=files)
print(r.text)
if "false" in r.text:
raise RuntimeError("Unknown error while uploading picture")
def upload_image(client, album_id, binary_img, img_format):
return client.add_photo(binary_img, str(uuid.uuid4()) + '.' + img_format, album_id)
return r.text
def get_image_direct_link(session, album_id, image_id):
data = {'albumID': album_id, 'photoID': image_id}
r = session.post(SCREENSHOT_CONFIG['base_url'] + "/api/Photo::get", data=data)
if r.text == "false":
raise ValueError("Image does not exist")
if r.headers['content-type'] != 'application/json':
raise RuntimeError("API call didn't return a JSON object")
image_info = r.json()
return SCREENSHOT_CONFIG['base_url'] + '/' + image_info['url']
def get_image_direct_link(client, image_id):
return SCREENSHOT_CONFIG['base_url'] + '/' + client.get_photo(image_id)['url']
def main(argv):
upload, save, path = read_args(argv)
@ -151,7 +109,7 @@ def main(argv):
s = login()
album_id = get_album_id(s, SCREENSHOT_CONFIG['album_name'])
image_id = upload_image(s, album_id, img, img_format)
image_link = get_image_direct_link(s, album_id, image_id)
image_link = get_image_direct_link(s, image_id)
pyperclip.copy(image_link)
subprocess.run(['notify-send', '-u', 'low', '-i', 'Info', '-a', 'Screenchost', 'Screenshot uploaded', 'Link copied to clipboard!'])
except subprocess.CalledProcessError as e: