def client(parameters): cmd = parameters[0] if cmd == "ls": path = parameters[1] to_file = parameters[2] resp = api_client.metadata(path) file = open(to_file, "w") if 'contents' in resp: for f in resp['contents']: name = os.path.basename(f['path']) encoding = locale.getdefaultlocale()[1] file.write(('%s\n' % name).encode(encoding)) file.close() if cmd == "share": param = parameters[1] f = api_client.share(param) url = str(f['url']) print(" > Generated link: " + url) elif cmd == "delta": state = load_state() cursor = state.get('cursor') tree = state['tree'] page = 0 changed = False page_limit = 15 try: delta_switch = parameter[1] except: delta_switch = 0 while (page_limit is None) or (page < page_limit): # Make an int for progress/total progress = 0 total = 0 # Get /delta results from Dropbox result = api_client.delta(cursor) page += 1 if result['reset'] == True: sys.stdout.write('reset\n') changed = True tree = {} cursor = result['cursor'] # Apply the entries one by one to our cached tree. for delta_entry in result['entries']: total = total + 1 for delta_entry in result['entries']: changed = True progress = progress + 1 print("Current entry: " + str(progress) + "/" + str(total)) apply_delta(tree, delta_entry) cursor = result['cursor'] #if not result['has_more']: break if not changed: sys.stdout.write('No updates.\n') else: # Save state state['cursor'] = cursor state['tree'] = tree save_state(state) elif cmd == "put": from_path = parameters[1] to_path = parameters[2] notify = parameters[3] # it can be 'add' or 'upd' from_file = open(os.path.expanduser(from_path), 'rb') # try: api_client.put_file("/" + to_path, from_file) # except: # print(" x Unable to upload file. ") common.orphiliaNotify(notify, from_path) elif cmd == "unlink": sess.unlink() print(" > Unlinked :C") elif cmd == "cat": f = api_client.get_file("/" + path) stdout.write(f.read()) stdout.write("\n") elif cmd == "mkdir": path = parameters[1] try: api_client.file_create_folder("/" + path) except: print(" x Unable to make directory " + path) print(" > Directory \'" + path + "\' created") elif cmd == "rm": path = path_rewrite.rewritepath('posix', parameters[1]) try: api_client.file_delete("/" + path) common.orphiliaNotify('rm', path) except: print(" x Unable to remove file " + path) elif cmd == "mv": from_path = parameters[1] to_path = parameters[2] try: api_client.file_move("/" + from_path, "/" + to_path) except: print(" x Unable to move file " + from_path + " to " + to_path) elif cmd == "account_info": f = api_client.account_info() pprint.PrettyPrinter(indent=2).pprint(f) elif cmd == "uid": f = api_client.account_info() uid = str(f['uid']) return uid elif cmd == "get": from_path = parameters[1] to_path = parameters[2] resp = api_client.metadata(from_path) modified = resp['client_mtime'] try: open(os.path.expanduser(to_path), 'rb') except: pass date1 = time.mktime(parse(modified).timetuple()) f = api_client.get_file("/" + from_path) dirname = os.path.dirname(os.path.abspath(to_path)) if not os.path.exists(dirname): os.makedirs(dirname) file = open(to_path, "wb") try: file.write(f.read()) file.close() os.utime(os.path.normpath(to_path), (date1, date1)) except: print(" x Unable to save file.") elif cmd == "sync": """ Command which syncs files. - if both file exists, compare the dates: - local is older? download from server - local is newer? remove from server and upload local - if only local exists, upload - if exists only on the server, download """ # get path that was sent to client path = parameters[1] localPath = os.path.normpath(dropboxPath + '/' + path) # assume that both files actually exist change = 'upd' # check if file exists on the server, try to get response data try: resp = api_client.metadata(path) modified = resp['client_mtime'] dropboxTime = parse(modified) except: # ok, it doesn't, so we're going to upload that file dropboxTime = 0 change = 'add' # check if local file exists and try to get it's modified date try: localTime = datetime.datetime.fromtimestamp( os.path.getmtime(localPath), tz=tz.tzutc()) except: change = 'get' localTime = 0 # uhm, this was not supposed to happen... if dropboxTime == 0 and localTime == 0: print( " x WTF?! It looks like there is no file on the server nor locally..." ) exit() else: if change == "upd": # both file exists, decide what's next if localTime < dropboxTime: # local file is older, download from server change = "get" else: # local file is newer, remove from server tmp = ['rm', path] client(tmp) # and reupload change = "add" # push tasks to client if change == "get": tmp = ['get', path, localPath] client(tmp) elif change == "add": tmp = ['put', localPath, path, change] client(tmp) print(" > Command '" + parameters[0] + "' executed")
def getPublicLink(parameters): par = parameters[0] link = 'https://dl.dropboxusercontent.com/u/' + accountUID + '/' + path_rewrite.rewritepath( 'url', par[len(os.path.normpath(dropboxPath + "/Public")) + 1:]) common.orphiliaNotify('link', link) print(link)
def getPublicLink(parameters): par = parameters[0] link = 'https://dl.dropboxusercontent.com/u/' + accountUID + '/' + path_rewrite.rewritepath('url',par[len(os.path.normpath(dropboxPath + "/Public"))+1:]) common.orphiliaNotify('link',link) print(link)
def client(parameters): cmd = parameters[0] if cmd == "ls": path = parameters[1] to_file = parameters[2] resp = api_client.metadata(path) file = open(to_file,"w") if 'contents' in resp: for f in resp['contents']: name = os.path.basename(f['path']) encoding = locale.getdefaultlocale()[1] file.write(('%s\n' % name).encode(encoding)) file.close() if cmd == "share": param = parameters[1] f = api_client.share(param) url = str(f['url']) print(" > Generated link: " + url) elif cmd == "delta": state = load_state() cursor = state.get('cursor') tree = state['tree'] page = 0 changed = False page_limit = 15 try: delta_switch = parameter[1] except: delta_switch = 0 while (page_limit is None) or (page < page_limit): # Make an int for progress/total progress = 0 total = 0 # Get /delta results from Dropbox result = api_client.delta(cursor) page += 1 if result['reset'] == True: sys.stdout.write('reset\n') changed = True tree = {} cursor = result['cursor'] # Apply the entries one by one to our cached tree. for delta_entry in result['entries']: total = total+1 for delta_entry in result['entries']: changed = True progress = progress +1 print("Current entry: "+str(progress)+"/"+str(total)) apply_delta(tree, delta_entry) cursor = result['cursor'] if not result['has_more']: break if not changed: sys.stdout.write('No updates.\n') else: # Save state state['cursor'] = cursor state['tree'] = tree save_state(state) elif cmd == "put": from_path = parameters[1] to_path = parameters[2] notify = parameters[3] # it can be 'add' or 'upd' from_file = open(os.path.expanduser(from_path), 'rb') #try: api_client.put_file("/" + to_path, from_file) #except: # print(" x Unable to upload file. ") common.orphiliaNotify(notify,from_path) elif cmd == "unlink": sess.unlink() print(" > Unlinked :C") elif cmd == "cat": f = api_client.get_file("/" + path) stdout.write(f.read()) stdout.write("\n") elif cmd == "mkdir": path = parameters[1] try: api_client.file_create_folder("/" + path) except: print(" x Unable to make directory " + path) print(" > Directory \'" + path + "\' created") elif cmd == "rm": path = path_rewrite.rewritepath('posix',parameters[1]) try: api_client.file_delete("/" + path) common.orphiliaNotify('rm',path) except: print(" x Unable to remove file " + path) elif cmd == "mv": from_path = parameters[1] to_path = parameters[2] try: api_client.file_move("/" + from_path, "/" + to_path) except: print(" x Unable to move file " + from_path + " to " + to_path) elif cmd == "account_info": f = api_client.account_info() pprint.PrettyPrinter(indent=2).pprint(f) elif cmd == "uid": param = parameters[1] f = api_client.account_info() uid = str(f['uid']) try: common.putIn(uid,param,'rewrite') except: print(" x Unable to save file.") print(" > UID updated") elif cmd == "get": from_path = parameters[1] to_path = parameters[2] resp = api_client.metadata(from_path) modified = resp['modified'] try: open(os.path.expanduser(to_path), 'rb') except: pass date1 = time.mktime(datetime.datetime.strptime(modified, "%a, %d %b %Y %H:%M:%S +0000").timetuple()) f = api_client.get_file("/" + from_path) file = open(to_path,"w+") try: file.write(f.read()) except: print(" x Unable to save file.") file.close() os.utime(os.path.normpath(to_path),(date1,date1)) elif cmd == "sync": path = parameters[1] localPath = os.path.normpath(dropboxPath + '/' + path) change = 'upd' try: resp = api_client.metadata(path) modified = resp['client_mtime'] except: change = 'add' try: localTime = os.path.getmtime(localPath) except: localTime = 0 if (change != 'add'): dropboxTime = time.mktime(datetime.datetime.strptime(modified, "%a, %d %b %Y %H:%M:%S +0000").timetuple()) if ((change != 'add') and (localTime < dropboxTime)): tmp = ['get',path,localPath] client(tmp) elif ((change != 'add') and (localTime == dropboxTime)): print(" > No need to update. localTime: " + str(localTime) + " = dropboxTime: " + str(dropboxTime)) else: if (change != 'add'): tmp = ['rm', path] client(tmp) tmp = ['put',localPath,path,change] client(tmp) print(" > Command '" + parameters[0] + "' executed")
def client(parameters): cmd = parameters[0] if cmd == "ls": path = parameters[1] to_file = parameters[2] resp = api_client.metadata(path) file = open(to_file, "w") if 'contents' in resp: for f in resp['contents']: name = os.path.basename(f['path']) encoding = locale.getdefaultlocale()[1] file.write(('%s\n' % name).encode(encoding)) file.close() elif cmd == "delta": state = load_state() cursor = state.get('cursor') tree = state['tree'] page = 0 changed = False page_limit = 5 try: delta_switch = parameter[1] except: delta_switch = 0 while (page_limit is None) or (page < page_limit): # Get /delta results from Dropbox result = api_client.delta(cursor) page += 1 if result['reset'] == True: sys.stdout.write('reset\n') changed = True tree = {} cursor = result['cursor'] # Apply the entries one by one to our cached tree. for delta_entry in result['entries']: changed = True apply_delta(tree, delta_entry) cursor = result['cursor'] if not result['has_more']: break if not changed: sys.stdout.write('No updates.\n') else: # Save state state['cursor'] = cursor state['tree'] = tree save_state(state) elif cmd == "put": from_path = parameters[1] to_path = parameters[2] notify = parameters[3] # it can be 'add' or 'upd' from_file = open(os.path.expanduser(from_path), 'rb') #try: api_client.put_file("/" + to_path, from_file) #except: # print(" x Unable to upload file. ") common.orphiliaNotify(notify, from_path) elif cmd == "unlink": sess.unlink() print(" > Unlinked :C") elif cmd == "cat": f = api_client.get_file("/" + path) stdout.write(f.read()) stdout.write("\n") elif cmd == "mkdir": path = parameters[1] try: api_client.file_create_folder("/" + path) except: print(" x Unable to make directory " + path) print(" > Directory \'" + path + "\' created") elif cmd == "rm": path = path_rewrite.rewritepath('posix', parameters[1]) try: api_client.file_delete("/" + path) common.orphiliaNotify('rm', path) except: print(" x Unable to remove file " + path) elif cmd == "mv": from_path = parameters[1] to_path = parameters[2] try: api_client.file_move("/" + from_path, "/" + to_path) except: print(" x Unable to move file " + from_path + " to " + to_path) elif cmd == "account_info": f = api_client.account_info() pprint.PrettyPrinter(indent=2).pprint(f) elif cmd == "uid": param = parameters[1] f = api_client.account_info() uid = str(f['uid']) try: common.putIn(uid, param, 'rewrite') except: print(" x Unable to save file.") print(" > UID updated") elif cmd == "get": from_path = parameters[1] to_path = parameters[2] try: open(os.path.expanduser(to_path), 'rb') except: resp = api_client.metadata(from_path) modified = resp['modified'] date1 = modified[5:] date1 = date_rewrite.generate_modifytime(date1) f = api_client.get_file("/" + from_path) file = open(to_path, "wb") try: file.write(f.read()) except: print(" x Unable to save file.") file.close() if sys.platform[:5] != "win32": os.system("touch -d \"" + date1 + "\" \"" + to_path + "\"") # this solution won't work on Windows elif cmd == "sync_folder": path = parameters[1] resp = api_client.metadata( path) # gets list of files in directory on Dropbox dirlist = os.listdir( dropboxPath + "/" + path) # gets list of files in directory on local computer rand1 = random.random() # generates random integer queue = Queue.Queue(0) if 'contents' in resp: # begin comparing both lists for f in resp['contents']: name = os.path.basename(f['path']) encoding = locale.getdefaultlocale()[1] if ('%s' % name).encode( encoding ) not in dirlist: # found Dropbox file, which isn't present on local computer print('%s' % name).encode(encoding) + " not found." if not os.path.isfile(('%s' % name).encode(encoding)): dir = f['is_dir'] if not dir: tmp = [ 'get', path + "/" + name, dropboxPath + "/" + path + name ] queue.put(client_new(tmp)) if dir: os.mkdir(dropboxPath + "/" + path + ('%s' % name).encode(encoding)) else: # found Dropbox file which is present on local computer, check this out, bro! name = os.path.basename(f['path']) encoding = locale.getdefaultlocale()[1] print(('%s' % name).encode(encoding) + " found. Checking...") print(" > Command '" + parameters[0] + "' executed")
def client(parameters): cmd = parameters[0] if cmd == "ls": path = parameters[1] to_file = parameters[2] resp = api_client.metadata(path) file = open(to_file,"w") if 'contents' in resp: for f in resp['contents']: name = os.path.basename(f['path']) encoding = locale.getdefaultlocale()[1] file.write(('%s\n' % name).encode(encoding)) file.close() elif cmd == "delta": state = load_state() cursor = state.get('cursor') tree = state['tree'] page = 0 changed = False page_limit = 5 try: delta_switch = parameter[1] except: delta_switch = 0 while (page_limit is None) or (page < page_limit): # Get /delta results from Dropbox result = api_client.delta(cursor) page += 1 if result['reset'] == True: sys.stdout.write('reset\n') changed = True tree = {} cursor = result['cursor'] # Apply the entries one by one to our cached tree. for delta_entry in result['entries']: changed = True apply_delta(tree, delta_entry) cursor = result['cursor'] if not result['has_more']: break if not changed: sys.stdout.write('No updates.\n') else: # Save state state['cursor'] = cursor state['tree'] = tree save_state(state) elif cmd == "put": from_path = parameters[1] to_path = parameters[2] notify = parameters[3] # it can be 'add' or 'upd' from_file = open(os.path.expanduser(from_path), 'rb') #try: api_client.put_file("/" + to_path, from_file) #except: # print(" x Unable to upload file. ") common.orphiliaNotify(notify,from_path) elif cmd == "unlink": sess.unlink() print(" > Unlinked :C") elif cmd == "cat": f = api_client.get_file("/" + path) stdout.write(f.read()) stdout.write("\n") elif cmd == "mkdir": path = parameters[1] try: api_client.file_create_folder("/" + path) except: print(" x Unable to make directory " + path) print(" > Directory \'" + path + "\' created") elif cmd == "rm": path = path_rewrite.rewritepath('posix',parameters[1]) try: api_client.file_delete("/" + path) common.orphiliaNotify('rm',path) except: print(" x Unable to remove file " + path) elif cmd == "mv": from_path = parameters[1] to_path = parameters[2] try: api_client.file_move("/" + from_path, "/" + to_path) except: print(" x Unable to move file " + from_path + " to " + to_path) elif cmd == "account_info": f = api_client.account_info() pprint.PrettyPrinter(indent=2).pprint(f) elif cmd == "uid": param = parameters[1] f = api_client.account_info() uid = str(f['uid']) try: common.putIn(uid,param,'rewrite') except: print(" x Unable to save file.") print(" > UID updated") elif cmd == "get": from_path = parameters[1] to_path = parameters[2] try: open(os.path.expanduser(to_path), 'rb') except: resp = api_client.metadata(from_path) modified = resp['modified'] date1 = modified[5:] date1 = date_rewrite.generate_modifytime(date1) f = api_client.get_file("/" + from_path) file = open(to_path,"wb") try: file.write(f.read()) except: print(" x Unable to save file.") file.close() if sys.platform[:5] != "win32": os.system("touch -d \"" + date1 + "\" \"" + to_path + "\"") # this solution won't work on Windows elif cmd == "sync_folder": path = parameters[1] resp = api_client.metadata(path) # gets list of files in directory on Dropbox dirlist = os.listdir(dropboxPath + "/" + path) # gets list of files in directory on local computer rand1 = random.random() # generates random integer queue = Queue.Queue(0) if 'contents' in resp: # begin comparing both lists for f in resp['contents']: name = os.path.basename(f['path']) encoding = locale.getdefaultlocale()[1] if ('%s' % name).encode(encoding) not in dirlist: # found Dropbox file, which isn't present on local computer print ('%s' % name).encode(encoding) + " not found." if not os.path.isfile(('%s' % name).encode(encoding)): dir = f['is_dir'] if not dir: tmp = [ 'get', path + "/" + name, dropboxPath + "/" + path + name] queue.put(client_new(tmp)) if dir: os.mkdir(dropboxPath + "/" + path + ('%s' % name).encode(encoding)) else: # found Dropbox file which is present on local computer, check this out, bro! name = os.path.basename(f['path']) encoding = locale.getdefaultlocale()[1] print(('%s' % name).encode(encoding) + " found. Checking...") print(" > Command '" + parameters[0] + "' executed")
def client(parameters): cmd = parameters[0] if cmd == "ls": path = parameters[1] to_file = parameters[2] resp = api_client.metadata(path) file = open(to_file, "w") if 'contents' in resp: for f in resp['contents']: name = os.path.basename(f['path']) encoding = locale.getdefaultlocale()[1] file.write(('%s\n' % name).encode(encoding)) file.close() if cmd == "share": param = parameters[1] f = api_client.share(param) url = str(f['url']) print(" > Generated link: " + url) elif cmd == "delta": state = load_state() cursor = state.get('cursor') tree = state['tree'] page = 0 changed = False page_limit = 15 try: delta_switch = parameter[1] except: delta_switch = 0 while (page_limit is None) or (page < page_limit): # Make an int for progress/total progress = 0 total = 0 # Get /delta results from Dropbox result = api_client.delta(cursor) page += 1 if result['reset'] == True: sys.stdout.write('reset\n') changed = True tree = {} cursor = result['cursor'] # Apply the entries one by one to our cached tree. for delta_entry in result['entries']: total = total + 1 for delta_entry in result['entries']: changed = True progress = progress + 1 print("Current entry: " + str(progress) + "/" + str(total)) apply_delta(tree, delta_entry) cursor = result['cursor'] #if not result['has_more']: break if not changed: sys.stdout.write('No updates.\n') else: # Save state state['cursor'] = cursor state['tree'] = tree save_state(state) elif cmd == "put": from_path = parameters[1] to_path = parameters[2] notify = parameters[3] # it can be 'add' or 'upd' from_file = open(os.path.expanduser(from_path), 'rb') # try: api_client.put_file("/" + to_path, from_file) # except: # print(" x Unable to upload file. ") common.orphiliaNotify(notify, from_path) elif cmd == "unlink": sess.unlink() print(" > Unlinked :C") elif cmd == "cat": f = api_client.get_file("/" + path) stdout.write(f.read()) stdout.write("\n") elif cmd == "mkdir": path = parameters[1] try: api_client.file_create_folder("/" + path) except: print(" x Unable to make directory " + path) print(" > Directory \'" + path + "\' created") elif cmd == "rm": path = path_rewrite.rewritepath('posix', parameters[1]) try: api_client.file_delete("/" + path) common.orphiliaNotify('rm', path) except: print(" x Unable to remove file " + path) elif cmd == "mv": from_path = parameters[1] to_path = parameters[2] try: api_client.file_move("/" + from_path, "/" + to_path) except: print(" x Unable to move file " + from_path + " to " + to_path) elif cmd == "account_info": f = api_client.account_info() pprint.PrettyPrinter(indent=2).pprint(f) elif cmd == "uid": f = api_client.account_info() uid = str(f['uid']) return uid elif cmd == "get": from_path = parameters[1] to_path = parameters[2] resp = api_client.metadata(from_path) modified = resp['client_mtime'] try: open(os.path.expanduser(to_path), 'rb') except: pass date1 = time.mktime(parse(modified).timetuple()) f = api_client.get_file("/" + from_path) dirname = os.path.dirname(os.path.abspath(to_path)) if not os.path.exists(dirname): os.makedirs(dirname) file = open(to_path, "wb") try: file.write(f.read()) file.close() os.utime(os.path.normpath(to_path), (date1, date1)) except: print(" x Unable to save file.") elif cmd == "sync": """ Command which syncs files. - if both file exists, compare the dates: - local is older? download from server - local is newer? remove from server and upload local - if only local exists, upload - if exists only on the server, download """ # get path that was sent to client path = parameters[1] localPath = os.path.normpath(dropboxPath + '/' + path) # assume that both files actually exist change = 'upd' # check if file exists on the server, try to get response data try: resp = api_client.metadata(path) modified = resp['client_mtime'] dropboxTime = parse(modified) except: # ok, it doesn't, so we're going to upload that file dropboxTime = 0 change = 'add' # check if local file exists and try to get it's modified date try: localTime = datetime.datetime.fromtimestamp(os.path.getmtime(localPath), tz=tz.tzutc()) except: change = 'get' localTime = 0 # uhm, this was not supposed to happen... if dropboxTime == 0 and localTime == 0: print(" x WTF?! It looks like there is no file on the server nor locally...") exit() else: if change == "upd": # both file exists, decide what's next if localTime < dropboxTime: # local file is older, download from server change = "get" else: # local file is newer, remove from server tmp = ['rm', path] client(tmp) # and reupload change = "add" # push tasks to client if change == "get": tmp = ['get', path, localPath] client(tmp) elif change == "add": tmp = ['put', localPath, path, change] client(tmp) print(" > Command '" + parameters[0] + "' executed")