Esempio n. 1
0
File: app.py Progetto: ehfeng/dbxapi
def oauth_callback():
    '''Callback function for when the user returns from OAuth.'''
    access_token, uid, extras = get_flow().finish(request.args)
    client = DropboxClient(access_token)

    return render_template('done.html',
                           display_name=client.account_info()['display_name'])
def dropbox_upload(request):
    if request.method == 'POST':
        # POST-ed file, file name, path to upload to
        fupload = request.FILES['file']
        fname = fupload.name
        fsize = fupload.size
        path = '/' + fname

        # Make connection to dropbox API
        token = UserProfile.objects.get(user=request.user).dropbox_token
        client = DropboxClient(token)

        if fupload.multiple_chunks():  # If file is large enough to require chunked uploading
            uploader = client.get_chunked_uploader(fupload, fname)
            print 'uploading: ', fsize
            while uploader.offset < fsize:
                try:
                    upload = uploader.upload_chunked()
                except rest.ErrorResponse, e:
                    print 'ERROR WHILE UPLOADING CHUNKED: ', str(e)

            uploader.finish(path)
        else:
            response = client.put_file(path, fupload)
            print 'uploaded: ', response

        # print request.FILES
        return render(request, 'website/dropbox/fileview.html')
Esempio n. 3
0
    def setup_user(cls, request_key):
        from dropbox.client import DropboxClient

        request_token = OAuthRequestToken.get(request_key)

        session = cls.get_session()
        session.obtain_access_token(request_token)
        client = DropboxClient(session)
        account_info = client.account_info()

        uid = str(account_info['uid'])
        q = DropboxUser.all().filter('uid = ', uid)
        if q.count() == 0:
            user = DropboxUser(uid=uid,
                               email=account_info['email'],
                               display_name=account_info['display_name'],
                               access_key=session.token.key,
                               access_secret=session.token.secret,
                               country=account_info['country'])
        else:
            user = q.get()
            user.email = account_info['email']
            user.display_name = account_info['display_name']
            user.access_key = session.token.key
            user.access_secret = session.token.secret
            user.put_pending_away(_save=False)
            user.country = account_info['country']
        user.put()

        OAuthRequestToken.delete(request_key)

        return user
Esempio n. 4
0
def test(): 
    db = DropboxClient(session['dropbox_access_token'])
    fd = db.get('test_file.txt')
    if fd: 
        return fd.read()
    else: 
        return "nah nothing bro"
Esempio n. 5
0
def main():
    global client
    global datastore
    global geoData
    access_token = connect()
    
    client = DropboxClient(access_token)

    print 'linked account: ', client.account_info()["display_name"]

    manager = DatastoreManager(client)
    datastore = manager.open_default_datastore()
#     manager.delete_datastore("default")
        
    geoData = datastore.get_table('GeoData')

    
    app = MyApp(0)
    app.MainLoop()


#     folder_metadata = client.metadata('/GeoDrive')
# #     print 'metadata: ', folder_metadata["contents"]
#     for geoFile in folder_metadata["contents"]:
# #         print file
#         print geoFile["path"] + "\n\tclient_mtime:" + geoFile["client_mtime"] + "\n\tmodified:" + geoFile["modified"]
#         entry = geoData.query(path=geoFile["path"])
#         print entry

#     print "Distance: " + str(distance_on_earth(33.644277, -117.842026, 33.645147, -117.831879))

#     geoData.insert(path='/GeoDrive/Work.txt', lat=33.644277, long=-117.842026, lastLoc=Date())
#     geoData.insert(path='/GeoDrive/Home.txt', lat=33.645147, long=-117.831879, lastLoc=Date())
    datastore.commit()
Esempio n. 6
0
def sbox_upload(temp_fd, file_name): 
    """ Upload a file to Shoebox 

        This function calls the generate_padfile function in the otp class to
        generate a one time pad with the same length as the plaintext file. 
        It then encrypts the plaintext using the pad to create the ciphertext. 
        
        The key is then pushed to Dropbox, while the ciphertext is pushed
        to Google Drive. All intermediate data is stored as named temporary files,
        which are automatically deleted on close. 
    """

    # TODO: when uploading, hash the file names to something different 
    db = DropboxClient(session['dropbox_access_token'])
    gd = GDriveClient(session['google_creds'])
    otp = OneTimePad()

    """ hashing stuff 
    db_hash_obj = sha256(b"dropbox-" + file_name)
    db_name_hash = db_hash_obj.hexdigest()

    gd_hash_obj = sha256(b"gdrive-" + file_name)
    gd_name_hash = gd_hash_obj.hexdigest()

    db.put(temp_key.name, db_name_hash)
    db.put(temp_key.name, db_name_hash)
    """

    temp_key, temp_ct = otp.encrypt(temp_fd)
    db.put(temp_key.name, file_name)
    gd.put(temp_ct.name, file_name)

    temp_key.close()
    temp_ct.close()
class DropBox(BaseDrive):
    def __init__(self, token, rootPath):
        BaseDrive.__init__(self, token, rootPath)
        APP_KEY = '5a91csqjtsujuw7'
        APP_SECRET = 'x5wbkk2o273jqz7'
        session = DropboxSession(APP_KEY, APP_SECRET)
        print token
        access_key, access_secret = token.split(',')
        session.set_token(access_key, access_secret)
        first_client = DropboxClient(session)
        token1 = first_client.create_oauth2_access_token()
        self.client = DropboxClient(token1)
    def ls(self, path):
        folder_metadata = self.client.metadata(path)
        contents = folder_metadata['contents']
        files = []
        for content in contents:
            if content['is_dir']:
                files.append(MyFile(content['path'], True))
            else:
                files.append(MyFile(content['path'], False))
        return files
    def get(self, myfile, temp_filename):
        out = open(temp_filename, 'wb')
        f = self.client.get_file(myfile.path)
        out.write(f.read())
        out.close()
