def __init__(self, in_user_mode=True): """Connect to the Dropbox servers so that files can be uploaded""" self.m = Message(in_user_mode=in_user_mode) session = DropboxSession(API_KEY, API_SECRET, self.ACCESS_TYPE) token_file = PyJson(TOKEN_PATH) token = token_file.doc # If there is a token saved, that can be used to connect with Dropbox if 'key' in token and 'secret' in token: # Read the token and authenticate the session with it session.set_token(token['key'], token['secret']) # Otherwise it is necessary to authenticate for the first time else: # Request an access token request_token = session.obtain_request_token() url = session.build_authorize_url(request_token) # Open the authentication page in the user's browser and wait for them to accept webbrowser.open(url, new=2) print("Press enter once you have authorised the app in your browser") input() # Get a new access token from the authenticated session # This will fail if the user didn't visit the above URL and press 'Allow' try: access_token = session.obtain_access_token(request_token) except Exception as error: if DEBUG: print(error) print("You didn't authorise the app, or something else went wrong") exit(1) # Save the access token to a file so that authentication is not needed next time the app is run token_file.add('key', access_token.key) token_file.add('secret', access_token.secret) token_file.save() # Create a Dropbox client from the session client = DropboxClient(session) self.client = client
def __init__(self, in_user_mode=True): """Connect to the Dropbox servers so that files can be uploaded""" self.m = Message(in_user_mode=in_user_mode) session = DropboxSession(API_KEY, API_SECRET, self.ACCESS_TYPE) token_file = PyJson(TOKEN_PATH) token = token_file.doc # If there is a token saved, that can be used to connect with Dropbox if 'key' in token and 'secret' in token: # Read the token and authenticate the session with it session.set_token(token['key'], token['secret']) # Otherwise it is necessary to authenticate for the first time else: # Request an access token request_token = session.obtain_request_token() url = session.build_authorize_url(request_token) # Open the authentication page in the user's browser and wait for them to accept webbrowser.open(url, new=2) print "Press enter once you have authorised the app in your browser" raw_input() # Get a new access token from the authenticated session # This will fail if the user didn't visit the above URL and press 'Allow' try: access_token = session.obtain_access_token(request_token) except Exception as error: if DEBUG: print error print "You didn't authorise the app, or something else went wrong" exit(1) # Save the access token to a file so that authentication is not needed next time the app is run token_file.add('key', access_token.key) token_file.add('secret', access_token.secret) token_file.save() # Create a Dropbox client from the session client = DropboxClient(session) self.client = client
class History: """ Defines methods for interacting with the file that stores a history of the files that the user has uploaded to the service """ def __init__(self): self.history_file = PyJson(HISTORY_PATH, base={'history': []}) self.history = self.history_file.doc['history'] def display(self, limit, sort_by, direction, start): """ Load the history and print out a number of records to the screen. Arguments: - limit: the number of items to show - direction: the order to sort the files in to, (ascending or descending) - sort_by: the value to sort by (id, path, url or date) - start: the record number to start at """ if len(self.history) < 1: print 'No records to display' return history = sorted(self.history, key=lambda k: k[sort_by], reverse=direction == 'd') grid = [['Id', 'URL', 'Local File', 'Date Created']] # iterate through the array of records, parse them and create a new 2d array of the formatted values for record in history[start - 1:]: if limit == 0: break id_ = str(record['id']) url = record['url'] path = record['path'] date_ = date.fromtimestamp(record['timestamp']).strftime('%d/%m/%Y') row = [id_, url, path, date_] grid.append(row) limit -= 1 print format_grid(grid, divider_positions=[1], truncatable_column=2) def add(self, path, filename, url): """ Write a new record to the history Arguments: - path: the path to the local copy of the file that was uploaded - url: the shortened URl that points to the copy of the file hosted on the users dropbox acount """ id_ = 1 if len(self.history) == 0 else self.history[-1]['id'] + 1 record = { 'id': id_, 'path': path, 'filename': filename, 'url': url, 'timestamp': time() } self.history.append(record) self.history_file.save()
class History: """ Defines methods for interacting with the file that stores a history of the files that the user has uploaded to the service """ def __init__(self): self.history_file = PyJson(HISTORY_PATH, base={'history': []}) self.history = self.history_file.doc['history'] def display(self, limit, sort_by, direction, start): """ Load the history and print out a number of records to the screen. Arguments: - limit: the number of items to show - direction: the order to sort the files in to, (ascending or descending) - sort_by: the value to sort by (id, path, url or date) - start: the record number to start at """ if len(self.history) < 1: print('No records to display') return history = sorted(self.history, key=lambda k: k[sort_by], reverse=direction == 'd') grid = [['Id', 'URL', 'Local File', 'Date Created']] # iterate through the array of records, parse them and create a new 2d array of the formatted values for record in history[start - 1:]: if limit == 0: break id_ = str(record['id']) url = record['url'] path = record['path'] date_ = date.fromtimestamp( record['timestamp']).strftime('%d/%m/%Y') row = [id_, url, path, date_] grid.append(row) limit -= 1 print(format_grid(grid, divider_positions=[1], truncatable_column=2)) def add(self, path, filename, url): """ Write a new record to the history Arguments: - path: the path to the local copy of the file that was uploaded - url: the shortened URl that points to the copy of the file hosted on the users dropbox acount """ id_ = 1 if len(self.history) == 0 else self.history[-1]['id'] + 1 record = { 'id': id_, 'path': path, 'filename': filename, 'url': url, 'timestamp': time() } self.history.append(record) self.history_file.save()