def get_map(config_path, plex):
    plex_map = {}
    current_length = 0
    current_count = 0
    if TMDB.valid:
        tmdb = TMDb()
        tmdb.api_key = TMDB(config_path).apikey
        tmovie = TMDb_Movie()
    print("|")
    if plex.library_type == "movie":
        config_dir = os.path.dirname(config_path)
        db_dir = os.path.join(config_dir, 'db')
        guid_map = os.path.join(
            db_dir, "{}_guid.db".format(
                os.path.splitext(os.path.basename(config_path))[0]))
        if not os.path.isfile(guid_map):
            create_guid_map(config_path)
            update_guid_map_from_db(config_path, plex)
    print("| Mapping Plex {}".format("Movies" if plex.library_type ==
                                     "movie" else "Shows"))
    plex_items = plex.Library.all()
    try:
        for item in plex_items:
            current_count += 1
            print_display = "| Processing: {}/{} {}".format(
                current_count, len(plex_items), item.title)
            print(adjust_space(current_length, print_display), end="\r")
            current_length = len(print_display)
            update = None  # TODO: Use this to refresh the GUID map
            key_id = None
            error_message = "Unable to map {} ID".format(
                "TMDb/IMDb" if plex.library_type == "movie" else "TVDb")

            guid = urlparse(item.guid)
            item_type = guid.scheme.split('.')[-1]
            check_id = guid.netloc
            if item_type == 'plex' and plex.library_type == "movie":
                # Check GUID map for TMDb ID
                key_id = query_guid_map(config_path, item.guid, 'tmdb_id')
                # Check GUID map for for IMDb ID
                if not key_id:
                    key_id = query_guid_map(config_path, item.guid, 'imdb_id')
                if not key_id:
                    imdb_id, tmdb_id = alt_id_lookup(plex, item)
                    if tmdb_id:
                        key_id = tmdb_id
                        print(
                            adjust_space(
                                current_length,
                                "| GUID map | {} | {:<46} | {:<6} | {}".format(
                                    "^" if update == True else "+", item.guid,
                                    key_id, item.title)))
                        update_guid_map(config_path, item.guid, tmdb_id=key_id)
                    if imdb_id:
                        key_id = imdb_id
                        print(
                            adjust_space(
                                current_length,
                                "| GUID map | {} | {:<46} | {:<6} | {}".format(
                                    "^" if update == True else "+", item.guid,
                                    key_id, item.title)))
                        update_guid_map(config_path, item.guid, imdb_id=key_id)
            elif item_type == 'imdb' and plex.library_type == "movie":
                # Check GUID map for TMDb ID
                key_id = query_guid_map(config_path, item.guid, 'tmdb_id')
                if TMDB.valid and key_id is None:
                    key_id = imdb_tools.imdb_get_tmdb(config_path, check_id)
                    if key_id:
                        print(
                            adjust_space(
                                current_length,
                                "| GUID map | {} | {:<46} | {:<6} | {}".format(
                                    "^" if update == True else "+", item.guid,
                                    key_id, item.title)))
                        update_guid_map(config_path, item.guid, tmdb_id=key_id)
                if TraktClient.valid and key_id is None:
                    key_id = trakt_tools.trakt_imdb_to_tmdb(
                        config_path, check_id)
                    if key_id:
                        print(
                            adjust_space(
                                current_length,
                                "| GUID map | {} | {:<46} | {:<6} | {}".format(
                                    "^" if update == True else "+", item.guid,
                                    key_id, item.title)))
                        update_guid_map(config_path, item.guid, tmdb_id=key_id)
                if key_id is None:
                    if TMDB.valid and TraktClient.valid:
                        error_message = "Unable to convert IMDb ID: {} to TMDb ID using TMDb or Trakt".format(
                            check_id)
                    elif TMDB.valid:
                        error_message = "Unable to convert IMDb ID: {} to TMDb ID using TMDb".format(
                            check_id)
                    elif TraktClient.valid:
                        error_message = "Unable to convert IMDb ID: {} to TMDb ID using Trakt".format(
                            check_id)
                    else:
                        error_message = "Configure TMDb or Trakt to covert IMDb ID: {} to TMDb ID".format(
                            check_id)
            elif item_type == 'themoviedb' and plex.library_type == "movie":
                tmdbapi = tmovie.details(check_id)
                if hasattr(tmdbapi, 'id'):
                    key_id = tmdbapi.id
                else:
                    key_id = None
                    error_message = "TMDb ID: {} Invalid".format(check_id)
            elif item_type == 'thetvdb' and plex.library_type == "show":
                key_id = check_id
            elif item_type == 'themoviedb' and plex.library_type == "show":
                key_id = None
                if TMDB.valid and key_id is None:
                    key_id = imdb_tools.tmdb_get_tvdb(config_path, check_id)
                if TraktClient.valid and key_id is None:
                    key_id = trakt_tools.trakt_tmdb_to_tvdb(
                        config_path, check_id)
                if key_id is None:
                    if TMDB.valid and TraktClient.valid:
                        error_message = "Unable to convert TMDb ID: {} to TVDb ID using TMDb or Trakt".format(
                            check_id)
                    elif TMDB.valid:
                        error_message = "Unable to convert TMDb ID: {} to TVDb ID using TMDb".format(
                            check_id)
                    elif TraktClient.valid:
                        error_message = "Unable to convert TMDb ID: {} to TVDb ID using Trakt".format(
                            check_id)
                    else:
                        error_message = "Configure TMDb or Trakt to covert TMDb ID: {} to TVDb ID".format(
                            check_id)
            elif item_type == "local":
                key_id = None
                error_message = "No match in Plex"
            else:
                key_id = None
                error_message = "Agent {} not supported".format(item_type)
            if key_id:
                plex_map[key_id] = item.ratingKey
            else:
                print(
                    adjust_space(
                        current_length, "| {} {:<46} | {} for {}".format(
                            "Cache | ! |", item.guid, error_message,
                            item.title)))
        print(
            adjust_space(
                current_length, "| Processed {} {}".format(
                    len(plex_items),
                    "Movies" if plex.library_type == "movie" else "Shows")))
    except:
        print()
        raise
    return plex_map