Esempio n. 8
0
def upload_chunked(file_path):
    """ Uploads a file in chucks to Dropbox, allowing it to resume on (connection) failure. """
    dropbox_settings = DropboxSettings.get_solo()
    file_name = os.path.split(file_path)[-1]

    # From Dropbox docs.
    retries = 3
    client = DropboxClient(dropbox_settings.access_token)

    size = os.stat(file_path).st_size
    file_handle = open(file_path, 'rb')

    uploader = client.get_chunked_uploader(file_handle, size)

    while uploader.offset < size:
        try:
            uploader.upload_chunked(chunk_size=1 * 1024 * 1024)
        except rest.ErrorResponse:   # pragma: no cover
            retries -= 1  # pragma: no cover

            if retries == 0:  # pragma: no cover
                raise IOError("Failed to upload to dropbox")  # pragma: no cover

    # This will commit the file and persist it in Dropbox. Due to rotating backups we MUST override.
    uploader.finish(file_name, overwrite=True)
Esempio n. 9
0
 def _upload(self, client: DropboxClient):
     """ Upload new files from directory.
         """
     now = time()
     for filename in listdir(self.directory):
         if fnmatch(filename, '*.upl'):
             continue
         local_name = '/' + filename
         full_name = path.join(self.directory, filename)
         upl_name = "{}.upl".format(full_name)
         if not path.isfile(upl_name) and stat(full_name).st_mtime < now - 60:
             with open(full_name, 'rb') as file_stream:
                 try:
                     client.put_file(local_name, file_stream)
                     share = client.share(local_name)
                 except (MaxRetryError, ErrorResponse):
                     continue
             with DatabaseConnection() as db:
                 update = """
                 UPDATE events
                    SET url = '{}',
                        uploaded = current_timestamp
                  WHERE file = '{}'
                 """.format(share['url'], full_name)
                 db.dml(update)
             try:
                 mknod(upl_name)
             except FileExistsError:
                 pass
             print("{} was uploaded to Dropbox.".format(filename))
Esempio n. 10
0
def upload(dropbox_helper_id, access_token, size, max_retries):
    from .models import DropboxUploadHelper
    helper = DropboxUploadHelper.objects.get(id=dropbox_helper_id)
    client = DropboxClient(access_token)
    retries = 0

    try:
        with open(helper.src, 'rb') as f:
            uploader = client.get_chunked_uploader(f, size)
            while uploader.offset < size:
                helper.progress = uploader.offset / size
                helper.save()
                try:
                    uploader.upload_chunked()
                except ErrorResponse, e:
                    if retries < helper.max_retries:
                        retries += 1
                    else:
                        helper.failure_reason = str(e)
                        helper.save()
                        raise e

            upload = uploader.finish(helper.dest)
    except Exception, e:
        helper.failure_reason = str(e)
        helper.save()
Esempio n. 11
0
    def auth_callback(self, user):
        # TODO: consider not using client library during auth flow
        try:
            access_token, dropbox_user_id, url_state = self.oauth_flow.finish(request.values)
        except (DropboxOAuth2Flow.NotApprovedException, DropboxOAuth2Flow.BadStateException):
            # 1) user cancelled and client library raised exc., or
            # 2) the state was manipulated, possibly due to time.
            # Either way, return and display info about how to properly connect.
            return
        except (DropboxOAuth2Flow.ProviderException, DropboxOAuth2Flow.CsrfException):
            raise HTTPError(http.FORBIDDEN)
        except DropboxOAuth2Flow.BadRequestException:
            raise HTTPError(http.BAD_REQUEST)

        self.client = DropboxClient(access_token)

        info = self.client.account_info()
        return self._set_external_account(
            user,
            {
                'key': access_token,
                'provider_id': info['uid'],
                'display_name': info['display_name'],
            }
        )
Esempio n. 12
0
def get_folder_tree_raw():
    """Returns the raw data retrieved from Drobox"""

    # Check authentication
    if 'user' not in session:
        return redirect(url_for('login'))
    access_token = get_access_token()
    #sizes = {}
    delta_metadatas = {}
    cursor = None
    if access_token is not None:
        client = DropboxClient(
            access_token)  #folder_metadata = client.metadata(path)
        #metadata = json.dumps(folder_metadata, sort_keys=True, indent=4, separators=(',', ': '))
        print("Fetching results...")
        i = 0
        while cursor is None or result['has_more']:
            i = i + 1
            print("Fetching result set %d." % i)
            result = client.delta(cursor)
            # delta_metadatas = parse_delta(result)
            for path, metadata in result['entries']:
                #sizes[path] = metadata['bytes'] if metadata else 0
                delta_metadatas[path] = metadata if metadata else None
            cursor = result['cursor']

            # Disable complete fetch
            result['has_more'] = False

    return jsonify(delta_metadatas)
Esempio n. 13
0
 def post(self):
     key = self.request.get('session_key')
     blob_info = self.request.get('blob_info')
     logging.info('Task Queues returns key: %s, blob_info: %s.' %(key, blob_info))
     session = SessionData.get(key)
     logging.info('Task Queues returns key: %s, blob_info: %s, retults in session: %s.' %(key, blob_info, session))
     client = DropboxClient(DB_TOKEN, "en_US", rest_client=None)
     if not session.presentation_uploaded_to_db:
         f = session.blob_store_key.open()
         size = session.blob_store_key.size
         uploader = client.get_chunked_uploader(f, size)
         while uploader.offset < size:
             try:
                 upload = uploader.upload_chunked()
             except:
                 logging.error("Drop Box Error")
         filename = session.lastname + '_' + session.filename
         if session.session_date: date = session.session_date
         else:  date = 'no-date-provided'
         
         response = uploader.finish('/beta/%s/%s/%s/%s'% (session.session_room, date, session.lastname, filename), overwrite = True) #folder structure /conf_name/room/date/lastname/filename
         session.presentation_uploaded_to_db = True
         session.presentation_db_path = response['mime_type']
         session.presentation_db_size = response['size']
         session.put()
         f.close()
     return
Esempio n. 14
0
class DBUpload:
    def __init__(self, key, secret):

        #See if I already have a stored access_token
        try:
            with open('.dbtoken.json', 'r') as json_data_file:
                  data = json.load(json_data_file)
            accessToken = data['accessToken']
        except:
            accessToken = None
        if accessToken is None:
            # connect to dropbox and start the session authorization process
            flow = DropboxOAuth2FlowNoRedirect(key, secret)
            logging.info("Authorize this application: {}".format(flow.start()))
            authCode = input("Enter auth code here: ").strip()

            # finish the authorization and grab the Dropbox client
            (accessToken, userID) = flow.finish(authCode)
            data = {'accessToken' : accessToken}
            with open('.dbtoken.json', 'w') as outfile:
                  json.dump(data, outfile)

        self.client = DropboxClient(accessToken)
        logging.info("dropbox account linked")

        self.Q = Queue()
        self.thread = Thread(target=self.pull_from_queue, args=())
        self.thread.daemon = True
        self.thread.start()

