def download_parts():
    service = util.build_service()
    options = util.parse_options("folder_creator_options.json")
    if options == None: return

    # Ensure that there are parts to download
    if len(options["download-parts"]) == 0:
        print(f'ERROR: No parts specified.')
        return

    # Get the current parts id
    print("Verifying DigitalLibrary format...")
    library_id = util.get_digital_library(service).get("library_id")
    if library_id == None: return 1

    curr_parts_id = util.get_separated_folders(service, library_id)["sec_curr"]
    if curr_parts_id == None: return 1

    for part in options["download-parts"]:
        check_stop_script()
        print(f'Downloading files for part "{part}"')
        pdf_tools.download_part_files(
            service, curr_parts_id, part,
            os.path.join(options["folder-dir"], "parts"), options["verbose"])

    print("Finished downloading parts")
    return 0
Beispiel #2
0
def redvest_creator():
    # Build service
    service = util.build_service()

    # Read folder locations
    redvest_options = util.parse_options("redvest_options.json")
    if redvest_options == None: return 1

    # Verify all needed folders exist and retrieve their ids
    print("Verifying DigitalLibrary format...")
    lib_ids = util.get_digital_library(service)
    current_chartz_id = lib_ids.get("current_id")
    future_chartz_id = lib_ids.get("future_id")
    if current_chartz_id == None or future_chartz_id == None: return 1

    # Verify (and collect) all chart ids
    print("Validating Chartz...")
    chart_ids = [
        verify_chart_name(service, chart,
                          [current_chartz_id, future_chartz_id])
        for chart in redvest_options["chartz"]
    ]
    if None in chart_ids:
        print('Try double-check your spelling, chart names are case-sensitive')
        print('ERROR: Redvest folder will not be created')
        return 1

    print("Verifying Redvest folder...")
    new_folder_id, new_resources_id = verify_redvest(service, redvest_options)
    if new_folder_id == None or new_resources_id == None: return 1

    # Only make individual section folders if field set to true
    alias_map = None
    section_ids = None
    check_stop_script()
    if redvest_options["individual-sections"]:
        # Read parts
        parts_dict = util.parse_options("parts.json")['parts']
        if parts_dict == None: return 1

        # Make individual section folders
        print("Making individual section folders...")
        section_ids = make_section_folders(service, new_folder_id,
                                           parts_dict.keys())

        # Invert parts_dict to create map of alias's -> folder
        alias_map = util.make_alias_map(parts_dict)

    # Write each chart to the new redvest folder
    for index, chart in enumerate(redvest_options["chartz"]):
        write_song(service, chart, chart_ids[index], new_folder_id,
                   new_resources_id, section_ids, alias_map)

    print(
        f'Successfully created new folder "{redvest_options["folder-name"]}"!')
    return 0
Beispiel #3
0
def clear_broken_shortcuts():
    # Build drive
    service = util.build_service()
    check_stop_script()

    # Verify all needed folders exist and retrieve their ids
    print("Verifying DigitalLibrary format...")
    lib_ids = util.get_digital_library(service)
    library_id = lib_ids.get("library_id")
    if library_id == None: return 1

    sep_ids = util.get_separated_folders(service, library_id)
    if sep_ids == None: return 1

    sep_parts = {
        age: {
            folder["name"]: folder["id"]
            for folder in util.get_drive_files(
                service, sep_ids[f"sec_{age}"], files_only=False)
        }
        for age in ["curr", "old", "future"]
    }
    sep_audio = {
        age: {
            folder["name"]: folder["id"]
            for folder in util.get_drive_files(
                service, sep_ids[f"aud_{age}"], files_only=False)
        }
        for age in ["curr", "old", "future"]
    }
    print("Examining shortcuts...")

    allFolders = []
    for age in ["curr", "old", "future"]:
        allFolders.extend(sep_parts[age].values())
        allFolders.extend(sep_audio[age].values())
        allFolders.append(sep_ids[f"sib_{age}"])

    for id in allFolders:
        for file in util.get_drive_files(service, id, is_shortcut=True):
            targetId = file.get("shortcutDetails").get("targetId")
            try:
                targetFile = service.files().get(fileId=targetId,
                                                 fields="trashed").execute()
                if targetFile["trashed"] is True:
                    raise Exception
            except:
                print(
                    f'File pointed to by {file.get("name")} is a broken shortcut.'
                )
                service.files().delete(fileId=file.get("id")).execute()

    return 0
