-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
460 lines (342 loc) · 13.7 KB
/
app.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import datetime
import json
import os
import re
import warnings
# suppress UserWarning from Transformers
warnings.filterwarnings("ignore")
import csv
from urllib.parse import urlparse
import pandas as pd
import requests
import torch
import validators
from transformers import AutoModelForSequenceClassification, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_model")
model = AutoModelForSequenceClassification.from_pretrained("stevhliu/my_awesome_model")
SENTIMENT_CLASSIFIER_CONFIDENCE = 0.8
CONSENSUS_STRATEGY = "in_one_or_more"
USER_AGENT = "Mozilla/5.0; disinfo-domains-checker/0.1"
CATEGORIES_TO_FLAG = {
"Satire": [
re.compile(r"Satire", re.IGNORECASE),
re.compile(r"Satirical", re.IGNORECASE),
],
}
# url2table heading
KNOWN_LISTS = {
"https://en.wikipedia.org/wiki/List_of_fake_news_websites": "Domain",
"https://en.wikipedia.org/wiki/List_of_miscellaneous_fake_news_websites": "Domain",
"https://en.wikipedia.org/wiki/List_of_corporate_disinformation_website_campaigns": "Domain",
"https://en.wikipedia.org/wiki/List_of_political_disinformation_website_campaigns_in_the_United_States": "Domain",
"https://en.wikipedia.org/wiki/List_of_political_disinformation_website_campaigns": "Domain",
"https://en.wikipedia.org/wiki/List_of_satirical_fake_news_websites": "Domain",
"https://en.wikipedia.org/wiki/List_of_fake_news_troll_farms": "Domain",
}
KNOWN_CSV_LISTS = {}
CACHE_DIRECTORY = ".disinfo-domains/cache" # os.path.join("~", ".disinfo-domains", "cache")
os.makedirs(CACHE_DIRECTORY, exist_ok=True)
global active_cache
global active_cache_day
active_cache_day = None
active_cache = {}
def get_day_cache(day=datetime.datetime.now().strftime("%Y-%m-%d")):
"""
Retrieve the cache for a specific day.
If the cache does not exist, an empty dictionary will be returned.
If the cache exists, the cache will be returned.
If the cache is for today, the active cache will be returned.
:param day: The day to retrieve the cache for.
:return: The cache for the specified day.
"""
print("Reading cache for", day)
global active_cache
global active_cache_day
# check active cache for today
if active_cache_day == datetime.datetime.now().strftime(
"%Y-%m-%d"
) and day == datetime.datetime.now().strftime("%Y-%m-%d"):
return active_cache
print("Reading cache for", day, "from file")
cache_file = os.path.join(CACHE_DIRECTORY, day + ".json")
active_cache_day = datetime.datetime.now().strftime("%Y-%m-%d")
if os.path.exists(cache_file):
with open(cache_file, "r") as f:
return json.load(f)
return {}
def save_to_cache(cache, data, key, day=datetime.datetime.now().strftime("%Y-%m-%d")):
"""
Save a value to the cache.
This function will set a value in the cache if the value does not exist, or merge the value with the existing value.
Merging is supported specifically because this package only deals with flat lists of items.
:param cache: The cache to save the value to.
:param data: The data to save.
:param key: The key to save the data under.
:param day: The day to save the data for.
:return: None
"""
cache_file = os.path.join(CACHE_DIRECTORY, day + ".json")
if key not in cache:
cache[key] = data
else:
cache[key] = list(set(cache[key] + data))
with open(cache_file, "w") as f:
json.dump(cache, f)
global active_cache
global active_cache_day
active_cache = cache
active_cache_day = day
def consensus(
domain: str,
n: int = 3,
consensus_strategy: str = "in_one_or_more",
consensus: float = 0.75,
):
"""
Check for a consensus of categories.
This function will retrieve the categories for the last n days and check for a consensus of categories.
The following strategies are supported:
- percent: A percentage of the days must have the category.
- majority: A majority of the days must have the category.
- unanimous: All days must have the category.
- in_one_or_more: The category must be in one or more days.
:param domain: The domain to check for a consensus.
:param n: The number of days to check.
:param consensus_strategy: The strategy to use.
:param consensus: The consensus threshold.
:return: A list of problematic categories.
"""
days = [datetime.datetime.now().strftime("%Y-%m-%d")]
if n == 1:
return get_day_cache()[domain]
for num in range(1, n):
days.append(
(datetime.datetime.now() - datetime.timedelta(days=num)).strftime(
"%Y-%m-%d"
)
)
categories = [get_day_cache(day).get(domain, []) for day in days]
category_count = {}
for day in categories:
for category in day:
if category not in category_count:
category_count[category] = 1
else:
category_count[category] += 1
problematic_categories = []
if consensus_strategy == "percent":
for category, count in category_count.items():
if count >= n * consensus:
problematic_categories.append(category)
elif consensus_strategy == "majority":
for category, count in category_count.items():
if count >= n / 2:
problematic_categories.append(category)
elif consensus_strategy == "unanimous":
for category, count in category_count.items():
if count == n:
problematic_categories.append(category)
elif consensus_strategy == "in_one_or_more":
for category, count in category_count.items():
if count >= 1:
problematic_categories.append(category)
return problematic_categories
def extract_categories(content: str) -> list:
"""
Extract all categories from a Wikipedia page.
:param content: The content of the Wikipedia page.
:return: A list of categories.
"""
categories = re.findall(r"\[\[Category:(.*?)\]\]", content)
categories = [re.sub(r"\|.*", "", category) for category in categories]
return categories
def extract_known_problematic_websites(cache: str, url: str) -> list:
"""
Extract known problematic websites from a specified Wikipedia page.
:param url: The URL of the Wikipedia page.
:return: A list of known problematic websites.
"""
heading = KNOWN_LISTS[url]
if url in cache:
result = cache["known_problematic_websites"]
else:
last_modified = cache.get("last_modified", {}).get(url, None)
headers = {"User-Agent": USER_AGENT}
if last_modified:
headers["If-Modified-Since"] = last_modified
try:
response = requests.get(
url,
headers=headers,
timeout=10,
)
except requests.exceptions.RequestException as e:
print("Error fetching", url, e)
return []
if not cache.get("last_modified"):
cache["last_modified"] = {}
cache["last_modified"][url] = response.headers.get("Last-Modified", None)
if response.status_code == 304:
result = cache.get("known_problematic_websites", [])
save_to_cache(cache, result, "known_problematic_websites")
return result
tables = pd.read_html(response.text)
flat_table = pd.concat(tables)
result = flat_table[heading].tolist()
# remove [.com] and nan
result = [x.replace("[.]", ".").lower() for x in result if isinstance(x, str)]
save_to_cache(cache, result, "known_problematic_websites")
return result
def extract_known_problematic_websites_csv(csv_file: str) -> list:
"""
Extract known problematic websites from a specified CSV file.
This is not used, but may be useful in scenarios where you want to restrict websites you have identified as problematic.
:param csv_file: The CSV file to extract the known problematic websites from.
:return: A list of known problematic websites.
"""
heading = KNOWN_CSV_LISTS[csv_file]
# header row is always the first row
with open(csv_file, newline="") as f:
reader = csv.reader(f)
data = list(reader)
data[0] = [x.replace('"', "").strip() for x in data[0]]
data[0] = [x.replace("\ufeff", "") for x in data[0]]
flat_table = pd.DataFrame(data[1:], columns=data[0])
flat_table = flat_table.apply(lambda x: x.str.lower() if x.dtype == "object" else x)
result = flat_table[heading].tolist()
# remove [.com] and nan
result = [x.replace("[.]", ".") for x in result if isinstance(x, str)]
return result
def get_wiki_page(title: str):
"""
Get the content of a Wikipedia page.
This strategy is taken since the Wikipedia Categories API returns the category
associated with a redirect, not the page to which the redirects -- which may
involve one or more hops -- point.
:param title: The title of the Wikipedia page.
:return: The content of the Wikipedia page.
"""
url = (
"https://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles="
+ title
+ "&rvslots=*&rvprop=content&formatversion=2&format=json"
)
response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
response_code = response.status_code
if response_code != 200:
return None, response_code
if "missing" in response.json()["query"]["pages"][0]:
return None, 404
content = response.json()["query"]["pages"][0]["revisions"][0]["slots"]["main"][
"content"
]
if content.startswith("#REDIRECT"):
text = re.search(r"\[\[(.*?)\]\]", content).group(1)
if "#" in text:
text = text.split("#")[0]
return get_wiki_page(text)
return content, response_code
def get_sentiment(text: str) -> str:
"""
Get the sentiment of a category.
:param text: The text to get the sentiment for.
:return: The sentiment of the text.
"""
inputs = tokenizer(text, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
predicted_class_idx = torch.argmax(logits).item()
id2label = model.config.id2label[predicted_class_idx]
# if LABEL_1 or confidence of LABEL_0 < 0.8
return (
"positive"
if id2label == "LABEL_1"
or torch.softmax(logits, dim=1)[0][0] < SENTIMENT_CLASSIFIER_CONFIDENCE
else "negative"
)
def generate_report(url: str, consensus = True) -> dict:
"""
Generate a report for a given URL.
The report contains the following:
- Flagged categories
- Negative sentiment categories
- Known problematic websites
:param url: The URL to generate a report for.
:return: A dictionary containing the report.
"""
if not validators.url(url):
url = "https://" + url
domain = urlparse(url).netloc
if domain.startswith("www."):
domain = domain[4:]
domain = domain.strip()
report = {
"flagged_categories": [],
"negative_sentiment_categories": [],
"known_problematic_websites": [],
"all_categories": [],
}
# if domain is in cache, return it
cache = get_day_cache()
if domain in cache:
result, status_code = None, 200
result, status_code = get_wiki_page(domain)
if status_code != 404:
if domain in cache:
categories = cache[domain]
else:
categories = extract_categories(result)
sentiments = {category: get_sentiment(category) for category in categories}
report["all_categories"] = categories
if any(sentiment == "negative" for sentiment in sentiments.values()):
negative_sentiment_categories_today = [
category
for category, sentiment in sentiments.items()
if sentiment == "negative"
]
save_to_cache(cache, negative_sentiment_categories_today, domain)
if consensus:
consensus_report = consensus(domain, consensus_strategy=CONSENSUS_STRATEGY)
report["negative_sentiment_categories"] = consensus_report
else:
report["negative_sentiment_categories"] = negative_sentiment_categories_today
for site in KNOWN_LISTS.keys():
if any(
domain in extract_known_problematic_websites(cache, site)
for domain in sentiments.keys()
):
report["known_problematic_websites"].extend(
[
domain
for domain in sentiments.keys()
if domain in extract_known_problematic_websites(cache, site)
]
)
for category, regexes in CATEGORIES_TO_FLAG.items():
for regex in regexes:
if any(regex.search(category) for category in categories):
report["flagged_categories"].append(category)
for site in KNOWN_CSV_LISTS.keys():
if any(
domain in extract_known_problematic_websites_csv(site)
for domain in [domain]
):
report["known_problematic_websites"].extend(
[
domain
for domain in [domain]
if domain in extract_known_problematic_websites_csv(site)
]
)
return report
DOMAIN = "ABCNews.com.co"
report = generate_report(DOMAIN.lower())
if any(len(value) > 0 for value in report.values()):
print("Website is flagged for the following reasons:")
for key, value in report.items():
if key == "all_categories":
continue
if len(value) > 0:
print(key + ": " + ", ".join(value))
else:
print("Website is not flagged")