def build_mega_store(): """ Fetch all objects from mega.co.nz Sets global variable MEGA_STORE which is a dict. MEGA_STORE: key - the result of megaz.get_id() value - the result of megaz.get_obj() """ global MEGA_STORE global INITIAL_STORE global SHIT_API #TODO: Need to fix this upstream or override functio #TODO: mega:115 while 1: if SHIT_API > ERROR_THRESHOLD: raise RequestError try: all_obj = MEGA_OBJ.get_files() break except RequestError: print("Bad result from Mega API... Trying again.") SHIT_API += 1 pass INITIAL_STORE = all_obj for mega_dict in all_obj: megaz = mega_thing(all_obj[mega_dict]) local_path = build_mega_path(megaz, 'absolute') if re.search(r'^/Cloud Drive/', local_path): MEGA_STORE[megaz.get_id()] = megaz.get_obj()
def get_mega(): """ Build a dict of all local files and directories in mega """ mega_dict = {} for item in MEGA_STORE: megaz = mega_thing(MEGA_STORE[item]) path = build_mega_path(megaz) mega_dict[path] = megaz.get_id() return mega_dict
def download(mega_obj=None, path=None): """ Download our mega object mega_obj - mega object we would like to download path - The directory the file should be downloaded to """ megaz = mega_thing(mega_obj) if megaz.get_type() == 'folder': path = LOCAL_SYNC + build_mega_path(megaz) os.makedirs(path) return MEGA_OBJ.download((0, mega_obj), path)
def update_mega_cache(): """ Discover our mega.co.nz file system and add it to our sqlite db. """ build_mega_store() DB.execute('DELETE from remote') #TODO: make efficient for item in MEGA_STORE: megaz = mega_thing(MEGA_STORE[item]) path = build_mega_path(megaz) mega_id = megaz.get_id() DB.insert_remote( (path, megaz.get_type(), mega_id, megaz.get_timestamp()))
def update_mega_cache(): """ Discover our mega.co.nz file system and add it to our sqlite db. """ build_mega_store() DB.execute('DELETE from remote') #TODO: make efficient for item in MEGA_STORE: megaz = mega_thing(MEGA_STORE[item]) path = build_mega_path(megaz) mega_id = megaz.get_id() DB.insert_remote((path, megaz.get_type(), mega_id, megaz.get_timestamp()))
def find_parent(path=None): """ Determine the mega.co.nz parent id for specified object path - local path to object """ build_mega_store() #TODO: This should only be called when we need it path = path[len(LOCAL_SYNC) - 1:] path = os.path.dirname(path) if re.match(r'^/[^/]+/$', path): return None for item in MEGA_STORE: megaz = mega_thing(MEGA_STORE[item]) megaz_path = build_mega_path(megaz) if path == megaz_path: return megaz.get_id()
def find_parent(path=None): """ Determine the mega.co.nz parent id for specified object path - local path to object """ build_mega_store() #TODO: This should only be called when we need it path = path[len(LOCAL_SYNC)-1:] path = os.path.dirname(path) if re.match(r'^/[^/]+/$', path): return None for item in MEGA_STORE: megaz = mega_thing(MEGA_STORE[item]) megaz_path = build_mega_path(megaz) if path == megaz_path: return megaz.get_id()
def build_mega_path(megaz=None, style='relative'): """ Build a absolute file or folder path obj - mega_thing object """ path = [megaz.get_name()] while 1: try: megaz_parent = mega_thing(INITIAL_STORE[megaz.get_parent()]) except KeyError: break path.append(megaz_parent.get_name()) megaz = megaz_parent path.reverse() path = '/' + '/'.join(path) if style == 'relative': path = re.sub('Cloud Drive/', '', path) return path
def build_mega_path(megaz=None, style='relative'): """ Build a absolute file or folder path obj - mega_thing object """ path = [ megaz.get_name()] while 1: try: megaz_parent = mega_thing(INITIAL_STORE[megaz.get_parent()]) except KeyError: break path.append(megaz_parent.get_name()) megaz = megaz_parent path.reverse() path = '/' + '/'.join(path) if style == 'relative': path = re.sub('Cloud Drive/', '', path) return path