recentf, recentmeta = client.get_file_and_metadata(FILE_PATH) revisions = client.revisions(FILE_PATH) print "files/revisions obtained" DAT_PATH = "/.DocumentRevisions-V100/DAT/" if not os.path.exists(DAT_PATH): os.makedirs(DAT_PATH) for i in revisions: rev = i['rev'] print "revision", rev, "obtained" f = client.get_file(FILE_PATH, rev) outfile = open(DAT_PATH + rev + "_dat.txt", 'w') outfile.write(f.read()) f.close() outfile.close() DB_PATH = "/.DocumentRevisions-V100/db-V1/db.sqlite" TMP_PATH = "tmp.db" SQL_PATH = "tmp.sql" shutil.copy2(DB_PATH, TMP_PATH) parID = os.stat(ACTUAL_FOLDER).st_ino inodeID = os.stat(ACTUAL_PATH).st_ino print "ids:", parID, inodeID
def download_file(src, dest): with open(dest, 'w') as fil: f = client.get_file(src) fil.write(f.read()) print fil
def lreplace(pattern, sub, string): return re.sub('^%s' % pattern, sub, string) # make connection to dropbox print "Connecting to dropbox..." sess = session.DropboxSession(args.app_key, args.app_secret, args.access_type) sess.set_token(args.access_token_key, args.access_token_secret) client = client.DropboxClient(sess) # get list of files for args.dropbox_dir print "Fetching list of files in:", args.dropbox_dir metadata = client.metadata(args.dropbox_dir) def files_only(metadata): return metadata['is_dir'] == False files = map(lambda metadata: metadata['path'], filter(files_only, metadata['contents'])) # download files to args.dest_dir assure_path_exists(args.dest_dir) for file in files: dest_file = args.dest_dir + lreplace(args.dropbox_dir, '', file) print "Downloading file: %s to: %s" % (file, dest_file) response = client.get_file(file) with open(dest_file, 'wb') as out: while not response.isclosed(): out.write(response.read(1024 * 1024)) # remove files in args.dropbox_dir if args.remove_downloaded_files == True: for file in files: print "Removing file: %s from dropbox" % (file) client.file_delete(file)
pass if USE_RESTORE: restore = client.restore(filedata["path"], alive["rev"]) print restore else: # try to download file. # I'm torn here - I could just use the Dropbox API and tell it to # restore the deleted file to the non-deleted version. PRoblem with # that is that it might recover too much. THis approach lets me restore # to a new folder with _just_ the restored files in, and cherry-pick # what I want to copy back into the main dropbox. try: fh = client.get_file(filedata["path"], rev=alive["rev"]) with open(target+".temp", "w") as oh: oh.write(fh.read()) os.rename(target+'.temp', target) print " ..recovered" except Exception, e: print "*** RECOVERY FAILED: %s"%e # now loop over the folders and recursively walk into them. Folders can # be deleted too, but don't try to undelete them, we'll rely on them being # implicitly reinflated when their files are restored. for file in filter(lambda f: f.get("is_dir", False), meta["contents"]): recover_tree(file["path"], recover_to)
# Make the user sign in and authorize this token url = sess.build_authorize_url(request_token) print "url:", url print "Please authorize in the browser. After you're done, press enter." 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) client = client.DropboxClient(sess) print "linked account:", client.account_info() # Retrieving movies from Rotten Tomatoes url = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit=16&country=us&apikey=" + rotten_api_key json = urllib2.urlopen(url).read() rotten_data = parse_json(json) out = open(path + '/movies.html', 'w') f = client.get_file('/movies.html').read() out.write('<html><h1>Top Movies (via RottenTomatoes)</h1><ul>') print f for movie in rotten_data['movies']: movie_name = movie['title'] out.write("<li>" + movie_name + "</li>") out.write('</ul></html>') out.close()
APP_KEY = '' APP_SECRET = '' # ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app ACCESS_TYPE = 'app_folder' sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE) access_token = '' auth_token = '' sess.set_token(access_token, auth_token) # Sign other API calls is to pass the session object to DropboxClient client = client.DropboxClient(sess) # Download a file f= client.get_file('/TODO.txt').read() file = f pad = f.split('\n') list = [] for i in range(len(pad)-1): if pad[i][0] != 'x': list.append(pad[i]) for i in range(len(list)): #take blank line into account if len(list[i])> 30: s = list[i].splitlines(10) # careful, if string is long enough this will break for i in range(len(s)): print s[i] else: print list[i]
print "basename: %s" % str(basename) scratchDir = tempfile.mkdtemp() os.chdir(scratchDir) f = open(os.path.join(scratchDir, basename), "w") try: subprocess.Popen(['git', 'init', '.'], stdout=sys.stdout, stderr=sys.stderr).wait() except OSError as e: print "Is git installed?" sys.exit(1) for r in sorted(revs, key=lambda r: r['revision']): f.seek(0) response = client.get_file(r['path'], rev=r['revision']) f.write(response.read()) response.close() f.flush() p = subprocess.Popen(['git', 'add', basename], stdout=sys.stdout, stderr=sys.stderr) exitCode = p.wait() if exitCode != 0: print "git add returned <> 0" # And now the fun begins p = subprocess.Popen(['git', 'commit', '-am', 'Revision %d from Dropbox2Git' % r['revision']], stdout=sys.stdout, stderr=sys.stderr)
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." webbrowser.open(url) 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) ####BEGIN CHECKBOOK CODE ###replace checkbook.test.db with you db client = client.DropboxClient(sess) f = client.get_file('/checkbook.test.db').read() with open('checkbook.test.db','r+') as file: file.write(f) transaction = {} conn = sqlite3.connect('checkbook.test.db') c = conn.cursor() def deposit(name, amount): c.execute("INSERT INTO deposits (NAME, AMOUNT, DATE) VALUES ('{}', {}, '{}')".format(name, float(amount.strip()[1:-1]), today)) conn.commit() print('deposit successful') def withdraw(name, amount): c.execute("INSERT INTO withdrawals (NAME, AMOUNT, DATE) VALUES ('{}', {}, '{}')".format(name, float(amount), today)) conn.commit()
pass if USE_RESTORE: restore = client.restore(filedata["path"], alive["rev"]) print restore else: # try to download file. # I'm torn here - I could just use the Dropbox API and tell it to # restore the deleted file to the non-deleted version. PRoblem with # that is that it might recover too much. THis approach lets me restore # to a new folder with _just_ the restored files in, and cherry-pick # what I want to copy back into the main dropbox. try: fh = client.get_file(filedata["path"], rev=alive["rev"]) with open(target + ".temp", "w") as oh: oh.write(fh.read()) os.rename(target + '.temp', target) print " ..recovered" except Exception, e: print "*** RECOVERY FAILED: %s" % e # now loop over the folders and recursively walk into them. Folders can # be deleted too, but don't try to undelete them, we'll rely on them being # implicitly reinflated when their files are restored. for file in filter(lambda f: f.get("is_dir", False), meta["contents"]): recover_tree(file["path"], recover_to) recover_tree(start_walk)