Example #1
0
def convertId(fromType, toType, id):
    logger.info("Converting from %s value %s to %s " % (fromType, id, toType))
    #Clean up names
    fromType = fromType.replace("rid", "tvrage").replace("id", "")
    toType = toType.replace("rid", "tvrage").replace("id", "")
    if fromType.replace("rid", "tvrage").replace("id", "") == toType.replace(
            "rid", "tvrage").replace("id", ""):
        return id

    if toType != "title" and not canConvertId(fromType, toType):
        logger.error("Unable to convert from %s to %s" % (fromType, toType))
        return None

    hasCacheEntry, result = _tryFromCache(fromType, toType, id)
    if hasCacheEntry:
        logger.debug("Found conversion from %s value %s to %s in cache" %
                     (fromType, id, toType))

    elif fromType == "imdb":
        convertedId, title = imdbid_to_tmdbid(id)
        if convertedId is None:
            return None
        result = MovieIdCache(tmdb=convertedId, imdb=id, title=title)
        result.save()

    elif fromType == "tmdb":
        convertedId, title = tmdbid_to_imdbid(id)
        if convertedId is None:
            return None
        result = MovieIdCache(imdb=convertedId, tmdb=id, title=title)
        result.save()

    elif fromType in ("tvrage", "tvdb", "tvmaze"):
        fromType = fromType.replace("tvdb",
                                    "thetvdb")  #TVMaze uses "thetvdb"...
        result = TvMaze.byId(fromType, id)
        if result is None:
            return None
        result = _cacheTvMazeIds(result)

    if result is None:
        return None
    wantedType = getattr(result, toType)
    if wantedType is None:
        return None

    logger.info("Successfully converted from %s value %s to %s " %
                (fromType, id, toType))
    if isinstance(wantedType, int):
        return str(wantedType)
    return wantedType
Example #2
0
def convertId(fromType, toType, id):
    logger.info("Converting %s value %s from %s " % (toType, id, fromType))
    #Clean up names
    fromType = fromType.replace("rid", "tvrage").replace("id", "")
    toType = toType.replace("rid", "tvrage").replace("id", "")

    if fromType.replace("rid", "tvrage").replace("id", "") == toType.replace(
            "rid", "tvrage").replace("id", ""):
        return id

    if not canConvertId(fromType, toType):
        logger.error("Unable to convert from %s to %s" % (fromType, toType))
        return None

    hasCacheEntry, fromCache = _tryFromCache(fromType, toType, id)
    if hasCacheEntry:
        logger.debug("Returning %s value %s from %s from cache" %
                     (toType, id, fromType))
        return fromCache

    if fromType == "imdb":
        result = imdbid_to_tmdbid(id)
        MovieIdCache(tmdb=result, imdb=id).save()
        return result

    if fromType == "tmdb":
        result = tmdbid_to_imdbid(id)
        MovieIdCache(imdb=result, tmdb=id).save()
        return result

    if fromType in ("tvrage", "tvdb", "tvmaze"):
        fromType = fromType.replace("tvdb",
                                    "thetvdb")  #TVMaze uses "thetvdb"...
        result = TvMaze.byId(fromType, id)
        if result is None:
            return None
        _cacheTvMazeIds(result)
        if toType == "tvdb":
            return str(result.tvdbid)
        if toType == "tvmaze":
            return str(result.tvmazeid)
        if toType == "tvrage":
            return str(result.rageid)

    return None