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
2018-03-27 11:31:37 +02:00
from subprocess import call , check_output
2018-07-18 09:37:10 +02:00
from collections import namedtuple
2017-10-14 12:03:36 +02:00
from random import choices
2018-03-27 12:21:53 +02:00
from datetime import date
2017-11-22 11:38:34 +01:00
from PIL import Image
2017-11-14 22:46:54 +01:00
import pyperclip
2018-03-27 12:23:42 +02:00
import config
2018-07-18 09:37:10 +02:00
import time
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
2018-01-22 22:31:48 +01:00
2017-11-20 15:44:42 +01:00
def parse_arguments ( ) :
parser = ArgumentParser ( )
2018-01-22 22:50:23 +01:00
parser . add_argument ( ' -m ' ' --mode ' , type = str , dest = ' mode ' , default = None ,
2018-03-27 12:21:53 +02:00
help = ' Sets the input mode. Allowed values are " screenshot " and " clipboard " . Implicit it if file(s) are set. ' )
2018-01-22 22:50:23 +01:00
parser . add_argument ( ' -f ' , ' --files ' , type = str , nargs = ' * ' , dest = ' files ' , help = ' List of files to be uploaded ' , default = None )
2018-01-22 23:20:35 +01:00
parser . add_argument ( ' -e ' , ' --edit ' , type = bool , dest = ' edit ' , default = False , help = ' Open the screenshot in gimp to edit it before uploading ' )
2017-11-20 15:44:42 +01:00
return parser . parse_args ( )
2017-10-14 12:03:36 +02:00
2018-04-01 18:08:59 +02:00
def generate_filename ( length : int , ext : str ) - > str :
2018-03-27 12:21:53 +02:00
filename = config . prefix + ' ' . join ( choices ( character_pool , k = length ) ) + ' . ' + ext
return filename
2018-04-01 18:08:59 +02:00
def get_local_full_path ( ) - > str :
2018-03-27 12:21:53 +02:00
if config . local_directory_nesting :
folder = get_date_folder ( )
return os . path . join ( config . local_directory , folder )
return config . local_directory
2018-04-01 18:08:59 +02:00
def get_date_folder ( ) - > str :
2018-03-27 12:21:53 +02:00
return date . today ( ) . strftime ( config . local_directory_nesting )
2017-10-14 12:03:36 +02:00
2017-10-25 14:22:58 +02:00
2018-04-01 18:08:59 +02:00
def upload_local_file ( path : str ) - > None :
2017-11-20 15:03:59 +01:00
if config . uploader in [ ' ftp ' , ' sftp ' ] :
2018-04-01 17:42:51 +02:00
filename = ftp_upload ( path )
if config . preserve_folders_on_remote :
filename = os . path . join ( get_date_folder ( ) , filename )
2018-04-01 18:08:59 +02:00
url = config . url_template . format ( filename )
2017-11-20 15:03:59 +01:00
else :
2018-04-01 18:08:59 +02:00
url = curl_upload ( path )
2018-08-21 17:57:04 +02:00
# Only pass the path when uploading an image
if re . search ( r ' \ .(png|jpe?g|bmp)$ ' , path ) :
notify_user ( url , path )
else :
notify_user ( url )
2017-10-14 12:03:36 +02:00
2018-07-18 09:37:10 +02:00
def prepare_file ( ext : str ) - > str :
" Generate a file name according to the config and create folder structure if necessary "
2018-03-27 17:26:57 +02:00
full_path = get_local_full_path ( )
if not os . path . exists ( full_path ) :
os . makedirs ( full_path )
2018-07-18 09:37:10 +02:00
tempname = generate_filename ( config . length , ext )
return os . path . join ( get_local_full_path ( ) , tempname )
def take_screenshot ( edit = False ) - > None :
file = prepare_file ( ' png ' )
2018-03-27 11:19:18 +02:00
call ( [ ' maim ' , ' -suk ' , file ] )
2018-01-23 00:24:03 +01:00
Image . open ( file ) . convert ( ' RGB ' ) . save ( file )
2018-01-22 23:20:35 +01:00
if edit :
call ( [ ' gimp ' , file ] )
2018-04-01 18:08:59 +02:00
upload_local_file ( file )
2018-01-22 22:31:48 +01:00
if not config . keep_local_copies :
os . remove ( file )
2017-11-15 20:27:55 +01:00
2018-04-01 16:55:56 +02:00
def get_extension ( filename : str ) - > str :
"""
Returns the extension of a file / full path as a string .
Emtpy if the file has no extension .
. tar . xx archives are handled accordingly .
"""
filename = os . path . basename ( filename )
if re . search ( ' \ .tar \ . \ w { 1,4} ' , filename ) :
num_exts = 2
else :
num_exts = 1
extension = ' . ' . join ( filename . split ( ' . ' ) [ - num_exts : ] )
return extension
def ftp_upload ( sourcefile : str ) - > str :
def prepare_remote_folder ( conn ) - > None :
" Create the necessary folder(s) on the remote server and change the directory accordingly "
if config . preserve_folders_on_remote :
full_remote_dir = os . path . join ( config . remote_directory , get_date_folder ( ) )
else :
full_remote_dir = config . remote_directory
if not conn . exists ( full_remote_dir ) :
conn . makedirs ( full_remote_dir )
conn . chdir ( full_remote_dir )
extension = get_extension ( sourcefile )
with Connection ( config . sftp_address , username = config . username , password = config . password , port = config . sftp_port ,
private_key = config . private_key , private_key_pass = config . private_key_pass ) as conn :
prepare_remote_folder ( conn )
extension = get_extension ( sourcefile )
dest_name = generate_filename ( config . length , extension )
while conn . exists ( dest_name ) :
dest_name = generate_filename ( config . length , extension )
conn . put ( sourcefile , dest_name )
return dest_name
2018-04-01 18:08:59 +02:00
def curl_upload ( filename : str ) - > str :
2018-03-27 11:31:37 +02:00
return check_output ( config . curl_command . format ( filename ) , shell = True ) . decode ( ) [ : - 1 ]
2017-10-25 14:22:58 +02:00
2017-10-18 14:38:36 +02:00
2018-04-01 18:08:59 +02:00
def notify_user ( url : str , image = None ) - > None :
2017-10-16 15:37:24 +02:00
print ( url )
2017-11-15 11:15:04 +01:00
pyperclip . copy ( url )
2018-03-27 12:21:53 +02:00
if config . enable_thumbnails and image :
2017-11-22 11:38:34 +01:00
img = Image . open ( image )
img . thumbnail ( ( 384 , 384 ) , Image . ANTIALIAS )
thumbnail = os . path . join ( config . local_directory , ' thumb.jpg ' )
img . save ( thumbnail )
2018-01-23 00:24:03 +01:00
call ( [ ' notify-send ' , ' -a ' , ' pyshare ' , url , ' -i ' , thumbnail , ' -t ' , ' 3000 ' ] )
2018-07-18 09:37:10 +02:00
time . sleep ( 0.2 ) # delay slightly before deleting the file so notify-send can actually read it
2017-11-22 11:38:34 +01:00
os . remove ( thumbnail )
else :
2018-01-23 00:24:03 +01:00
call ( [ ' notify-send ' , ' -a ' , ' pyshare ' , url , ' -t ' , ' 3000 ' ] )
2017-10-16 15:37:24 +02:00
2018-05-07 10:51:29 +02:00
def parse_text ( text ) :
2018-01-22 22:31:48 +01:00
if re . match ( r ' (https?|s?ftp):// ' , text ) :
mirror_file ( text )
elif os . path . isfile ( text ) :
upload_local_file ( text )
else :
upload_text ( text )
2017-11-15 10:06:45 +01:00
2018-04-01 18:08:59 +02:00
def mirror_file ( text : str ) :
2017-10-25 14:54:58 +02:00
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 ) )
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-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 ( )
2018-01-22 22:50:23 +01:00
if args . mode is None :
if args . files is not None :
args . mode = ' files '
else :
args . mode = ' screenshot '
2018-01-22 22:31:48 +01:00
if args . mode == ' screenshot ' :
2018-01-22 23:20:35 +01:00
take_screenshot ( args . edit )
2018-01-22 22:52:35 +01:00
elif args . mode in ( ' clipboard ' , ' text ' , ' b ' ) :
2018-01-22 22:31:48 +01:00
parse_text ( pyperclip . paste ( ) )
2017-11-20 15:03:59 +01:00
else :
2017-11-20 15:44:42 +01:00
for file in args . files :
upload_local_file ( file )
2018-01-22 22:31:48 +01:00