2019-02-26 00:24:02 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
import subprocess
|
|
|
|
import pathlib
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
|
|
|
|
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):
|
|
|
|
path = '~/img/screenshots'
|
|
|
|
|
|
|
|
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-02-26 00:24:02 +01:00
|
|
|
def screenshot(img_format):
|
2019-03-13 17:20:12 +01:00
|
|
|
proc = subprocess.run(['maim', '-sl', '--color=0.6,0.4,0.2,0.2', '-f', img_format, '-u', '-m', '9', '-b', '5'], capture_output=True, check=True)
|
|
|
|
return proc.stdout
|
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
|
|
|
|
date = time.strftime("%Y-%m-%d_%H:%M:%S")
|
|
|
|
|
|
|
|
# Write image to appropriate file.
|
|
|
|
# We get binary from maim, so open in binary mode
|
|
|
|
filepath = os.path.join(path, date + '.' + img_format)
|
|
|
|
print(filepath)
|
|
|
|
with open(filepath, 'wb') as f:
|
|
|
|
f.write(img)
|
|
|
|
|
|
|
|
#def upload(binary_img):
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
# If we upload the file, set format to jpg, even if we save the screenshot after
|
|
|
|
img_format = 'jpg' if upload else 'png'
|
|
|
|
|
|
|
|
|
|
|
|
if upload or save:
|
|
|
|
try:
|
|
|
|
img = screenshot(img_format)
|
|
|
|
if save:
|
|
|
|
save_img(path, img, img_format)
|
|
|
|
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:])
|
|
|
|
|