##        # setup the queue
##        q = Queue()
##        number_workers = 4
##
##        for i in range(number_workers):
##            t = Thread(target=pull_from_queue, q)
##            t.daemon = True
##            t.start()
##
##        while True:
##            time.sleep(1)

    def upload_file(self, source, target, timestamp):
        logging.info(" {}".format(timestamp))
        self.client.put_file(target, open(source, "rb"))
        # remove the file
        os.remove(source)

    def queue_file(self, source, target, timestamp):
        self.Q.put((source, target, timestamp))

    def pull_from_queue(self):
        while True:
            if not self.Q.empty():
                (source, target, timestamp) = self.Q.get()
                logging.info ("found item in queue" )
                self.upload_file(source,target, timestamp)

            else:
                time.sleep(1.0)
Esempio n. 15
0
def auth_finish(request):
    try:
        access_token, user_id, url_state = get_auth(request.session).finish(request.GET)
        client = DropboxClient(access_token)
        user = request.user
        client_email = client.account_info()["email"]
        uid = client.account_info()["uid"]
        name = client.account_info()["display_name"]

        try:
            formerAccount = DropboxAccount.objects.get(uid=uid)
            formerAccount.token = access_token
            formerAccount.display_name = name
            formerAccount.email = client_email
            formerAccount.save()
            new_account = False
        except ObjectDoesNotExist:
            new_account = True
            account = user.dropboxaccount_set.create(uid=uid, token=access_token, email=client_email, display_name=name)

        response = HttpResponseRedirect("/services")
        response.set_cookie("service_added", "dropbox", path="/")
        response.set_cookie("new_account", new_account, path="/")
        response.set_cookie("account_uid", uid, path="/")

        return response

    except DropboxOAuth2Flow.BadRequestException, e:
        http_status(400)
        info = "error404"
        return render(request, "dropbox.html", {"info": info})
def salvage_broken_user_settings_document(document):
    if not document['access_token'] or not document['dropbox_id']:
        return False
    if not document['owner'] or not User.load(document['owner']).is_active:
        return False
    if document['deleted']:
        return False
    if not document.get('dropbox_info') or not document['dropbox_info']['display_name']:
        logger.info(
            "Attempting dropbox_info population for document (id:{0})".format(document['_id'])
        )
        client = DropboxClient(document['access_token'])
        document['dropbox_info'] = {}
        try:
            database['dropboxusersettings'].find_and_modify(
                {'_id': document['_id']},
                {
                    '$set': {
                        'dropbox_info': client.account_info()
                    }
                }
            )
        except Exception:
            # Invalid token probably
            # Still want Dropbox to be enabled to show error message
            # to user on node settings, pass
            return True
        else:
            return True

    return False
Esempio n. 17
0
def decryptfile():        
    print "Decrypting file"
    # To avoid writing plaintext to any files, copy the response
    # data from Dropbox directly into a read/write in memory buffer
    if request.method == 'POST':
        infile = request.form['filename']
        bfirmid = base64.b64encode(request.form.get('firmid'))
        bclientid = base64.b64encode(request.form.get('clientid'))
        try:
            access_token = config.get('Credentials','access_token')
            dclient = DropboxClient(access_token)
            print "Requesting /%s" % infile
            httpresp = dclient.get_file("/%s" % infile)
            instream = io.BytesIO()
            inbuf = io.BufferedRandom(instream)
            inbuf.write(httpresp.read())
            inbuf.flush()
            inbuf.seek(0)
            result = getfile(inbuf,bfirmid,bclientid)
            print "done with getfile"
            
            result.seek(0,os.SEEK_END)
            print "Got %d bytes" % result.tell()
            result.seek(0)
            # Copy the decrypted data into a HTTP response object
            # to be returned to the user
            print "getting ready to return to response object"

            return Response(chunkfd(result,blocksize=4096),
                mimetype='application/octet-stream')
            
        except Exception as e:
            print e
Esempio n. 18
0
def get_folder_tree_raw():

    """Returns the raw data retrieved from Drobox"""

    # Check authentication
    if 'user' not in session:
        return redirect(url_for('login'))
    access_token = get_access_token()
    #sizes = {}
    delta_metadatas = {}
    cursor = None
    if access_token is not None:
        client = DropboxClient(access_token)    #folder_metadata = client.metadata(path)
        #metadata = json.dumps(folder_metadata, sort_keys=True, indent=4, separators=(',', ': '))
        print ("Fetching results...")
        i = 0
        while cursor is None or result['has_more']:
            i = i + 1
            print ("Fetching result set %d." % i)
            result = client.delta(cursor)
            # delta_metadatas = parse_delta(result)
            for path, metadata in result['entries']:
                #sizes[path] = metadata['bytes'] if metadata else 0
                delta_metadatas[path] = metadata if metadata else None
            cursor = result['cursor']

            # Disable complete fetch
            result['has_more'] = False

    return jsonify(delta_metadatas)
Esempio n. 19
0
def upload_welcome_pdf(dropbox_id):
    user = User.query.filter_by(dropbox_id=dropbox_id, active=True).first()
    if user is None:
        return False

    # If we've already sent the welcome PDF before, Dropbox webhook went
    # trigger, so do it here.
    if user.uploaded_welcome_pdf:
        return kindlebox(dropbox_id)

    analytics.track(str(user.id), 'Sent welcome pdf')

    client = DropboxClient(user.access_token)
    try:
        with open('app/static/bookdrop_welcome.pdf', 'rb') as f:
            response = client.put_file('Welcome to BookDrop.pdf',
                                       f,
                                       overwrite=True)
            if response:
                log.info(u"Welcome PDF sent to user ID {0}.".format(user.id))
            else:
                raise Exception(
                    "No response received after sending welcome PDF")

        user.set_uploaded_welcome_pdf()
        db.session.commit()

    except:
        log.error((u"Welcome PDF failed for user ID "
                   "{0}.").format(user.id),
                  exc_info=True)
        return False

    return True
