allow mirroring of files and clipboard upload
This commit is contained in:
parent
ee23f3ec56
commit
38774f0317
57
pyshare.py
57
pyshare.py
|
@ -6,7 +6,7 @@ from pysftp import Connection
|
|||
from subprocess import call
|
||||
from random import choices
|
||||
import pyperclip
|
||||
import config
|
||||
import config2 as config
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
|
@ -27,15 +27,15 @@ def generate_filename(length, ext, prefix=''):
|
|||
|
||||
|
||||
def find_valid_filename(prefix, length, ext, conn):
|
||||
filename = generate_filename(prefix, length, ext)
|
||||
filename = generate_filename(prefix=prefix, length=length, ext=ext)
|
||||
i = 0
|
||||
while conn.exists(filename):
|
||||
filename = generate_filename(prefix, length, ext)
|
||||
filename = generate_filename(length=length, ext=ext, prefix=prefix)
|
||||
i += 1
|
||||
if i > 1000:
|
||||
# completely, definitely, totally justified recursion... yay?
|
||||
find_valid_filename(prefix, length + 1, ext, conn)
|
||||
|
||||
return find_valid_filename(prefix, length + 1, ext, conn)
|
||||
return filename
|
||||
|
||||
def upload_local_file(path: str) -> str:
|
||||
filename = ftp_upload(mode='file', sourcefile=path)[1]
|
||||
|
@ -48,16 +48,16 @@ def take_screenshot(filename: str) -> None:
|
|||
|
||||
def ftp_upload(mode='screenshot', ext=None, sourcefile=None) -> tuple:
|
||||
if ext is None:
|
||||
if mode == 'screenshot':
|
||||
ext = 'png'
|
||||
else:
|
||||
ext = sourcefile.rsplit('.', 1)[1]
|
||||
ext = {
|
||||
'screenshot': 'png',
|
||||
'text': 'txt',
|
||||
}.get(mode, sourcefile.rsplit('.', 1)[1])
|
||||
|
||||
with Connection(config.sftp_address, username=config.username, password=config.password,
|
||||
private_key=config.private_key) as conn:
|
||||
conn.chdir(config.remote_directory)
|
||||
|
||||
filename = find_valid_filename(config.prefix, config.length, ext, conn) + '.{}'.format(ext)
|
||||
filename = find_valid_filename(prefix=config.prefix, length=config.length, ext=ext, conn=conn)
|
||||
fullpath = os.path.join(config.local_directory, filename)
|
||||
|
||||
if mode == 'screenshot':
|
||||
|
@ -83,6 +83,16 @@ def notify_user(url):
|
|||
call(['notify-send', url])
|
||||
|
||||
|
||||
def parse_clipboard(args):
|
||||
text = pyperclip.paste()
|
||||
if re.match(r'https?://', text):
|
||||
mirror_file(text)
|
||||
elif os.path.isfile(text):
|
||||
upload_local_file(text)
|
||||
else:
|
||||
upload_text(text)
|
||||
|
||||
|
||||
def mirror_file(text):
|
||||
os.chdir(config.local_directory)
|
||||
call(['wget', text])
|
||||
|
@ -98,28 +108,25 @@ def upload_text(text):
|
|||
with open(os.path.join(config.local_directory, filename), 'w') as file:
|
||||
file.write(text)
|
||||
url = upload_local_file(os.path.join(config.local_directory, filename))
|
||||
os.remove(os.path.join(config.local_directory, filename))
|
||||
#os.remove(os.path.join(config.local_directory, filename))
|
||||
pyperclip.copy(url)
|
||||
notify_user(url)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
args = parse_arguments()
|
||||
if args.mode == 'screenshot':
|
||||
ext = 'png'
|
||||
elif args.files is not None:
|
||||
for file in args.files:
|
||||
upload_local_file(file)
|
||||
elif args.mode == 'text':
|
||||
text = pyperclip.paste()
|
||||
if re.match(r'https?://', text):
|
||||
mirror_file(text)
|
||||
elif os.path.isfile(text):
|
||||
upload_local_file(text)
|
||||
|
||||
if config.uploader in ['ftp', 'sftp']:
|
||||
if args.files is not None:
|
||||
for file in args.files:
|
||||
upload_local_file(file)
|
||||
elif args.mode == 'text':
|
||||
parse_clipboard(args)
|
||||
else:
|
||||
upload_text(text)
|
||||
|
||||
"""
|
||||
ftp_upload(mode=args.mode)
|
||||
"""
|
||||
elif args.files is not None:
|
||||
|
||||
if config.uploader in ['ftp', 'sftp']:
|
||||
if mode != 'screenshot' and '.' in file:
|
||||
ext = '.' + file.rsplit('.', 1)[1]
|
||||
|
|
Loading…
Reference in New Issue
Block a user