fact-check / google_fact_check_tool.py
Dustin Haring
restructure and fixes and improvements; added a debug variable to control debug prints
fddd74b
import requests
API_GOOGLE_SEARCH_KEY = "AIzaSyA4oDDFtPxAfmPC8EcfQrkByb9xKm2QfMc"
def query_fact_check_api(claim):
"""Queries the Google Fact Check Tools API for a given claim.
Args:
claim (str): The claim to search for fact checks.
Returns:
dict: The API response parsed as a JSON object.
"""
url = "https://factchecktools.googleapis.com/v1alpha1/claims:search"
params = {
"key": API_GOOGLE_SEARCH_KEY,
"query": claim,
}
response = requests.get(url, params=params)
response.raise_for_status() # Raise an exception for error HTTP statuses
return response.json()
def response_break_out(response):
if response.get("claims"):
iteration = 0
answer = ""
for claim in response["claims"]:
answer = answer + """claim: """ + claim['text'] + "\n"
for review in claim["claimReview"]:
answer = answer + """publisher: """ + review['publisher']['name'] + "\n"
answer = answer + """rating: """ + review['textualRating'] + "\n"
if iteration >= 1:
break
iteration += 1
else:
answer = """No fact checks found for this claim."""
return answer