File size: 1,244 Bytes
fddd74b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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