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