use maim to capture images

This commit is contained in:
kageru 2018-01-22 22:31:48 +01:00
parent de3bd43f68
commit b96da45245
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 46 additions and 45 deletions

View File

@ -18,7 +18,7 @@ Needless to say, this is being developed for and tested on Linux. If you're on W
``` ```
$ pip install pysftp pyperclip pillow $ pip install pysftp pyperclip pillow
``` ```
as well as `escrotum` or `i3-scrot` (if you’re using i3wm) and `notify-send` which should be available in your favorite package manager as well as `maim` (https://github.com/naelstrof/maim) and `notify-send` which should be available in your favorite package manager
(or pre-installed, depending on your distribution and desktop environment). (or pre-installed, depending on your distribution and desktop environment).
### Usage ### Usage

View File

@ -16,12 +16,8 @@ curl_command = None
# This is where the screenshots are saved locally # This is where the screenshots are saved locally
local_directory = '/home/kageru/pyshare/' local_directory = '/home/kageru/pyshare/'
# If set to false, images will be deleted after uploading them
# Use i3scrot for screen capture. Otherwise, escrotum will be used. keep_local_copies = True
# You might have to install the capture tool you want to use via your package manager.
# i3scrot was much faster for me when working with multiple high-res displays.
# If you do use it, you manually need to set its save directory to your local_directory (above) + 'tmp' in ~/.config/i3-scrot.conf
use_i3scrot = False
# Directory on the ftp server where you want the screenshots to be sent # Directory on the ftp server where you want the screenshots to be sent
remote_directory = '/usr/share/nginx/html/pyshare/' remote_directory = '/usr/share/nginx/html/pyshare/'

View File

@ -14,11 +14,12 @@ import re
character_pool = ascii_letters + digits character_pool = ascii_letters + digits
def parse_arguments(): def parse_arguments():
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument('-m' '--mode', type=str, parser.add_argument('-m' '--mode', type=str, dest='mode', default='screenshot',
help='Sets the input mode. Allowed values are "screenshot" and "clipboard". Implicit it file(s) are set.') help='Sets the input mode. Allowed values are "screenshot" and "clipboard". Implicit it file(s) are set.')
parser.add_argument('-f', '--files', type=str, nargs='*', help='List of files to be uploaded') parser.add_argument('-f', '--files', type=str, nargs='*', dest='files', help='List of files to be uploaded')
return parser.parse_args() return parser.parse_args()
@ -37,6 +38,7 @@ def find_valid_filename(prefix, length, ext, conn):
return find_valid_filename(prefix, length + 1, ext, conn) return find_valid_filename(prefix, length + 1, ext, conn)
return filename return filename
def upload_local_file(path: str) -> str: def upload_local_file(path: str) -> str:
if config.uploader in ['ftp', 'sftp']: if config.uploader in ['ftp', 'sftp']:
filename = ftp_upload(path)[1] filename = ftp_upload(path)[1]
@ -46,22 +48,12 @@ def upload_local_file(path: str) -> str:
def take_screenshot() -> None: def take_screenshot() -> None:
tmppath = os.path.join(config.local_directory, 'tmp') tempname = generate_filename(config.length, 'png')
if not os.path.isdir(tmppath): file = os.path.join(config.local_directory, tempname)
os.mkdir(tmppath) call(['maim', '-s', file])
else: ftp_upload(ext='png', sourcefile=file, mode='screenshot')
tmpdir = os.listdir(tmppath) if not config.keep_local_copies:
for f in tmpdir: os.remove(file)
os.remove(os.path.join(tmppath, f))
if config.use_i3scrot:
call(['i3-scrot', '-s'])
file = os.path.join(tmppath, os.listdir(tmppath)[0])
else:
tempname = generate_filename(3, 'png')
call(['escrotum', '-s', tempname])
file = os.path.join(tmppath, tempname)
ftp_upload(ext='png', sourcefile=file)
os.remove(file)
def ftp_upload(sourcefile, *, mode=None, ext=None) -> tuple: def ftp_upload(sourcefile, *, mode=None, ext=None) -> tuple:
@ -70,19 +62,32 @@ def ftp_upload(sourcefile, *, mode=None, ext=None) -> tuple:
exts = { exts = {
'screenshot': 'png', 'screenshot': 'png',
'text': 'txt', 'text': 'txt',
} }
ext = exts.get(mode, mode not in exts and sourcefile.split('.')[-1]) # Only do the split if necessary if re.search('\.tar\.\w{1,4}]', sourcefile):
# properly handle .tar.something files
ext = sourcefile.split('.')[-2:]
else:
ext = exts.get(mode, mode not in exts and sourcefile.split('.')[-1]) # Only do the split if necessary
with Connection(config.sftp_address, username=config.username, password=config.password, with Connection(config.sftp_address, username=config.username, password=config.password,
private_key=config.private_key, private_key_pass=config.private_key_pass) as conn: private_key=config.private_key, private_key_pass=config.private_key_pass) as conn:
conn.chdir(config.remote_directory) conn.chdir(config.remote_directory)
os.chdir(config.local_directory)
filename = find_valid_filename(prefix=config.prefix, length=config.length, ext=ext, conn=conn)
cur_name = sourcefile.split('/')[-1]
filename = cur_name
if mode == 'screenshot':
if conn.exists(cur_name):
filename = find_valid_filename(prefix=config.prefix, length=config.length, ext=ext, conn=conn)
conn.put(filename, filename)
else:
filename = find_valid_filename(prefix=config.prefix, length=config.length, ext=ext, conn=conn)
if mode == 'file':
conn.put(sourcefile, filename)
fullpath = os.path.join(config.local_directory, filename) fullpath = os.path.join(config.local_directory, filename)
if mode == 'file':
conn.put(sourcefile, filename)
notify_user(config.url_template.format(filename)) notify_user(config.url_template.format(filename))
return fullpath, filename return fullpath, filename
@ -107,13 +112,13 @@ def notify_user(url, image=None):
def parse_text(args): def parse_text(args):
text = pyperclip.paste() text = pyperclip.paste()
if re.match(r'(https?|s?ftp)://', text): if re.match(r'(https?|s?ftp)://', text):
mirror_file(text) mirror_file(text)
elif os.path.isfile(text): elif os.path.isfile(text):
upload_local_file(text) upload_local_file(text)
else: else:
upload_text(text) upload_text(text)
def mirror_file(text): def mirror_file(text):
@ -136,14 +141,14 @@ def upload_text(text):
if __name__ == '__main__': if __name__ == '__main__':
args = parse_arguments() args = parse_arguments()
if args.mode == 'clipboard': if args.mode == 'screenshot':
parse_text(pyperclip.paste())
elif args.mode == 'screenshot'
take_screenshot() take_screenshot()
elif args.mode == 'clipboard':
parse_text(pyperclip.paste())
else: else:
for file in args.files: for file in args.files:
upload_local_file(file) upload_local_file(file)
""" """
if config.uploader in ['ftp', 'sftp']: if config.uploader in ['ftp', 'sftp']:
if args.files is not None: if args.files is not None: