/
GoogleScanner.go
117 lines (101 loc) · 2.86 KB
/
GoogleScanner.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package NewsScanner
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
//googleApiResp defines the response struct for Google fact checker APIs
type googleApiResp struct {
claims []claim `json:"claim"`
nextPageToken string `json:"nextPageToken`
}
//claim is an entity under googleApiResp
type claim struct {
text string `json:"text"`
clamant string `json:"clamant"`
claimDate string `json:"claimDate"`
claimReview []claimReview `json:"claimReview`
}
//claimReview is an entity under googleApiResp.claim
type claimReview struct {
publisher publisher `json:"publisher"`
url string `json:"url"`
title string `json:"title"`
reviewDate string `json:"reviewDate"`
textualRating string `json:"textualRating"`
languageCode string `json:"languageCode"`
}
//publisher is an entity under claimReview, represents the fact publisher
type publisher struct {
name string `json:"name"`
site string `json:"site"`
}
//GoogleScanner implements INewsScanner using google APIs in the backend
type GoogleScanner struct {
key string
baseApi string
maxAgeDays int
}
//NewGoogleScanner returns a new GoogleScanner
func NewGoogleScanner() *GoogleScanner {
return &GoogleScanner{
baseApi: "https://factchecktools.googleapis.com/v1alpha1/claims:search",
maxAgeDays: 10,
}
}
//SetMaxAgeDays sets the maximum age(in days) for the returned search resutls
func (g *GoogleScanner) SetMaxAgeDays(maxAgeDays int) {
g.maxAgeDays = maxAgeDays
}
//SetApiKey sets key to be used to access the APIs
func (g *GoogleScanner) SetApiKey(key string) {
g.key = key
}
//IsFake tells whether the supplied news is fake
func (g *GoogleScanner) IsFake(news string) (isFake bool, err error) {
reqUrl := fmt.Sprintf("%s?query=%s&languageCode=en-US&maxAgeDays=%d&key=%s", g.baseApi, url.PathEscape(news), g.maxAgeDays, g.key)
req, err := http.NewRequest("GET", reqUrl, nil)
if err != nil {
return
}
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(requestDump))
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
var respStruct googleApiResp
fmt.Println(string(respBody))
err = json.Unmarshal(respBody, &respStruct)
if err != nil {
return
}
//fakeCount: claims suggesting the news to be false
//totalCount: total facts/claims found for the news
var fakeCount, totalCount int
for _, c := range respStruct.claims {
for _, cr := range c.claimReview {
totalCount++
if strings.Contains(strings.ToLower(cr.textualRating), "false") {
fakeCount++
}
}
}
//if claims suggesting news to be fake >= 50% claims, news is fake
if 2*fakeCount >= totalCount {
return true, nil
}
return false, nil
}