Esempio n. 20
0
def multiuploader(request):
    if request.method == 'POST':

        user_profile = UserProfile.objects.get(user = request.user)
        drop_client = DropboxClient(user_profile.dropbox_profile.access_token['key'])

        files = request.FILES.getlist(u'files[]')
        for file in files:
            folder = Project.objects.get(id=request.POST['project_id']).title
            result_db = drop_client.put_file(settings.APP_NAME + '/' + folder + '/' + file.name, file.file)

        #generating json response array
        result = []
        result.append({"files": [
          {
            "name": result_db['path'][result_db['path'].rfind('/') + 1:],
            "size": result_db['bytes'],
            "mime": result_db['mime_type']
          }
        ]})
        response_data = simplejson.dumps(result)
        
        #checking for json data type
        if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
            mimetype = 'application/json'
        else:
            mimetype = 'text/plain'
        return HttpResponse(response_data, mimetype=mimetype)
    else: #GET
        return HttpResponse('Only POST accepted')
Esempio n. 21
0
def process_user(uid):
    '''Call /delta for the given user ID and process any changes.'''

    # OAuth token for the user
    token = redis_client.hget('tokens', uid)

    # /delta cursor for the user (None the first time)
    cursor = redis_client.hget('cursors', uid)

    client = DropboxClient(token)
    has_more = True

    while has_more:
        result = client.delta(cursor)

        for path, metadata in result['entries']:

            # Ignore deleted files, folders, and non-markdown files
            if (metadata is None or
                    metadata['is_dir'] or
                    not path.endswith('.md')):
                continue

            # Convert to Markdown and store as <basename>.html
            html = markdown(client.get_file(path).read())
            client.put_file(path[:-3] + '.html', html, overwrite=True)

        # Update cursor
        cursor = result['cursor']
        redis_client.hset('cursors', uid, cursor)

        # Repeat only if there's more to do
        has_more = result['has_more']
Esempio n. 22
0
  def post(self):
    user_email = self.session.get('user')
    user_obj = getUser(user_email)
    errors = []
    success = []

    user_sitename = self.request.get('Sitename', '')
    user_siteID = self.request.get('SiteID', '')

    if( user_siteID == "" ):
      errors.append("Don't forget to give siteID!")
      template_values = {'errors': '<br/>'.join(errors),'user': user_obj, 'addsite' : True}
      showIndex(self, template_values)
      return
    idobj = database.Mapping.all()
    idobj.filter("SiteID =", user_siteID)
    counter = idobj.count(limit=1)
    if (counter == 0):
      success.append("URL registered!")
      database.Mapping(Sitename = user_sitename, SiteID = user_siteID, user=user_obj).put()
      client = DropboxClient(user_obj.access_token)
      response = client.put_file('%s/index.html' % user_siteID, '<h1>Hello World</h1>')
      template_values = {'success': '<br/>'.join(success), 'user' : True}
      self.redirect('/home#banner')
    else:
      errors.append("ID already exist!! Try another :)")
      template_values = {'success': '<br/>'.join(success), 'errors': '<br/>'.join(errors), 'user' : True,'addsite' : True}
      self.redirect('/addsite#banner')
    showIndex(self, template_values)
Esempio n. 23
0
class DBUpload:
    def __init__(self, key, secret):

        #See if I already have a stored access_token
        try:
            with open('.dbtoken.json', 'r') as json_data_file:
                data = json.load(json_data_file)
            accessToken = data['accessToken']
        except:
            accessToken = None
        if accessToken is None:
            # connect to dropbox and start the session authorization process
            flow = DropboxOAuth2FlowNoRedirect(key, secret)
            logging.info("Authorize this application: {}".format(flow.start()))
            authCode = input("Enter auth code here: ").strip()

            # finish the authorization and grab the Dropbox client
            (accessToken, userID) = flow.finish(authCode)
            data = {'accessToken': accessToken}
            with open('.dbtoken.json', 'w') as outfile:
                json.dump(data, outfile)

        self.client = DropboxClient(accessToken)
        logging.info("dropbox account linked")

        self.Q = Queue()
        self.thread = Thread(target=self.pull_from_queue, args=())
        self.thread.daemon = True
        self.thread.start()

##        # setup the queue
##        q = Queue()
##        number_workers = 4
##
##        for i in range(number_workers):
##            t = Thread(target=pull_from_queue, q)
##            t.daemon = True
##            t.start()
##
##        while True:
##            time.sleep(1)

    def upload_file(self, source, target, timestamp):
        logging.info(" {}".format(timestamp))
        self.client.put_file(target, open(source, "rb"))
        # remove the file
        os.remove(source)

    def queue_file(self, source, target, timestamp):
        self.Q.put((source, target, timestamp))

    def pull_from_queue(self):
        while True:
            if not self.Q.empty():
                (source, target, timestamp) = self.Q.get()
                logging.info("found item in queue")
                self.upload_file(source, target, timestamp)

            else:
                time.sleep(1.0)
Esempio n. 24
0
def sync():
    if 'user' not in session:
        return redirect(url_for('login'))

    access_token = get_access_token()
    print(access_token)
    real_name = ''

    if access_token is not None:
        client = DropboxClient(access_token)
        gallery_items = client.metadata('/gallery')
        for idx, item in enumerate(gallery_items['contents']):
            if item['is_dir']:
                pass
            else:
                name = get_image_name(item['path'], '/gallery/')

                f, metadata = client.get_file_and_metadata(item['path'])

                make_dir('%s/%s' % (GAL_PATH, name))

                for lang in LANGUAGES:
                    make_dir('%s/%s/%s' % (GAL_PATH, name, lang))

                create_file('%s/%s.jpg' % (GAL_PATH, name), f.read())

                for lang in LANGUAGES:
                    create_file(
                        '%s/%s/%s/%s' % (GAL_PATH, name, lang, 'title.txt'),
                        '')

    return redirect(url_for('admin.dashboard'))
Esempio n. 25
0
def upload(dropbox_helper_id, access_token, size, max_retries):
    from .models import DropboxUploadHelper
    helper = DropboxUploadHelper.objects.get(id=dropbox_helper_id)
    client = DropboxClient(access_token)
    retries = 0

    try:
        with open(helper.src, 'rb') as f:
            uploader = client.get_chunked_uploader(f, size)
            while uploader.offset < size:
                helper.progress = uploader.offset / size
                helper.save()
                try:
                    uploader.upload_chunked()
                except ErrorResponse, e:
                    if retries < helper.max_retries:
                        retries += 1
                    else:
                        helper.failure_reason = str(e)
                        helper.save()
                        raise e

            upload = uploader.finish(helper.dest)
    except Exception, e:
        helper.failure_reason = str(e)
        helper.save()
