Example #1
0
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()
Example #2
0
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()
Example #3
0
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
Example #4
0
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
Example #5
0
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)
Example #6
0
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()))
Example #7
0
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)
Example #8
0
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()))
Example #9
0
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()
Example #10
0
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()
Example #11
0
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
Example #12
0
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