def reset_permissions():
    # Build drive
    service = util.build_service()
    check_stop_script()

    # Verify all needed folders exist and retrieve their ids
    print("Verifying DigitalLibrary format...")
    lib_ids = util.get_digital_library(service)
    library_id = lib_ids.get("library_id")
    if library_id == None: return 1

    sep_ids = util.get_separated_folders(service, library_id)
    if sep_ids == None: return 1

    print("Reading chart permissions...")
    all_chart_permissions = get_all_chart_permissions(
        service, lib_ids.get("current_id"), lib_ids.get("past_id"),
        lib_ids.get("future_id"))
    all_chart_batch = service.new_batch_http_request(callback=batch_callback)
    print(len(all_chart_permissions.keys()))
    fixed = fix_permissions(service,
                            all_chart_batch,
                            all_chart_permissions,
                            allPrivate=True)
    if fixed:
        print("Making all chart folders private...")
        all_chart_batch.execute()

    for chartName in all_chart_permissions:
        entry_batch = service.new_batch_http_request(callback=batch_callback)
        chartId = all_chart_permissions[chartName][0]

        # Edit this line if you need to reset the permissions for a single chart
        # if chartName != 'Example Chart': continue
        print(f'Resetting permissions for "{chartName}"')
        entry_permissions = get_all_entry_permissions(service, chartId)
        parts_id, audio_id = util.get_parts_and_audio_folders(
            service, chartName, chartId)
        entry_permissions.update(get_all_entry_permissions(service, parts_id))
        entry_permissions.update(get_all_entry_permissions(service, audio_id))
        fixed = fix_permissions(service, entry_batch, entry_permissions)
        if fixed:
            print(f'Executing permissions updates')
            sleep(2)  # Avoid too many api calls
            entry_batch.execute()

    print("Successfully reset permissions")
def upload_files():
    # Build service
    service = util.build_service()

    # Read options
    alias_map = util.make_alias_map(util.parse_options("parts.json")['parts'])
    options = util.parse_options("upload_options.json")
    if options == None: return 1
    check_stop_script()

    # Get list of files that need to be uploaded
    files = util.get_dir_files(options["resources-directory"],
                               options["supported-file-types"])
    check_stop_script()

    # Verify all needed folders exist and retrieve their ids
    print("Verifying DigitalLibrary format...")
    lib_ids = util.get_digital_library(service)
    library_id = lib_ids.get("library_id")
    if library_id == None: return 1
    check_stop_script()

    separated_ids = util.get_separated_folders(service, library_id)
    if separated_ids == None: return 1
    check_stop_script()

    # Cache will store folder + parts folder ids and a list of files
    cache = {}

    # 0 = update only, 1 = new files only, 2 = update and add new files
    mode = options["mode"]

    # Create new folders (if in proper mode)
    if mode != 0:
        for chart_info in options["new-chartz"]:
            check_stop_script()
            chart_dest = chart_info["to"]
            new_chart_dest_key = "current_id" if chart_dest == 0 else "past_id" if chart_dest == 1 else "future_id" if chart_dest == 2 else "archive_id"
            chart_id, parts_id, audio_id = lib_management.create_chart_structure(
                service, lib_ids.get(new_chart_dest_key), chart_info["name"])
            if chart_id:
                cache[chart_info["name"]] = {
                    "chart_id": chart_id,
                    "parts_id": parts_id,
                    "audio_id": audio_id,
                    "loc": chart_info["to"],
                    "files": []
                }

    # Operate on files
    for file in files:
        # Check to see if we should exit
        check_stop_script()

        # Populate cache
        lib_management.populate_cache(service, lib_ids.get("current_id"),
                                      lib_ids.get("past_id"),
                                      lib_ids.get("future_id"),
                                      lib_ids.get("archive_id"),
                                      util.parse_file(file, alias_map)[0],
                                      cache, options)
        updated = None
        added = None

        # Update file
        if mode != 1:
            updated = lib_management.update_file(service, file, alias_map,
                                                 cache, options)

        # Add file
        if mode != 0 and not updated:
            added = lib_management.add_file(service, file, separated_ids,
                                            alias_map, cache, options)

        # print output
        if updated == True:
            print(f'Successfully updated "{file}"')
        elif added == True:
            print(f'Successfully added "{file}"')
        elif updated == False:
            print(f'ERROR: Unable to update "{file}"')

    print("Finished uploading files")
    return 0