Esempio n. 26
0
def dropbox_upload(access_token, local_directory, remote_directory):
	#Create a client variable
	client = DropboxClient(access_token)
	
	#See if Dropbox.com is available.
	#If so, continue. If not, return false.
	try:
		urllib.request.urlopen("http://www.dropbox.com")
	except urllib.error.URLError as e:
		return False
	else:
		#Create a directory loop that:
		#1. Loops through all the files in a directory
		#2. Grabs the file path for each existing file
		#3. Uploads it to Dropbox
		#4. Removes the file after it's been uploaded.
		for root, dirs, files in os.walk(local_directory):
			for filename in files:
				local_path = os.path.join(root, filename)
				relative_path = os.path.relpath(local_path, local_directory)
				dropbox_path = os.path.join(remote_directory, relative_path)

				with open(local_path, 'rb') as f:
					client.put_file(dropbox_path, f)
					#Remove this code if you don't
					#want your image deleted after upload
					os.remove(local_path)
		return True
Esempio n. 27
0
 def __init__(self, app_key, app_secret, locale='UTF-8'):
     oauth_flow = DropboxOAuth2FlowNoRedirect(app_key, app_secret)
     authorize_url = oauth_flow.start()
     print(authorize_url)
     access_token, user_id = oauth_flow.finish(input("code: ").strip())
     print(access_token)
     DropboxClient.__init__(self, access_token, locale)
Esempio n. 28
0
class DropBoxStorage(Storage):
    """DropBox Storage class for Django pluggable storage system."""
    def generate_url(self, name):
        url = self.client.share(name, name)
        url = url['url']
        f = request.urlopen(url)
        path = f.geturl()
        return re.sub('www.dropbox.com','dl.dropboxusercontent.com', path)

    def url(self, name):
        return name #

    def __init__(self, oauth2_access_token=setting('DROPBOX_OAUTH2_TOKEN')):
        if oauth2_access_token is None:
            raise ImproperlyConfigured("You must configure a token auth at"
                                       "'settings.DROPBOX_OAUTH2_TOKEN'.")
        self.client = DropboxClient(oauth2_access_token)

    def delete(self, name):
        self.client.file_delete(name)

    def exists(self, name):
        response = self.client.search('/', name, file_limit=1)
        return bool(response)

    def listdir(self, path):
        directories, files = [], []
        metadata = self.client.metadata(path)
        for entry in metadata['contents']:
            if entry['is_dir']:
                directories.append(entry['path'])
            else:
                files.append(entry['path'])
        return directories, files

    def size(self, name):
        metadata = self.client.metadata(name)
        return metadata['bytes']

    def modified_time(self, name):
        metadata = self.client.metadata(name)
        mod_time = datetime.strptime(metadata['modified'], DATE_FORMAT)
        return mod_time

    def accessed_time(self, name):
        metadata = self.client.metadata(name)
        acc_time = datetime.strptime(metadata['client_mtime'], DATE_FORMAT)
        return acc_time

    def _open(self, name, mode='rb'):
        remote_file = DropBoxFile(name, self)
        return remote_file

    def _save(self, name, content):
        self.client.put_file(name, content)
        return name

    def _read(self, name, num_bytes=None):
        data = self.client.get_file(name)
        return data.read(num_bytes)
Esempio n. 29
0
def get_dropbox_client():
    client = DropboxClient(session['access_token'], locale='en_UK')

    user_info = client.account_info()
    session['user_id'] = user_info['uid']
    session['display_name'] = user_info['display_name']
    return client
Esempio n. 30
0
def upload_welcome_pdf(dropbox_id):
    user = User.query.filter_by(dropbox_id=dropbox_id,
                                active=True).first()
    if user is None:
        return False

    # If we've already sent the welcome PDF before, Dropbox webhook went
    # trigger, so do it here.
    if user.uploaded_welcome_pdf:
        return kindlebox(dropbox_id)

    analytics.track(str(user.id), 'Sent welcome pdf')

    client = DropboxClient(user.access_token)
    try:
        with open('app/static/bookdrop_welcome.pdf', 'rb') as f:
            response = client.put_file('Welcome to BookDrop.pdf', f, overwrite=True)
            if response:
                log.info(u"Welcome PDF sent to user ID {0}.".format(user.id))
            else:
                raise Exception("No response received after sending welcome PDF")

        user.set_uploaded_welcome_pdf()
        db.session.commit()

    except:
        log.error((u"Welcome PDF failed for user ID "
                   "{0}.").format(user.id), exc_info=True)
        return False

    return True
Esempio n. 31
0
def connectDropbox():
    sess = DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)

    if os.path.exists(TOKENS):
        token_file = open(TOKENS)
        token_key, token_secret = token_file.read().split('|')
        token_file.close()
        sess.set_token(token_key, token_secret)
    else:
        request_token = sess.obtain_request_token()

        url = sess.build_authorize_url(request_token)

        # Make the user sign in and authorize this token
        print "url:", url
        print "Please visit this website and press the 'Allow' button, then hit 'Enter' here."

        raw_input()

        # This will fail if the user didn't visit the above URL and hit 'Allow'
        access_token = sess.obtain_access_token(request_token)

        # Save the key to the file so we don't need to do this again
        token_file = open(TOKENS, 'w')

        token_key = access_token.key
        token_secret = access_token.secret
        token_file.write("%s|%s" % (token_key, token_secret))

        token_file.close()

    client = DropboxClient(sess)
    print "Linked account: %s" % client.account_info()
    return client
def salvage_broken_user_settings_document(document):
    if not document['access_token'] or not document['dropbox_id']:
        return False
    if not document['owner'] or not User.load(document['owner']).is_active:
        return False
    if document['deleted']:
        return False
    if not document.get(
            'dropbox_info') or not document['dropbox_info']['display_name']:
        logger.info(
            "Attempting dropbox_info population for document (id:{0})".format(
                document['_id']))
        client = DropboxClient(document['access_token'])
        document['dropbox_info'] = {}
        try:
            database['dropboxusersettings'].find_and_modify(
                {'_id': document['_id']},
                {'$set': {
                    'dropbox_info': client.account_info()
                }})
        except Exception:
            # Invalid token probably
            # Still want Dropbox to be enabled to show error message
            # to user on node settings, pass
            return True
        else:
            return True

    return False
