This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

boxupload 10/18/12 5:00 PM EST

By: a guest on Oct 18th, 2012  |  syntax: Python  |  size: 5.52 KB  |  views: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. # Box Upload
  3. # This code accepts arguments to upload files.
  4.  
  5. # WARNING!
  6. # This code does not check SSL certificate validity!
  7. # TODO: Implement certificate validation.
  8.  
  9. # Get the API key by making a Box application here: http://box.com/developers/services/edit/
  10. api_key = 'replace-with-api-key'
  11.  
  12. import os
  13. import sys
  14. import urllib
  15. import urllib2
  16. import MultipartPostHandler, cookielib # MultipartPostHandler can be found at: http://pypi.python.org/pypi/MultipartPostHandler2
  17. from xml.dom import minidom
  18.  
  19. def authenticate(api_key):
  20.         xml_ticket = minidom.parseString(urllib.urlopen('https://www.box.com/api/1.0/rest?action=get_ticket&api_key='+api_key).read()) # This grabs the XML authentication ticket that we'll be using to generate the auth_token.
  21.         status = xml_ticket.firstChild.childNodes[0].firstChild.data # This gets the ticket status to make sure we actually retrieved a valid ticket.
  22.         if status == 'get_ticket_ok': # Did we get a valid ticket? If so, continue. If not, the user needs to check their API key.
  23.                 ticket = xml_ticket.firstChild.childNodes[1].firstChild.data
  24.                 print 'Please go to https://www.box.com/api/1.0/auth/'+ticket+' and follow the instructions there. Then, press ENTER or RETURN to continue.'
  25.                 raw_input()
  26.                 xml_auth_token = minidom.parseString(urllib.urlopen('https://www.box.net/api/1.0/rest?action=get_auth_token&api_key='+api_key+'&ticket='+ticket).read()) # This is the auth token in XML format.
  27.                 auth_status = xml_auth_token.firstChild.childNodes[0].firstChild.data # Reads the authentication status into "auth_status"
  28.                 if auth_status == 'get_auth_token_ok':
  29.                         auth_token = xml_auth_token.firstChild.childNodes[1].firstChild.data
  30.                         token_path = os.path.expanduser('~/.box_auth_token')
  31.                         auth_token_file = open(token_path,'w')
  32.                         auth_token_file.write(auth_token)
  33.                         auth_token_file.close()
  34.                         print 'The authentication token has been saved in the file "'+token_path+'". If you remove this file, you will need to reauthenticate.'
  35.                         print ''
  36.                         print 'Please run "boxupload" again to initiate your upload.'
  37.                         return
  38.                 elif auth_status == 'not_logged_in':
  39.                         print "I'm sorry, but you haven't authorized me to connect to your account. Please run \"boxupload\" again to restart the authentication process."
  40.                 elif auth_status == 'get_auth_token_error':
  41.                         print "I'm sorry, but there was an error in retrieving your authentication token. Please run \"boxupload\" again to restart the authentication process."
  42.                 else:
  43.                         print "Error: "+auth_status+". Try reauthenticating."
  44.         else:
  45.                 print 'Error retrieving ticket! Check your API key.'
  46.  
  47. def get_account_info(api_key,auth_token):
  48.         xml_account_info = minidom.parseString(urllib.urlopen('https://www.box.net/api/1.0/rest?action=get_account_info&api_key='+api_key+'&auth_token='+auth_token).read())
  49.         status = xml_account_info.firstChild.childNodes[0].firstChild.data
  50.         if status == 'get_account_info_ok':
  51.                 login = str(xml_account_info.firstChild.childNodes[1].childNodes[0].firstChild.data)
  52.                 email = str(xml_account_info.firstChild.childNodes[1].childNodes[1].firstChild.data)
  53.                 access_id = int(xml_account_info.firstChild.childNodes[1].childNodes[2].firstChild.data)
  54.                 user_id = int(xml_account_info.firstChild.childNodes[1].childNodes[3].firstChild.data)
  55.                 space_amount = int(xml_account_info.firstChild.childNodes[1].childNodes[4].firstChild.data) # Total capacity of the account in bytes
  56.                 space_used = int(xml_account_info.firstChild.childNodes[1].childNodes[5].firstChild.data) # Data usage of the account in bytes
  57.                 max_upload_size = int(xml_account_info.firstChild.childNodes[1].childNodes[6].firstChild.data) # Maximum file upload size in bytes
  58.                 account_info = [login, email, access_id, user_id, space_amount, space_used, max_upload_size] # Turn the data into a list for easy access
  59.                 return account_info
  60.         elif status == 'not_logged_in':
  61.                 print "I'm sorry, your authentication token is invalid. Try removing your \"auth_token\" file and reauthenticating."
  62.         elif status == 'Wrong input':
  63.                 print "I'm sorry, the API key you are using is mistyped."
  64.  
  65. def upload(file,auth_token):
  66.         if not os.path.exists(file):
  67.                 print 'ERROR: File \"'+file+'\" was not found!'
  68.                 return
  69.         url = 'https://upload.box.net/api/1.0/upload/'+auth_token+'/0'
  70.         cookies = cookielib.CookieJar()
  71.         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), MultipartPostHandler.MultipartPostHandler)
  72.         params = { "file" : open(file, "rb") }
  73.         xml_response = minidom.parseString(opener.open(url, params).read())
  74.         upload_status = xml_response.firstChild.childNodes[0].firstChild.data
  75.         if upload_status == 'upload_ok':
  76.                 print "Upload successful!"
  77.         else:
  78.                 print "Upload failure!"
  79.  
  80. try: # Check for and read auth_token file
  81.         token_path = os.path.expanduser('~/.box_auth_token')
  82.         auth_token_file = open(token_path,'r')
  83.         auth_token = auth_token_file.read()
  84.         pass
  85. except IOError as e:
  86.         authenticate(api_key) # If there's no auth_token, initiate authentication.
  87.         sys.exit(1)
  88.  
  89. if len(sys.argv) < 2:
  90.     print "usage: "+sys.argv[0]+" file_1 [file_2] [file_3] ... [file_n]"
  91.     sys.exit(1)
  92.  
  93. # account_info = get_account_info(api_key,auth_token)
  94.  
  95. ''' # Uncomment for debugging.
  96. print 'Auth token: ' + auth_token
  97. print 'Max upload size: ' + str((account_info[6])/1024.0/1024) + ' MB'
  98. print 'Total capacity: ' + str((account_info[4])/1024.0/1024) + ' MB | Space used: ' + str((account_info[5])/1024.0/1024) + ' MB | Space remaining: ' + str((account_info[4]-account_info[5])/1024.0/1024) + ' MB | Percent free: ' + str(100.0*(account_info[4]-account_info[5])/account_info[4]) + '%'
  99. '''
  100.  
  101. arguments = sys.argv
  102. arguments.pop(0)
  103.  
  104. for argv in arguments:
  105.         upload(argv,auth_token)
clone this paste RAW Paste Data