def testIsTargetType(url, target):
    options = {0: actor,
               1: film
               }

    query = options[target](url)
    uri = runQuery_returnBindings(query)
    return uri
Exemplo n.º 2
0
def getRdfFromUrl(url, requestType):
    # ATTENTION. Si on modifie les requêtes associées aux indices, il faut supprimer (à la main) les fichiers en cache !!!
    options = {0: subject, 1: item, 2: subjectAndItem}
    cache_file = "{0}/{1}_{2}.txt".format(
        CACHE_DIRECTORY, url.replace("http://", "").replace("/", "_").replace(":", "_"), requestType
    )
    # Try finding url dbpedia content in cache
    if os.path.isfile(cache_file):
        # print('cache found')
        cache_content = False
        with open(cache_file, "r", encoding="utf-8") as f:
            try:
                cache_content = ast.literal_eval(ast.literal_eval(f.read()).decode("utf-8"))
                if len(cache_content) == 0:  # Not loaded correctly
                    cache_content = False
                else:
                    # print("Loaded {0} from cache".format(cache_file))
                    pass
            except:
                pass
        # If the cache_content is still false, remove existing invalid cache file + send request
        if not cache_content:
            os.remove(cache_file)
            # print("Error cache loading {0}".format(cache_file))
            return getRdfFromUrl(url, requestType)
    # Else, query dbpedia
    else:
        # print("Query dbpedia {0}".format(url))
        query = options[requestType](url)
        # print(query)
        cache_content = runQuery_returnBindings(query)

        # Save in cache
        if not os.path.exists(CACHE_DIRECTORY):
            os.makedirs(CACHE_DIRECTORY)
        try:
            with open(cache_file, "w") as f:
                f.write(str(str(cache_content).encode("utf-8")))
        except:
            # print('Cache writing error {0}'.format(cache_file))
            pass

    return cache_content