Esempio n. 33
0
 def __init__(self, oauth2_access_token=None, root_path=None):
     oauth2_access_token = oauth2_access_token or setting('DROPBOX_OAUTH2_TOKEN')
     self.root_path = root_path or setting('DROPBOX_ROOT_PATH', '/')
     if oauth2_access_token is None:
         raise ImproperlyConfigured("You must configure a token auth at"
                                    "'settings.DROPBOX_OAUTH2_TOKEN'.")
     self.client = DropboxClient(oauth2_access_token)
Esempio n. 34
0
    def downloadFile(self, file, path):
        from dropbox.client import DropboxClient
        log = SetLog()
        status = Status()
        # Usado para comprimir el archivo
        zip = Compress()
        # Extrae el nombre, la extension del archivo
        dir, name = os.path.split(file)
        # Archivo local donde se almacenara
        localFile = os.path.join(path, name)
        cliente = DropboxClient(self.TOKEN)
        thread.start_new_thread(status.setDownloadstatus, (name, path, 1,))
        with self.stopwatch('download'):
            try:
                out = open(localFile, 'wb')
                with self.stopwatch("download"):
                    with cliente.get_file(file) as f:
                        out.write(f.read())
            except dropbox.exceptions.HttpError as err:
                log.newLog("error_download", "T", file)
                return False
        out.close()

        if os.path.exists(localFile):
            thread.start_new_thread(status.setDownloadstatus, (name, path, 2,))
            zip.uncompress(localFile)
            log.newLog("success_download", "T", file)
            thread.start_new_thread(status.setDownloadstatus, (name, path, 0,))
            return True
        else:
            log.newLog("error_download", "T", file)
            return False
Esempio n. 35
0
    def __init__(self, key, secret):

        #See if I already have a stored access_token
        try:
            with open('.dbtoken.json', 'r') as json_data_file:
                data = json.load(json_data_file)
            accessToken = data['accessToken']
        except:
            accessToken = None
        if accessToken is None:
            # connect to dropbox and start the session authorization process
            flow = DropboxOAuth2FlowNoRedirect(key, secret)
            logging.info("Authorize this application: {}".format(flow.start()))
            authCode = input("Enter auth code here: ").strip()

            # finish the authorization and grab the Dropbox client
            (accessToken, userID) = flow.finish(authCode)
            data = {'accessToken': accessToken}
            with open('.dbtoken.json', 'w') as outfile:
                json.dump(data, outfile)

        self.client = DropboxClient(accessToken)
        logging.info("dropbox account linked")

        self.Q = Queue()
        self.thread = Thread(target=self.pull_from_queue, args=())
        self.thread.daemon = True
        self.thread.start()
Esempio n. 36
0
    def __init__(self):
        self.hog = cv2.HOGDescriptor()
        self.hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
        warnings.filterwarnings("ignore")
        self.conf = json.load(open("conf.json"))
        self.lastUploaded = datetime.datetime.now()
        self.timestamp = datetime.datetime.now()
        self.ts = self.timestamp.strftime("%A %d %B %Y %I:%M:%S%p")
        self.pts = deque(maxlen=32)
        (self.dX, self.dY) = (0, 0)
        self.direction = ""
        self.counter = 0
        self.client = None
        self.avg = None
        self.for_show = None
        self.track_windows = None

        # initialize the camera and grab a reference to the raw camera capture
        self.camera = cv2.VideoCapture(0)

        # check to see if the Dropbox should be used
        if self.conf["use_dropbox"]:
            # connect to dropbox and start the session authorization process
            flow = DropboxOAuth2FlowNoRedirect(self.conf["dropbox_key"],
                                               self.conf["dropbox_secret"])
            print "[INFO] Authorize this application: {}".format(flow.start())
            authCode = raw_input("Enter auth code here: ").strip()

            # finish the authorization and grab the Dropbox client
            (accessToken, userID) = flow.finish(authCode)
            self.client = DropboxClient(accessToken)
            print "[SUCCESS] dropbox account linked"
Esempio n. 37
0
	def get_path(self, path):
		client = DropboxClient(self.token)
		files_list = client.metadata(path)['contents']
		quota_info = client.account_info()['quota_info']

		if path != '/':
			path = '/' + path.replace("%20", " ")
			path_regex = re.compile(path, re.IGNORECASE)
			for item in files_list:
				item['name'] = path_regex.split(item['path'], 1)[-1][1:]
				item['path'] = '/' + item['name']
		else:
			for item in files_list:
				item['path'] = item['path'][1:]
				item['name'] = item['path']

		parent_url = (SITE_URL + 'api/path/dropbox/%i' + path) %self.uid
		upload_url = (SITE_URL + 'api/upload/dropbox/%i' + path) %self.uid

		data = {	'bytes_total': quota_info['quota'],
					'bytes_used': quota_info['normal'] + quota_info['shared'],
					'contents': files_list,
					'display_name': self.display_name,
					'parent_path': parent_url,
					'service_class': 'dropbox',
					'service_name': 'dropbox',
					'uid': self.uid,
					'upload_path': upload_url,
					'username': self.email
					
				}
		return data
Esempio n. 38
0
 def getRemoteFilesList(self, ruta):
     self.creaFolder(ruta)
     files = []
     cliente = DropboxClient(self.TOKEN)
     respuesta = cliente.metadata(ruta)
     for file in respuesta["contents"]:
         files.append(file["path"])
     return files
Esempio n. 39
0
 def get_dropbox_profile(self):
     dropbox_profile = None
     if self.dropbox_token is not None:
         client = DropboxClient(self.dropbox_token)
         try:
             dropbox_profile = client.account_info()
         except Exception, e:
             pass
Esempio n. 40
0
 def __init__(self):
     session = DropboxSession(settings.DROPBOX_CONSUMER_KEY,
                              settings.DROPBOX_CONSUMER_SECRET,
                              settings.DROPBOX_ACCESS_TYPE,
                              locale=None)
     session.set_token(settings.DROPBOX_ACCESS_TOKEN,
                       settings.DROPBOX_ACCESS_TOKEN_SECRET)
     self.client = DropboxClient(session)
