diff --git a/geturl b/geturl index 34142c2..d3a4b6b 100755 --- a/geturl +++ b/geturl @@ -6,34 +6,31 @@ try: except ImportError: from check_output import check_output +import ConfigParser import json -import sys import os -import ConfigParser +import sys +import urllib -####################### -# Constants -FPURL = "https://www.filepicker.io" -FPAPIURL = "https://developers.filepicker.io" +# Constants. +FPURL = 'https://www.filepicker.io' +FPAPIURL = 'https://developers.filepicker.io' CONFIG_FILE = os.path.expanduser('~/.geturl') -####################### -# Loading Config -APIKEY = "" +# Loading Config. +APIKEY = '' config = ConfigParser.ConfigParser() config.read(CONFIG_FILE) if config.has_option('filepicker', 'apikey'): APIKEY = config.get('filepicker', 'apikey') -####################### -# Make sure we have curl +# Make sure curl is installed. try: - check_output('curl --help > /dev/null', shell=True) + check_output('which curl > /dev/null', shell=True) except CalledProcessError: - exit("`curl` is required. Please install it") + exit('`curl` is required. Please install it') -####################### -# Find or register for an Filepicker.io account +# Find or register a filepicker.io account. if not APIKEY: email = '' while not email: @@ -47,42 +44,45 @@ if not APIKEY: config.set('filepicker', 'apikey', APIKEY) config.write(open(CONFIG_FILE, 'w+')) -####################### -# Usage if no right number of args -if len(sys.argv) != 2: - print "usage: geturl " - exit() +# Show usage if incorrect number of args or not a file. +filename = sys.argv[1] +try: + f = open(filename, 'r') + if len(sys.argv) != 2 or type(f) != file: # hasattr(f, 'read') is more Pythonic + print 'usage: geturl ' + exit() +except IOError: + print 'usage: geturl ' -####################### -# Upload the file -print -print "Uploading the file to Filepicker.io..." -output = check_output('curl --progress-bar -F "fileUpload=@%(filename)s" -F "apikey=%(apikey)s" %(fpurl)s/api/path/storage/%(filename)s' % - {"filename": sys.argv[1], "apikey": APIKEY, "fpurl": FPURL}, shell=True) +# Upload the file. +print '\nUploading file to Filepicker.io...' +#safe_filename = urllib.pathname2url(filename) +safe_filename = urllib.quote_plus(filename) +curl_command = ('curl --progress-bar -F "fileUpload=@{0}" -F "apikey={1}" ' + '{2}/api/path/storage/{0}').format(filename, APIKEY, FPURL) +output = check_output(curl_command, shell=True) +#output = check_output('curl --progress-bar -F "fileUpload=@%(filename)s" -F "apikey=%(apikey)s" %(fpurl)s/api/path/storage/%(filename)s' % {"filename": filename, "apikey": APIKEY, "fpurl": FPURL}, shell=True) +# curl --progress-bar -F "fileUpload=@foo bar - hello_world.txt" -F "apikey=A8yuJ4aohTym_BH_TB07Lz" https://www.filepicker.io/api/path/storage/foo bar - hello_world.txt +# output = {"data": [{"url": "https://www.filepicker.io/api/file/GGsGQM4BRHW-cKVkK4vr", "data": {"size": 0, "type": "text/plain", "filename": "test.txt"}}], "result": "ok"} data = json.loads(output) url = data['data'][0]['url'] -print -print "A Public Share URL for: %s" % sys.argv[1] -print url -print +print '\nA public URL for: {}\n{}\n'.format(filename, url) -####################### -# Copy to the clipboard +# Copy to the clipboard. try: if sys.platform == 'darwin': check_output('echo "%s" | pbcopy' % (url), shell=True) - print "Also in your clipboard" + print 'Link also in your clipboard.' elif sys.platform == 'linux2': try: check_output('echo "%s" | xclip -selection clipboard > /dev/null 2>&1' % (url), shell=True) - print "Also in your clipboard" + print 'Link also in your clipboard.' except CalledProcessError: try: check_output('echo "%s" | xsel -b -i > /dev/null 2>&1' % (url), shell=True) - print "Also in your clipboard" + print 'Link also in your clipboard' except CalledProcessError: pass except Exception: pass -