-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.py
53 lines (46 loc) · 1.9 KB
/
news.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
from newspaper import Article
from newsapi.newsapi_client import NewsApiClient
from model import predict
newsClient=NewsApiClient(api_key='7d125ba012bc447681da91239d255267')
news_feed = newsClient.get_top_headlines( language='en',page_size=80)
def getArticlePredict(url):
article = downloadArticle(url)
result = predict(article.text)
return {'url':url,
'title':article.title,
'text':article.text,
'image':article.top_image,
'pred_result':result[0],
'pred_score':result[1]}
def downloadArticle(url):
article=Article(url)
article.download()
article.parse()
return article
def getSpecifArticle(currentReal=0,currentFake=0, index=0, maxReal=0,maxFake=0):
global news_feed
for i in range(index, len(news_feed['articles'])):
# print(f"$%$%$$$%$% >>>>> {i}")
article = news_feed['articles'][i]
try:
result = predict(downloadArticle(article['url']).text)
except:
continue
article['pred_result']= result[0]
article['pred_score']= result[1]
article['date'] = article['publishedAt'][0:10]
print(i,len(news_feed), index, currentReal ,article['title'][:10])
if maxReal == 0 and maxFake ==0:
return article, 0,0, i+1
if article['pred_result'].upper() == "FAKE" and currentFake < maxFake:
return article, currentReal,currentFake+1, i+1
elif article['pred_result'].upper() == "REAL" and currentReal < maxReal:
return article, currentReal+1,currentFake, i+1
else:
article = news_feed['articles'][0]
# print(len(news_feed), index, currentReal )
result = predict(downloadArticle(article['url']).text)
article['pred_result']= result[0]
article['pred_score']= result[1]
article['date'] = article['publishedAt'][0:10]
return article, currentReal,currentFake, 20