gett-cli / uploader.py

#!/usr/bin/env python

import sys, os, os.path
import argparse
import signal

from gett import *
from time import sleep

signal.signal(signal.SIGINT, signal.SIG_DFL)

home = os.getenv('USERPROFILE') or os.getenv('HOME')

parser = argparse.ArgumentParser(description="A simple ge.tt uploader")
parser.add_argument('file', nargs='+', type=argparse.FileType('rb'), help="Name of the file to upload")
parser.add_argument('-t', dest='title', help='Title of the share (defaults to first file name)')
parser.add_argument('-s', dest='share', help='Name of the share to upload to (defaults to a newly created one)')
parser.add_argument('-e', dest='email', help='Email to login with')
parser.add_argument('-p', dest='password', help='Password to login with')
parser.add_argument('-k', dest='tokenfile', help='Ge.tt token file path (~/.gett-token)', default=os.path.join(home, '.gett-token'))

args = parser.parse_args()

user = User()
try:
    # Try to log-in with the token

    token = open(args.tokenfile, 'r').read()
    user.login_token(token)
except:
    # If it doesn't work, ask the user its email and password

    if not args.email:
        args.email = input('Please enter your Ge.tt email : ')

    if not args.password:
        import getpass
        args.password = getpass.getpass('Please enter your Ge.tt password : ')

    try:
        user.login_auth(args.email, args.password)
    except Exception as ex:
        print('Unable to login : %s' % ex.args[0])
        sys.exit(1)

with open(args.tokenfile, 'w') as file:
    file.write(user.rtoken)

if args.share:
    share = user.get_share(args.share)
else:
    if args.title:
        share = user.create_share(args.title)
    else:
        share = user.create_share(os.path.basename(args.file[0].name))


uploads = []

for fp in args.file:
    name = os.path.basename(fp.name)
    file = share.create_file(name)

    print('%s : %s' % (name, file.url))

    upload = FileUpload(file, fp)
    upload.start()
    uploads.append(upload)

print()

uploads.sort(key=lambda item: item.file_size)

def print_status(upload):
    name = upload.file.name

    if len(name) > 15:
        name = name[:12] + '...'

    name = name.ljust(15)

    bar_size = int(50 * upload.percent_done / 100)
    bar = '[' + (bar_size * '#') + ((50 - bar_size) * '-') + ']'

    sys.stderr.write('\r%s %s %d %%' % (name, bar, upload.percent_done))

for upload in uploads:
    while True:
        print_status(upload)
        upload.join(0.5)

        if not upload.is_alive():
            print_status(upload)

            if upload.ex:
                sys.stderr.write(('\rError uploading %s : %s' % (upload.file.name, upload.ex)).ljust(80))

            sys.stderr.write('\n')
            break
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.