Source

gett-cli / uploader.py

Full commit
#!/usr/bin/env python

import sys, os, os.path
import argparse

from gett import *
from time import sleep

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:
        args.password = input('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))

uploaders = []

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

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

    uploader = FileUploader(file, fp)
    uploader.start()
    uploaders.append(uploader)

finished = False

while not finished:
    finished = True

    for upl in uploaders:
        if not upl.is_alive():
            continue

        finished = False

        print('%s : %d %%' % (upl.file.name, upl.percent_done))

    sleep(1)

for upl in uploaders:
    upl.join()

print('Done !')