Commits

mickael9 committed 0c9f78e

Added new features to downloader

- Ability to delete a file or a share, by giving its name or URL
- Added debug switch (-D)
- Display the file index and count along with the progress bar
- Display the storage usage at the end of the program

  • Participants
  • Parent commits f05e87e

Comments (0)

Files changed (1)

 import sys, os, os.path
 import argparse
 import signal
+import re
 
 from gett import *
 from time import sleep
 
+url_re = re.compile(r'^(?:http://ge\.tt/|/)?(\w+)(?:/(?:v/(\d+)))?$')
+
 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('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'))
+parser.add_argument('-D', dest='debug', action='store_true', help='Debug API calls')
+parser.add_argument('--delete', nargs=1, dest='share_or_file', help='Delete a share or a file (url or path)')
 
 args = parser.parse_args()
 
+if args.debug:
+    import gett
+    gett.DEBUG = True
+
 user = User()
 try:
     # Try to log-in with the token
     # 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 : ')
+        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 : ')
+        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])
+        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)
+if not args.share_or_file:
+    args.share_or_file = []
+
+for item in args.share_or_file:
+    match = url_re.match(item)
+
+    if not match:
+        parser.error('argument --delete: invalid format, please supply file/share url or path')
+
+    share = user.get_share(match.group(1))
+
+    if match.group(2):
+        id = match.group(2)
+        file = share.files[id]
+        file.destroy()
     else:
-        share = user.create_share(os.path.basename(args.file[0].name))
+        share.destroy()
 
+if args.file:
+    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)
+    uploads = []
 
-    print('%s : %s' % (name, file.url))
+    for fp in args.file:
+        name = os.path.basename(fp.name)
+        file = share.create_file(name)
 
-    upload = FileUpload(file, fp)
-    upload.start()
-    uploads.append(upload)
+        print('%s: %s' % (name, file.url))
 
-print()
+        upload = FileUpload(file, fp)
+        upload.start()
+        uploads.append(upload)
 
-uploads.sort(key=lambda item: item.file_size)
+    print()
 
-def print_status(upload):
-    name = upload.file.name
+    uploads.sort(key=lambda item: item.file_size)
 
-    if len(name) > 15:
-        name = name[:12] + '...'
+    def print_status(upload, index, count):
+        name = upload.file.name
 
-    name = name.ljust(15)
+        if len(name) > 15:
+            name = name[:12] + '...'
 
-    bar_size = int(50 * upload.percent_done / 100)
-    bar = '[' + (bar_size * '#') + ((50 - bar_size) * '-') + ']'
+        name = name.ljust(15)
 
-    sys.stderr.write('\r%s %s %d %%' % (name, bar, upload.percent_done))
+        bar_size = int(50 * upload.percent_done / 100)
+        bar = '[' + (bar_size * '#') + ((50 - bar_size) * '-') + ']'
 
-for upload in uploads:
-    while True:
-        print_status(upload)
-        upload.join(0.5)
+        sys.stderr.write('\r%s (%d/%d) %s %d %%' % (name, index, count, bar, upload.percent_done))
 
-        if not upload.is_alive():
-            print_status(upload)
+    for i, upload in enumerate(uploads):
+        while True:
+            print_status(upload, i + 1, len(uploads))
+            upload.join(0.5)
 
-            if upload.ex:
-                sys.stderr.write(('\rError uploading %s : %s' % (upload.file.name, upload.ex)).ljust(80))
+            if not upload.is_alive():
+                print_status(upload, i + 1, len(uploads))
 
-            sys.stderr.write('\n')
-            break
+                if upload.ex:
+                    sys.stderr.write(('\rError uploading %s: %s' % (upload.file.name, upload.ex)).ljust(80))
 
+                sys.stderr.write('\n')
+                break
+
+user.refresh()
+
+def humanize(nbytes):
+    if nbytes >= 10**9:
+        return '%.2f GB' % (nbytes / 10**9)
+    if nbytes >= 10**6:
+        return '%.2f MB' % (nbytes / 10**6)
+    if nbytes >= 10**3:
+        return '%.2f KB' % (nbytes / 10**3)
+
+    return '%.2f B' % nbytes
+
+print('Storage used : %s out of %s (%d %%)' % (
+        humanize(user.storage_used),
+        humanize(user.storage_limit),
+        int(100 * user.storage_used / user.storage_limit)))