def get_map(config_path, plex):
    plex_map = {}
    current_length = 0
    current_count = 0
    if TMDB.valid:
        tmdb = TMDb()
        tmdb.api_key = TMDB(config_path).apikey
        tmovie = TMDb_Movie()
    print("|")
    if plex.cache:
        create_cache(config_path)
    print("| Mapping Plex {}".format("Movies" if plex.library_type ==
                                     "movie" else "Shows"))
    plex_items = plex.Library.all()
    try:
        for item in plex_items:
            current_count += 1
            print_display = "| Processing: {}/{} {}".format(
                current_count, len(plex_items), item.title)
            print(adjust_space(current_length, print_display), end="\r")
            current_length = len(print_display)
            update = None
            key_id = None
            error_message = "Unable to map {} ID".format(
                "TMDb" if plex.library_type == "movie" else "TVDb")
            if plex.cache == True:
                key_id, update = query_cache(config_path, plex.cache_interval,
                                             item.guid)
            if key_id is None or update:
                guid = urlparse(item.guid)
                item_type = guid.scheme.split('.')[-1]
                check_id = guid.netloc
                if item_type == 'plex' and plex.library_type == "movie":
                    key_id = new_movie_agent_get_tmdb(plex, item)
                elif item_type == 'imdb' and plex.library_type == "movie":
                    key_id = None
                    if TMDB.valid and key_id is None:
                        key_id = imdb_tools.imdb_get_tmdb(
                            config_path, check_id)
                    if TraktClient.valid and key_id is None:
                        key_id = trakt_tools.trakt_imdb_to_tmdb(
                            config_path, check_id)
                    if key_id is None:
                        if TMDB.valid and TraktClient.valid:
                            error_message = "Unable to convert IMDb ID: {} to TMDb ID using TMDb or Trakt".format(
                                check_id)
                        elif TMDB.valid:
                            error_message = "Unable to convert IMDb ID: {} to TMDb ID using TMDb".format(
                                check_id)
                        elif TraktClient.valid:
                            error_message = "Unable to convert IMDb ID: {} to TMDb ID using Trakt".format(
                                check_id)
                        else:
                            error_message = "Configure TMDb or Trakt to covert IMDb ID: {} to TMDb ID".format(
                                check_id)
                elif item_type == 'themoviedb' and plex.library_type == "movie":
                    tmdbapi = tmovie.details(check_id)
                    if hasattr(tmdbapi, 'id'):
                        key_id = tmdbapi.id
                    else:
                        key_id = None
                        error_message = "TMDb ID: {} Invalid".format(check_id)
                elif item_type == 'thetvdb' and plex.library_type == "show":
                    key_id = check_id
                elif item_type == 'themoviedb' and plex.library_type == "show":
                    key_id = None
                    if TMDB.valid and key_id is None:
                        key_id = imdb_tools.tmdb_get_tvdb(
                            config_path, check_id)
                    if TraktClient.valid and key_id is None:
                        key_id = trakt_tools.trakt_tmdb_to_tvdb(
                            config_path, check_id)
                    if key_id is None:
                        if TMDB.valid and TraktClient.valid:
                            error_message = "Unable to convert TMDb ID: {} to TVDb ID using TMDb or Trakt".format(
                                check_id)
                        elif TMDB.valid:
                            error_message = "Unable to convert TMDb ID: {} to TVDb ID using TMDb".format(
                                check_id)
                        elif TraktClient.valid:
                            error_message = "Unable to convert TMDb ID: {} to TVDb ID using Trakt".format(
                                check_id)
                        else:
                            error_message = "Configure TMDb or Trakt to covert TMDb ID: {} to TVDb ID".format(
                                check_id)
                elif item_type == "local":
                    key_id = None
                    error_message = "No match in Plex"
                else:
                    key_id = None
                    error_message = "Agent {} not supported".format(item_type)
                if plex.cache == True and key_id:
                    print(
                        adjust_space(
                            current_length,
                            "| Cache | {} | {:<46} | {:<6} | {}".format(
                                "^" if update == True else "+", item.guid,
                                key_id, item.title)))
                    update_cache(config_path, item.guid, key_id,
                                 True if update == True else False,
                                 plex.cache_interval)
            if key_id:
                plex_map[key_id] = item.ratingKey
            else:
                print(
                    adjust_space(
                        current_length, "| {} {:<46} | {} for {}".format(
                            "Cache | ! |"
                            if plex.cache == True else "Mapping Error:",
                            item.guid, error_message, item.title)))
        print(
            adjust_space(
                current_length, "| Processed {} {}".format(
                    len(plex_items),
                    "Movies" if plex.library_type == "movie" else "Shows")))
    except:
        print()
        raise
    return plex_map