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()
def build_client(config, auth_token=None): """ Builds the Dropbox Client """ if auth_token: pass elif not auth_token and config.get("auth_token"): auth_token = config.get("auth_token") elif not auth_token and not config.get("auth_token"): auth_token, config = start_auth_flow(config) __log__.debug("Creating the dropbox client!") client = DropboxClient(auth_token) __log__.debug("Successfully created client!") # Put the information on a copy of config object configClone = config.copy() configClone.update({ "auth_token": auth_token, "client": client, }) return (client, configClone)
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
def get_filetree(): if 'access_token' not in session: abort(400) client = DropboxClient(session['access_token']) user_id = session['user_id'] cursor = None if DBC.user_exists(user_id): result = update_filetree() cached_tree = result['tree'] cursor = result['cursor'] else: tree, cursor = crawl_all_deltas(client) DBC.store(session['user_id'], tree, cursor) cached_tree = prune(tree, MAX_DIRECTORY_DEPTH) used, total = get_quota_usage(client) result = { 'tree': cached_tree, 'used': used, 'total': total, 'cursor': cursor } return jsonify(result)
def overview(): if 'access_token' not in session: return redirect(url_for('index')) try: client = DropboxClient(session['access_token']) except ErrorResponse, e: abort(401)
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"
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
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
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']
def connect(self, query): sess = DropboxSession(self.app_key, self.app_secret, "app_folder") # Access token is saved to memcache and the filesystem s_token = self.cache.get("s_token") or read_file(".s_token") s_secret = self.cache.get("s_secret") or read_file(".s_secret") if s_token and s_secret: sess.set_token(s_token, s_secret) elif "oauth_token" in query: # callback from Dropbox s_token = sess.obtain_access_token( dropbox.session.OAuthToken(self.cache.get("r_token"), self.cache.get("r_token_secret"))) self.cache.set("s_token", s_token.key) self.cache.set("s_secret", s_token.secret) with open(".s_token", "w") as f: f.write(s_token.key) with open(".s_secret", "w") as f: f.write(s_token.secret) self.cache.delete("r_token") self.cache.delete("r_token_secret") else: # start of Dropbox auth req_token = sess.obtain_request_token() self.cache.set("r_token", req_token.key) self.cache.set("r_token_secret", req_token.secret) url = sess.build_authorize_url(req_token, cherrypy.url()) raise cherrypy.HTTPRedirect(url) self.client = DropboxClient(sess)
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()
def link(self, auth_code): auth_flow = DropboxOAuth2FlowNoRedirect(self.app_key, self.app_secret) url = auth_flow.start() access_token, user_id = auth_flow.finish(auth_code) with open(self.TOKEN_FILE, 'w') as f: f.write('oauth2:' + access_token) self.api_client = DropboxClient(access_token)
def get_images_of_this_month(): client = DropboxClient(DROPBOX_TOKEN) manager = DatastoreManager(client) datastore = manager.open_default_datastore() offer_table = datastore.get_table('offers') offers = offer_table.query() images_to_show = [] for offer in offers: # dropbox.datastore.Record name = offer.get('offerName') begin = datetime.datetime.strptime(offer.get('begin'), "%Y-%m-%d").date() end = datetime.datetime.strptime(offer.get('end'), "%Y-%m-%d").date() begin_month = '{:02d}'.format(begin.month) end_month = '{:02d}'.format(end.month) current_month = '{:02d}'.format(datetime.datetime.now().month) year = '{:4d}'.format(datetime.datetime.now().year) if current_month == begin_month or current_month == end_month: # belong to the current month, so we show it images_to_show.append(name) images_paths = download_and_save_images(images_to_show, year, current_month) TEMPLATE_PATH.insert(0, 'pages/') return template('galeria', images=images_paths)
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'], } )
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'))
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
def build_client(config, auth_token = None, force_new = False): """ Builds the Dropbox Client. Also update the config object. If the client is already available just return that copy. Use `force_new` to create a new instance. """ if config.get("client") and not force_new: __log__.debug("Reusing client!") return (config.get("client"), config) if auth_token: __log__.debug("Auth Token available!") elif not auth_token and config.get("auth_token"): __log__.debug("Using auth_token from config") auth_token = config.get("auth_token") elif not auth_token and not config.get("auth_token"): __log__.debug("No auth_token available. Starting auth flow") auth_token, config = start_auth_flow(config) __log__.debug("Creating the dropbox client!") client = DropboxClient(auth_token) __log__.debug("Successfully created client!") # Put the information on a copy of config object configClone = config.copy() configClone.update({ "auth_token": auth_token, "client": client, }) return (client, configClone)
def post(self): #attempt build or a db_client before generating tasks, redirect if authorization does not exists conference_data = self.get_conference_data() try: client = DropboxClient(conference_data.dbox_access_token, "en_US", rest_client=None) except: conference_data.dbox_update = False data_cache.set('%s-conference_data'% self.module, None) conference_data.put() return self.render_response('utilities.html', failed = True, message = 'Invalid DropBox authorization, please authorize again') sessions = self.get_sessions() for session in sessions: if session.blob_store_key != None: params = { 'session_key':session.key(), 'conf_key':self.get_conference_data().key(), 'blob_key':session.blob_store_key.key()} taskqueue.add(url='/utilities/update_dropbox/', method='POST', params=params, target='db-upload') logging.info('taskqueue created') else: logging.error('Session did not post %s'% sesssion.name) return self.render_response('utilities.html', success = True, message = "Dropbox upload in progress...", user_id = None)
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)
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) redis_client.hset('tokens', uid, access_token) return render_template('done.html', display_name=uid)
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)
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
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
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
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
def get_client(redirect_on_fail=True): access_token = get_config('dropbox_access_token') if access_token: client = DropboxClient(access_token, rest_client=RestClient) client.session.rest_client = RestClient return client if redirect_on_fail: raise Redirect(url_for('.start_auth'))
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)
def credentials_are_valid(self, user_settings, client): if user_settings: client = client or DropboxClient(user_settings.external_accounts[0].oauth_key) try: client.account_info() except (ValueError, IndexError, ErrorResponse): return False return True
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']]
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)