Esempio n. 41
0
 def get_dropbox_profile(self):
     dropbox_profile = None
     if self.dropbox_token is not None:
         client = DropboxClient(self.dropbox_token)
         try:
             dropbox_profile = client.account_info()
         except Exception, e:
             pass
Esempio n. 42
0
def get_dropbox_share(request):
    if request.method == 'GET':

        user_profile = UserProfile.objects.get(user = request.user)
        drop_client = DropboxClient(user_profile.dropbox_profile.access_token['key'])

        res = drop_client.share(request.GET.get('path'))
        return redirect(res[u'url'])
Esempio n. 43
0
def home():
  real_name = None
  if 'access_token' in session and session['access_token'] is not None:
    access_token = session['access_token']
    client = DropboxClient(access_token)
    account_info = client.account_info()
    real_name = account_info['display_name']
  return render_template('index.html', real_name=real_name)
Esempio n. 44
0
    def get_folders(self, **kwargs):
        folder_id = kwargs.get('folder_id')
        if folder_id is None:
            return [{
                'addon': 'dropbox',
                'id': '/',
                'path': '/',
                'kind': 'folder',
                'name': '/ (Full Dropbox)',
                'urls': {
                    'folders':
                    api_v2_url('nodes/{}/addons/dropbox/folders/'.format(
                        self.owner._id),
                               params={'id': '/'})
                }
            }]

        client = DropboxClient(self.external_account.oauth_key)
        file_not_found = HTTPError(
            http.NOT_FOUND,
            data={
                'message_short':
                'File not found',
                'message_long':
                'The Dropbox file you requested could not be found.'
            })

        max_retry_error = HTTPError(
            http.REQUEST_TIMEOUT,
            data={
                'message_short': 'Request Timeout',
                'message_long': 'Dropbox could not be reached at this time.'
            })

        try:
            metadata = client.metadata(folder_id)
        except ErrorResponse:
            raise file_not_found
        except MaxRetryError:
            raise max_retry_error

        # Raise error if folder was deleted
        if metadata.get('is_deleted'):
            raise file_not_found

        return [{
            'addon': 'dropbox',
            'kind': 'folder',
            'id': item['path'],
            'name': item['path'].split('/')[-1],
            'path': item['path'],
            'urls': {
                'folders':
                api_v2_url('nodes/{}/addons/box/folders/'.format(
                    self.owner._id),
                           params={'id': item['path']})
            }
        } for item in metadata['contents'] if item['is_dir']]
Esempio n. 45
0
    def post(self, request):
        try:
            result = {}
            path = request.POST["source_path"]
            session_id = request.POST["session_id"]
            user_id = get_user_from_session(session_id).id
            dest_path = request.POST["dest_path"]
            dest_path = dest_path.split("/")
            dest_path.append(str(user_id))
            dest_path[-1], dest_path[-2] = dest_path[-2], dest_path[-1]
            dest_path = "/".join(dest_path)
            print "dest_path at starting is ", dest_path
            try:
                # Increment the name of new directory by one
                directories = map(int, os.listdir(dest_path))
                dest_path += str(max(directories) + 1)
            except:
                # If no directory exists then give it name 1
                dest_path += "/" + "1"

            if dest_path[-1] != "/":
                # Append forward slash to given url if not exists
                dest_path += "/"
            if not os.path.isdir(dest_path):
                os.makedirs(dest_path)
            if path.split(":")[0].lower() == "s3":
                print "[DOWNLOAD API FOR S3]"
                bucket = path.split(":")[1][2:]
                source_path = str(path.split(":")[2])
                result = get_data_from_s3(request, source_path, dest_path, bucket, user_id)

            elif path.split(":")[0].lower() == "dropbox":
                print "[DOWNLOAD API FOR DROPBOX]"
                source_path = path.split(":")[1][1:]
                access_token = SocialToken.objects.get(account__user__id=user_id, app__name="Dropbox")
                session = DropboxSession(settings.DROPBOX_APP_KEY, settings.DROPBOX_APP_SECRET)
                access_key, access_secret = (
                    access_token.token,
                    access_token.token_secret,
                )  # Previously obtained OAuth 1 credentials
                session.set_token(access_key, access_secret)
                client = DropboxClient(session)
                token = client.create_oauth2_access_token()
                print "########### ok ###########"
                result = get_data_from_dropbox(request, source_path, dest_path, token, user_id)
            elif path.split(":")[0].lower() == "gdrive":
                # NON FUNCTIONAL
                get_data_from_google(request, path, access_token)
            else:
                shutil.rmtree(dest_path[:-1])
                result = {
                    "error": "Check if you have attached the type of cloud storage with your account or enter valid path"
                }
        except:
            shutil.rmtree(dest_path[:-1])
            result = {"error": "Invalid Input Provided"}
        print result
        return HttpResponse(json.dumps(result))
Esempio n. 46
0
    def __init__(self, filepath=None, token=None):
        if not (filepath or token):
            raise TokenNotProvidedError('Access token is not provided')

        if filepath:
            with open(filepath) as token_file:
                token = token_file.read().strip()

        self.client = DropboxClient(token)
Esempio n. 47
0
 def getBackups(self, ruta):
     self.creaFolder(ruta)
     files = []
     cliente = DropboxClient(self.TOKEN)
     respuesta = cliente.metadata(ruta)
     for file in respuesta["contents"]:
         files.append(file)
     files = sorted(files, key=lambda file: file['client_mtime'])
     return files
Esempio n. 48
0
def spaceUsage():
    client = DropboxClient(session['accessToken'])
    accountInfo = client.account_info()
    quotaInfo = accountInfo["quota_info"]
    total = quotaInfo["quota"]
    normal = quotaInfo["normal"]
    shared = quotaInfo["shared"]
    used = normal + shared
    return toMB(used), toMB(total - used)
def getdropboxfile(request,name):

	dp = DropboxClient(settings.DROPBOX_ACCESS_TOKEN)

	f, metadata = dp.get_file_and_metadata(name)

	response = HttpResponse(f.read(),content_type='application/force-download')
	response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(name)
	return response
