2019-02-26 00:24:02 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
import subprocess
|
|
|
|
import pathlib
|
|
|
|
import time
|
|
|
|
import os
|
2019-04-06 19:20:32 +02:00
|
|
|
import requests
|
|
|
|
import uuid
|
2021-11-24 14:38:08 +01:00
|
|
|
import urllib.parse
|
2019-04-06 19:20:32 +02:00
|
|
|
import json
|
|
|
|
import pyperclip
|
|
|
|
from config import SCREENSHOT_CONFIG
|
2021-12-01 23:49:27 +01:00
|
|
|
from pychee import pychee
|
2019-02-26 00:24:02 +01:00
|
|
|
|
|
|
|
def usage(path):
|
2019-03-13 17:20:12 +01:00
|
|
|
print('A tiny wrapper around maim to take screenshots and save or upload them (+ copy link in clipboard).\n')
|
|
|
|
print('The default behaviour is : nothing.')
|
|
|
|
print('Usage:')
|
|
|
|
print('\t' + sys.argv[0] + ' [--upload] [--save] [--path=<save_path>]')
|
|
|
|
print('\t' + sys.argv[0] + ' --help')
|
|
|
|
print('\nOptions:')
|
|
|
|
print('\t-h --help \tShow this screen.')
|
|
|
|
print('\t-u --upload \tUpload to server and copy link to clipboard.')
|
|
|
|
print('\t\t\tOnly Lychee Laravel server is supported for now.')
|
|
|
|
print('\t-s --save\tSave locally.')
|
|
|
|
print('\t-p --path\tPath where to save locally [default: ' + path +'].')
|
2019-02-26 00:24:02 +01:00
|
|
|
|
2019-03-13 17:20:12 +01:00
|
|
|
def read_args(argv):
|
2019-04-06 19:20:32 +02:00
|
|
|
path = SCREENSHOT_CONFIG['default_path']
|
2019-03-13 17:20:12 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
opts, _ = getopt.getopt(argv, "husp:", ["help", "upload", "save", "path="])
|
|
|
|
except getopt.GetoptError:
|
|
|
|
usage(path)
|
|
|
|
|
|
|
|
upload = False
|
|
|
|
save = False
|
|
|
|
|
|
|
|
for opt, _ in opts:
|
|
|
|
if opt in ('-h', '--help'):
|
|
|
|
usage(path)
|
|
|
|
elif opt in ('-u', '--upload'):
|
|
|
|
upload = True
|
|
|
|
elif opt in ('-s', '--save'):
|
|
|
|
save = True
|
|
|
|
elif opt in ('-p', '--path'):
|
|
|
|
path = opt
|
|
|
|
|
|
|
|
# Expand path if there is a ~ inside
|
|
|
|
path = os.path.expanduser(path)
|
|
|
|
|
|
|
|
return (upload, save, path)
|
2019-04-06 19:20:32 +02:00
|
|
|
|
2019-02-26 00:24:02 +01:00
|
|
|
def screenshot(img_format):
|
2020-05-04 18:17:32 +02:00
|
|
|
output = '/tmp/screenshot'
|
|
|
|
proc = subprocess.run(['maim', '-sl', '--color=0.6,0.4,0.2,0.2', '-f', img_format, '-u', '-m', '9', '-b', '5', output], check=True)
|
2020-05-04 18:40:00 +02:00
|
|
|
with open(output, 'rb') as f:
|
2020-05-04 18:17:32 +02:00
|
|
|
return f.read()
|
2019-02-26 00:24:02 +01:00
|
|
|
|
|
|
|
def save_img(path, img, img_format):
|
2019-03-13 17:20:12 +01:00
|
|
|
# Create directory if needed
|
|
|
|
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
# Get date formatted with second granularity
|
2019-05-01 21:57:09 +02:00
|
|
|
date = time.strftime("%Y-%m-%d_%H-%M-%S")
|
2019-03-13 17:20:12 +01:00
|
|
|
|
|
|
|
# Write image to appropriate file.
|
|
|
|
# We get binary from maim, so open in binary mode
|
|
|
|
filepath = os.path.join(path, date + '.' + img_format)
|
|
|
|
with open(filepath, 'wb') as f:
|
|
|
|
f.write(img)
|
2019-04-06 19:20:32 +02:00
|
|
|
|
|
|
|
return filepath
|
|
|
|
|
|
|
|
def login():
|
2021-12-01 23:49:27 +01:00
|
|
|
client = pychee.LycheeClient(SCREENSHOT_CONFIG['base_url'])
|
|
|
|
client.login(SCREENSHOT_CONFIG['user'], SCREENSHOT_CONFIG['password'])
|
|
|
|
return client
|
2019-04-06 19:20:32 +02:00
|
|
|
|
2021-12-01 23:49:27 +01:00
|
|
|
def get_album_id(client, name):
|
|
|
|
albums = client.get_albums()
|
2019-04-06 19:20:32 +02:00
|
|
|
for album in albums['albums']:
|
|
|
|
if album['title'] == name:
|
|
|
|
return album['id']
|
|
|
|
raise RuntimeError("No album " + name + " found!")
|
|
|
|
|
2021-12-01 23:49:27 +01:00
|
|
|
def upload_image(client, album_id, binary_img, img_format):
|
|
|
|
return client.add_photo(binary_img, str(uuid.uuid4()) + '.' + img_format, album_id)
|
|
|
|
|
|
|
|
def get_image_direct_link(client, image_id):
|
2023-02-20 16:09:56 +01:00
|
|
|
return 'https://pic.chosto.me' + '/' + client.get_photo(image_id)['size_variants']['original']['url']
|
2019-03-13 17:20:12 +01:00
|
|
|
|
2019-02-26 00:24:02 +01:00
|
|
|
def main(argv):
|
2019-03-13 17:20:12 +01:00
|
|
|
upload, save, path = read_args(argv)
|
2019-10-09 14:28:41 +02:00
|
|
|
|
2019-03-13 17:20:12 +01:00
|
|
|
# If we upload the file, set format to jpg, even if we save the screenshot after
|
2019-04-06 19:20:32 +02:00
|
|
|
img_format = SCREENSHOT_CONFIG['upload_format'] if upload else SCREENSHOT_CONFIG['save_format']
|
2019-03-13 17:20:12 +01:00
|
|
|
|
|
|
|
if upload or save:
|
|
|
|
try:
|
|
|
|
img = screenshot(img_format)
|
|
|
|
if save:
|
2019-04-06 19:20:32 +02:00
|
|
|
final_path = save_img(path, img, img_format)
|
|
|
|
pyperclip.copy(final_path)
|
|
|
|
subprocess.run(['notify-send', '-u', 'low', '-i', 'Info', '-a', 'Screenchost', 'Screenshot taken', 'Path copied to clipboard!'])
|
|
|
|
if upload:
|
|
|
|
s = login()
|
|
|
|
album_id = get_album_id(s, SCREENSHOT_CONFIG['album_name'])
|
2023-02-20 16:09:56 +01:00
|
|
|
image_info = upload_image(s, album_id, img, img_format)
|
|
|
|
image_link = get_image_direct_link(s, image_info['id'])
|
2019-04-06 19:20:32 +02:00
|
|
|
pyperclip.copy(image_link)
|
|
|
|
subprocess.run(['notify-send', '-u', 'low', '-i', 'Info', '-a', 'Screenchost', 'Screenshot uploaded', 'Link copied to clipboard!'])
|
2019-03-13 17:20:12 +01:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print("Screenshot taking has failed {0}".format(e))
|
2019-02-26 00:24:02 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-03-13 17:20:12 +01:00
|
|
|
main(sys.argv[1:])
|