2017-10-14 12:03:36 +02:00
#!/usr/bin/env python
from string import ascii_letters , digits
2017-11-14 22:46:54 +01:00
from argparse import ArgumentParser
from pysftp import Connection
from subprocess import call
2017-10-14 12:03:36 +02:00
from random import choices
2017-11-14 22:46:54 +01:00
import pyperclip
2017-11-15 14:12:51 +01:00
import config
2017-11-14 22:46:54 +01:00
import sys
import os
import re
2017-10-14 12:03:36 +02:00
character_pool = ascii_letters + digits
2017-11-20 15:44:42 +01:00
def parse_arguments ( ) :
parser = ArgumentParser ( )
parser . add_argument ( ' -m ' ' --mode ' , type = str ,
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 ' )
return parser . parse_args ( )
2017-10-14 12:03:36 +02:00
2017-10-25 14:54:58 +02:00
def generate_filename ( length , ext , prefix = ' ' ) :
2017-10-18 14:38:36 +02:00
return prefix + ' ' . join ( choices ( character_pool , k = length ) ) + ' . ' + ext
2017-10-14 12:03:36 +02:00
2017-10-25 14:22:58 +02:00
2017-11-06 06:29:29 +01:00
def find_valid_filename ( prefix , length , ext , conn ) :
2017-11-15 10:06:45 +01:00
filename = generate_filename ( prefix = prefix , length = length , ext = ext )
2017-10-16 15:19:57 +02:00
i = 0
while conn . exists ( filename ) :
2017-11-15 10:06:45 +01:00
filename = generate_filename ( length = length , ext = ext , prefix = prefix )
2017-10-16 15:19:57 +02:00
i + = 1
if i > 1000 :
2017-10-25 14:22:58 +02:00
# completely, definitely, totally justified recursion... yay?
2017-11-15 10:06:45 +01:00
return find_valid_filename ( prefix , length + 1 , ext , conn )
return filename
2017-10-14 12:03:36 +02:00
2017-11-06 06:29:29 +01:00
def upload_local_file ( path : str ) - > str :
2017-11-20 15:03:59 +01:00
if config . uploader in [ ' ftp ' , ' sftp ' ] :
filename = ftp_upload ( path ) [ 1 ]
return config . url_template . format ( filename )
else :
return curl_upload ( path )
2017-10-14 12:03:36 +02:00
2017-11-15 20:27:55 +01:00
def take_screenshot ( ) - > None :
tmppath = os . path . join ( config . local_directory , ' tmp ' )
2017-11-22 09:49:00 +01:00
if not os . path . isdir ( tmppath ) :
os . mkdir ( tmppath )
else :
tmpdir = os . listdir ( tmppath )
for f in tmpdir :
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 )
2017-11-15 20:27:55 +01:00
ftp_upload ( ext = ' png ' , sourcefile = file )
os . remove ( file )
2017-11-20 15:03:59 +01:00
def ftp_upload ( sourcefile , * , mode = None , ext = None ) - > tuple :
2017-11-06 06:29:29 +01:00
if ext is None :
2017-11-15 14:12:51 +01:00
# TODO files without extension
2017-11-15 11:15:04 +01:00
exts = {
2017-11-15 10:06:45 +01:00
' screenshot ' : ' png ' ,
' text ' : ' txt ' ,
2017-11-15 11:15:04 +01:00
}
ext = exts . get ( mode , mode not in exts and sourcefile . split ( ' . ' ) [ - 1 ] ) # Only do the split if necessary
2017-10-16 15:19:57 +02:00
2017-11-14 22:46:54 +01:00
with Connection ( config . sftp_address , username = config . username , password = config . password ,
2017-11-20 15:03:59 +01:00
private_key = config . private_key , private_key_pass = config . private_key_pass ) as conn :
2017-11-14 22:46:54 +01:00
conn . chdir ( config . remote_directory )
2017-11-15 10:06:45 +01:00
filename = find_valid_filename ( prefix = config . prefix , length = config . length , ext = ext , conn = conn )
2017-11-14 22:46:54 +01:00
fullpath = os . path . join ( config . local_directory , filename )
2017-10-16 15:37:24 +02:00
2017-11-15 20:27:55 +01:00
if mode == ' file ' :
2017-11-14 22:46:54 +01:00
conn . put ( sourcefile , filename )
2017-11-15 14:12:51 +01:00
notify_user ( config . url_template . format ( filename ) )
2017-10-18 14:38:36 +02:00
2017-10-16 15:37:24 +02:00
return fullpath , filename
2017-10-18 14:38:36 +02:00
def curl_upload ( filename ) :
2017-11-20 15:03:59 +01:00
return call ( config . custom_curl_command )
2017-10-25 14:22:58 +02:00
2017-10-18 14:38:36 +02:00
2017-10-16 15:37:24 +02:00
def notify_user ( url ) :
print ( url )
2017-11-15 11:15:04 +01:00
pyperclip . copy ( url )
2017-11-14 22:46:54 +01:00
call ( [ ' notify-send ' , url ] )
2017-10-16 15:37:24 +02:00
2017-11-20 15:44:42 +01:00
def parse_text ( args ) :
2017-11-15 10:06:45 +01:00
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 )
2017-10-25 14:54:58 +02:00
def mirror_file ( text ) :
os . chdir ( config . local_directory )
call ( [ ' wget ' , text ] )
2017-11-06 06:29:29 +01:00
filename = text . rsplit ( ' / ' , 1 ) [ 1 ]
2017-10-25 14:54:58 +02:00
url = upload_local_file ( os . path . join ( config . local_directory , filename ) )
os . remove ( os . path . join ( config . local_directory , filename ) )
notify_user ( url )
def upload_text ( text ) :
filename = generate_filename ( config . length , ' txt ' )
with open ( os . path . join ( config . local_directory , filename ) , ' w ' ) as file :
file . write ( text )
2017-11-06 06:29:29 +01:00
url = upload_local_file ( os . path . join ( config . local_directory , filename ) )
2017-11-15 11:15:04 +01:00
os . remove ( os . path . join ( config . local_directory , filename ) )
2017-11-06 06:29:29 +01:00
notify_user ( url )
2017-10-25 14:54:58 +02:00
2017-10-16 15:37:24 +02:00
if __name__ == ' __main__ ' :
2017-11-20 15:44:42 +01:00
args = parse_arguments ( )
if args . mode == ' clipboard ' :
parse_text ( pyperclip . paste ( ) )
elif args . mode == ' screenshot '
2017-11-20 15:03:59 +01:00
take_screenshot ( )
else :
2017-11-20 15:44:42 +01:00
for file in args . files :
upload_local_file ( file )
2017-11-15 10:06:45 +01:00
2017-11-20 15:03:59 +01:00
"""
2017-11-15 10:06:45 +01:00
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 )
2017-10-25 14:54:58 +02:00
else :
2017-11-15 20:27:55 +01:00
take_screenshot ( )
2017-11-20 15:03:59 +01:00
2017-11-15 10:06:45 +01:00
elif args . files is not None :
2017-10-18 14:38:36 +02:00
if config . uploader in [ ' ftp ' , ' sftp ' ] :
if mode != ' screenshot ' and ' . ' in file :
ext = ' . ' + file . rsplit ( ' . ' , 1 ) [ 1 ]
# TODO: mode file for FTP
fullpath , filename = ftp_upload ( mode , ext )
elif config . uploader == ' curl ' :
if mode == ' screenshot ' :
filename = generate_filename ( length = config . length , ext = ' .png ' )
fullpath = os . path . join ( config . local_directory , filename )
take_screenshot ( fullpath )
else :
fullpath = file
curl_upload ( fullpath )
else :
print ( ' Unknown mode ' )
sys . exit ( - 1 )
2017-10-16 15:37:24 +02:00
url = config . url_template . format ( filename )
notify_user ( url )
2017-10-25 14:22:58 +02:00
"""