Esempio n. 50
0
def send_file_to_dropbox(data, task_pair_id, access_token, filename, path,
                         url):
    client = DropboxClient(access_token)
    file_path = posixpath.join(path, filename)
    file_path = file_path.replace('\\', '|')
    file_path = file_path.encode('ascii', errors='replace').decode()
    resp = requests.get(url)
    resp.raise_for_status()
    client.put_file(file_path, resp.content, overwrite=True)
Esempio n. 51
0
def send_file_to_dropbox(data, task_pair_id, access_token,
                         filename, path, url):
    client = DropboxClient(access_token)
    file_path = posixpath.join(path, filename)
    file_path = file_path.replace('\\', '|')
    file_path = file_path.encode('ascii', errors='replace').decode()
    resp = requests.get(url)
    resp.raise_for_status()
    client.put_file(file_path, resp.content, overwrite=True)
Esempio n. 52
0
 def get_user_info(self, access_token):
     client = DropboxClient(access_token)
     info = client.account_info()
     return {
         "email": info["email"],
         "verified": info["email_verified"],
         "first_name": info["name_details"]["given_name"],
         "last_name": info["name_details"]["surname"],
         "country": info["country"]
     }
Esempio n. 53
0
def backup(filename):
    """
    Backs up a specified file
    """
    token = DevelopmentConfig.dropbox_app_token
    client = DropboxClient(token)
    f = open(filename, 'rb')
    dropbox_filename = '/' + filename
    response = client.put_file(dropbox_filename, f, overwrite=True)
    return "uploaded:", response
Esempio n. 54
0
def disconnect_dropbox(request):
    if request.method == 'POST':
        dropbox_token = request.user.profile.dropbox_token
        if dropbox_token is not None:
            client = DropboxClient(dropbox_token)
            client.disable_access_token()
            request.user.profile.dropbox_token = None
            request.user.save()
            messages.success(request, _('Your Dropbox account were disconnected successfully!'))
    return redirect(r('settings:connections'))
Esempio n. 55
0
    def revoke_remote_oauth_access(self, external_account):
        """Overrides default behavior during external_account deactivation.

        Tells Dropbox to remove the grant for the OSF associated with this account.
        """
        client = DropboxClient(external_account.oauth_key)
        try:
            client.disable_access_token()
        except ErrorResponse:
            pass
Esempio n. 56
0
 def post(self):
     key = self.request.get('session_key')
     c_key = self.request.get('conf_key')
     blob_info = self.request.get('blob_info')
     session = SessionData.get(key)
     conference_data = ConferenceData.get(c_key)
     if session.uploaded_to_dbox: 
         logging.info('Session | %s | already exists'% session.name)
         return
     if conference_data.dbox_access_token:
         access_token = conference_data.dbox_access_token
     else:
         logging.error('FAILED access_token does not exist') 
         #params = {'message':'Authorization token is either revoked or does not exist'}
         #taskqueue.add(url='/utilities/update_dropbox/',
         #                    method='GET',
         #                    params=params, 
         #                    target='%s'% conference_data.module)
         return None
     try:
         client = DropboxClient(access_token, "en_US", rest_client=None)
         logging.info('SUCCESS dbox_client created %s' % client)
     except:
         logging.error('FAILED dbox_client was not created')
         return None
     f = session.blob_store_key.open()
     size = session.blob_store_key.size
     uploader = client.get_chunked_uploader(f, size)
     while uploader.offset < size:
         try:
             upload = uploader.upload_chunked()
         except:
             logging.error('FAILED upload of file %s'% f)
             params = {'session_key':key, 
                     'conf_key': c_key, 
                     'blob_key':blob_info}
             taskqueue.add(url='/utilities/update_dropbox/',
                             method='POST',
                             params=params, 
                             target='db-upload')
     filename = session.filename
     if (conference_data.name and session.room and session.presenter[1] and filename):
         response = uploader.finish('/%s/%s/%s/%s'% (conference_data.name, session.room, session.presenter[1], filename), overwrite = False) #folder structure /conf_name/room/date/lastname/filename
     elif filename:
         response = uploader.finish('/default/%s'% filename, overwrite = False)
     else:
         logging.error('FAILED problem naming file, file skipped')
         f.close()
         return None
     session.uploaded_to_dbox = True
     session.dbox_path = response['path']
     session.dbox_size = response['size']
     session.put()
     f.close()
     return
Esempio n. 57
0
def process():
    # Save file metadata to DB for the first time
    if 'accessToken' not in session:
        session.clear()
        return redirect(url_for('index'))
    elif 'cached' not in session:
        client = DropboxClient(session['accessToken'])
        folderMetadata = client.metadata('/')
        contents = folderMetadata["contents"]
        names = []
        types = []
        sizes = []
        paths = []
        BFS(client, contents, names, types, sizes, paths)
        saveToDB(session["userID"], names, types, sizes, paths)
        session["cached"] = 1

    # File Search from search page
    if request.method == 'POST':
        keyWord = request.form["keyword"]
        paths = []
        sizes = []
        searchInDB(session["userID"], paths, sizes, keyWord)
        data = sorted(zip(sizes, paths))
        searchCount = len(paths)
        return render_template("search.html",
                               data=data,
                               searchCount=searchCount)

    # Button clicks from account page
    if request.args.get('list') is not None:
        names = []
        types = []
        sizes = []
        retrieveFromDB(session["userID"], types, sizes, names)
        data = sorted(zip(types, sizes, names))
        return render_template("files.html", data=data)
    elif request.args.get('filesearch') is not None:
        return render_template("search.html")
    elif request.args.get('analyze') is not None:
        names = []
        types = []
        sizes = []
        retrieveFromDB(session["userID"], types, sizes, names)
        used, free = spaceUsage()
        types, totalSizes = getFileTypeSizes(names, sizes)
        colors = getColors(len(types))
        data = sorted(zip(totalSizes, colors, types))
        return render_template("analysis.html",
                               used=used,
                               free=free,
                               rows=data)
    else:
        session.clear()
        return redirect(url_for('index'))
Esempio n. 58
0
def home():
    if 'user' not in session:
        return redirect(url_for('login'))
    access_token = get_access_token()
    real_name = None
    print(access_token)
    if access_token is not None:
        client = DropboxClient(access_token)
        account_info = client.account_info()
        real_name = account_info["display_name"]
    return render_template('index.html', real_name=real_name)