-
Notifications
You must be signed in to change notification settings - Fork 1
/
AnalyzeNews.py
319 lines (269 loc) · 10.2 KB
/
AnalyzeNews.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# requests to connect to the grammar check API
import requests
# Article to scrape the webpage of the given url
from newspaper import Article
# json to parse the API response
import json
# regular expressions to search the article for quotes and citations
import re
import socket
import pprint
from pymongo import MongoClient
#Server
# import http.server
# import socketserver
#
# PORT = 8080
# Handler = http.server.SimpleHTTPRequestHandler
#
# with socketserver.TCPServer(("", PORT), Handler) as httpd:
# print("serving at port", PORT)
# httpd.serve_forever()
#end of server
import sys
client = MongoClient()
db = client['true-news']
# async def listen(socket, path):
# name = await socket.recv()
# print(f"< {name}")
#
# start_server = socket.getservbyport(12345, 'localhost')
# asyncio.get_event_loop().run_until_complete(start_server)
# asyncio.get_event_loop().run_forever()
# HOST = '127.0.0.1'
# PORT = 12345
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#
# s.bind((HOST, PORT))
# s.listen(1)
class ArticleReputation():
def __init__(self,url=""):
"""
Initialize building the reputation for an Article
"""
self.url = url
self.credible_host = 0 #0 is an unknown host, -1 is an unreliable host, 1 is a reliable host (host is the site the news is hosted on)
self.documents = db.documents
self.get_article()
self.reputation()
def check_grammar(self, text):
"""
Returns number or spelling mistakes in the Article
uses textgear API and my token
:param text: string
:return: int
"""
url = "https://api.textgears.com/check.php?text=" + text + "!&key=75YaNnAeqOCH5nf7"
response = requests.get(url)
try:
json_data = json.loads(response.text)
except:
print("JSON data from grammar check could not be loaded")
return 0
num_of_errors = len(json_data["errors"])
return num_of_errors
def add_article_to_db(self):
"""
Add a document of the articles information to the database
:return:
"""
document = {
"authors": self.authors,
"title": self.title,
"text": self.text,
"url": self.url,
"publish-date": self.publish_date
}
if self.credible_host == 1:
document_id = self.reliable.insert_one(document).inserted_id
self.documents.insert_one(document).inserted_id
elif self.credible_host == 0:
document_id = self.documents.insert_one(document).inserted_id
else:
document_id = self.unreliable.insert_one(document).inserted_id
self.documents.insert_one(document).inserted_id
print(document_id)
return document_id
# def socket(self):
# """
# Listen for a url from chrome extension
# :return:
# """
# #print("Waiting")
# #conn1, addr1 = s.accept()
# #print('Connected by', addr1)
# print("entering while loop")
# conn1, addr1 = s.accept()
# while 1:
# #cfile = conn1.makefile('rw', 0)
#
# try:
# data = conn1.recv(1024)
# except socket.error:
# print('error')
# if data:
# print("Data Recieved")
# print(data.decode('utf-8'))
# self.url = data.decode('utf-8')
def get_article(self):
"""
Given the url of an article
:param url:
:return:
"""
document_cursor = db.documents.find_one({"url": self.url})
if document_cursor:
document_id = document_cursor["_id"]
else:
document_id = None
if document_id:
print("This article was already in the database")
self.db_doc = db.documents.find_one({"_id": document_id})
self.title = self.db_doc["title"]
self.authors = self.db_doc["authors"]
self.text = self.db_doc["text"]
self.publish_date = self.db_doc["publish-date"]
return None
self.article = Article(self.url)
grammar_mistakes = self.check_grammar(self.article.title)
# try to download the article
try:
self.article.download()
except:
print("couldnt download")
return None
# try to parse the article
try:
self.article.parse()
except:
print("couldnt parse")
return None
# Try to use nlp on the article
try:
self.article.nlp()
except:
print("couldnt use nlp")
return None
self.title = self.article.title
self.authors = self.article.authors
self.text = self.article.text
self.publish_date = self.article.publish_date
print(self.title)
print(self.authors)
#self.grammar_mistakes = self.check_grammar(self.text)
# document_cursor = db.documents.find_one({"url": self.url})
# if document_cursor:
# document_id = document_cursor["_id"]
# else:
# document_id = None
# if document_id:
document_id=self.add_article_to_db()
for author in self.authors:
author_cursor = db.authors.find_one({"author":author})
if author_cursor:
#author_id = author_cursor["_id"]
if document_id != author_cursor["known_articles"]:
db.authors.updateOne({"author":author},{"known_articles":author_cursor["known_articles"].extend(document_id)})
else:
db.authors.insert_one({
"author": author,
"known_articles": [document_id]
})
def reputation(self):
"""
Calulates how likely this article is to be trust worthy
:return:
"""
rep_dict = {}
if self.text != "":
grammar_mistakes = self.check_grammar(self.text)
if grammar_mistakes > 0:
print("This article has "+ str(grammar_mistakes)+" spelling mistakes")
rep_dict["grammar_mistakes"] = "This article has "+ str(grammar_mistakes)+" spelling mistakes"
else:
print("This article does not have any spelling mistakes")
rep_dict["grammar_mistakes"] = "This article does not have any spelling mistakes"
bad_news = self.check_bad_newsource()
reliable_news = self.check_reliable_news()
if bad_news:
print("This article is from a source that is known for hosting unreliable news")
rep_dict["news_source"] = "This article is from a source that is known for hosting unreliable news"
elif reliable_news:
print("This article is from a reliable news source")
rep_dict["news_source"] = "This article is from a reliable news source"
else:
print("This article is hosted on an unknown news source")
rep_dict["news_source"] = "This article is hosted on an unknown news source"
return rep_dict
def collect_quotes(self):
"""
Return all quotes in the text of this article
:return:
"""
# use the regex
quotes = re.findall('"(.*?)"', self.text)
if quotes:
print(quotes)
return quotes
def hoaxy_api_lookup(self, query):
"""
Return information about the article's meta data from hoaxy_api:
More info: https://rapidapi.com/truthy/api/hoaxy
:return:
header = "default-application_3628393"
xkey = "a2412dd557mshf6c4ce57635dc78p1daa1bjsna5cf0cfd6317"
URL = "https://api-hoaxy.p.rapidapi.com/articles?sort_by=relevant&use_lucene_syntax=true&query"
"=pizzagate+AND+date_published%3A%5B2016-10-28+TO+2016-12-04%5D",
header=("X-RapidAPI-Key", "a2412dd557mshf6c4ce57635dc78p1daa1bjsna5cf0cfd6317"))
"""
#ToDo:
return None
def check_fake_news_detector(self):
"""
Return information if article is in fake news detector database.
more info https://github.com/fake-news-detector/fake-news-detector/blob/master/api/README.md
(currently look up has been unsuccessful)
:return:
"""
response = requests.get("https://api.fakenewsdetector.org/link/all",)
if response.status_code == 200:
x = json.loads(response.text)
return x
else:
return None
def check_bad_newsource(self):
"""
Returns True if the newsource is from a hardcoded list of not trustworthy news sources
Based on https://www.marketwatch.com/story/these-are-the-most-and-the-least-trusted-news-sources-in-the-us-2017-08-03
:return: boolean
"""
bad_news_sources = ["theonion.com", "70news.wordpress.com","Abcnews.com.co",
"Alternativemediasyndicate.com", "Americannews.com"]
for bad_news in bad_news_sources:
if bad_news in self.url:
self.credible_host = -1
return True
return False
def check_reliable_news(self):
"""
Returns True if the news source is from a hardcoded list of reliable news sources
Based on https://www.marketwatch.com/story/these-are-the-most-and-the-least-trusted-news-sources-in-the-us-2017-08-03
:return: boolean
"""
reliable_news_sources = ["economist.com","reuters.com","bbc.com","npr.org","pbs.org","theguardian.com","wsj.com","latimes.com"]
for reliable_news in reliable_news_sources:
if reliable_news in self.url:
self.credible_host = 1
return True
return False
#AR = ArticleReputation("https://educateinspirechange.org/nature/earth/cigarette-butts-are-the-oceans-single-largest-source-of-pollution/?fbclid=IwAR1z7VY0ZNpG2JkcWrRixiJvt2G0HAnX-3JTGnU6MLD4meO3jF5zhjzhRVc")
# AR.authors = "test_authors"
# AR.title = "test title"
# AR.text = "test text"
#AR.get_article()
#AR.add_article_to_db()
#AR.socket()
#AR.get_article()
# AR.collect_quotes()
# AR.reputation()