-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
136 lines (125 loc) · 3.82 KB
/
Copy pathmain.py
File metadata and controls
136 lines (125 loc) · 3.82 KB
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
import json, requests, nltk
from textblob import TextBlob
from google.cloud import language
from flask import Flask
"""
'breitbart-news',
'buzzfeed',
'entertainment-weekly',
'espn',
'espn-cric-info',
'fox-sports',
'ign',
'talksport',
'the-sport-bible',
'football-italia',
'bbc-sport',
'nfl-news',
'polygon',
'wirtschafts-woche'
"""
call = requests.get('https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=b506a06468994fcc9ed9f55451000921')
payload = json.loads(call.text)
print(payload)
sources= ['abc-news-au',
'al-jazeera-english',
'ars-technica',
'associated-press',
'bbc-news',
'bild',
'bloomberg',
'business-insider',
'business-insider-uk',
'cnbc',
'cnn',
'daily-mail',
'der-tagesspiegel',
'die-zeit',
'engadget',
'financial-times',
'focus',
'fortune',
'four-four-two',
'google-news',
'gruenderszene',
'hacker-news',
'handelsblatt',
'independent',
'mashable',
'metro',
'mirror',
'mtv-news',
'mtv-news-uk',
'national-geographic',
'new-scientist',
'newsweek',
'new-york-magazine',
'recode',
'reddit-r-all',
'reuters',
'spiegel-online',
't3n',
'techcrunch',
'techradar',
'the-economist',
'the-guardian-au',
'the-guardian-uk',
'the-hindu',
'the-huffington-post',
'the-lad-bible',
'the-new-york-times',
'the-next-web',
'the-telegraph',
'the-times-of-india',
'the-verge',
'the-wall-street-journal',
'the-washington-post',
'time',
'usa-today',
'wired-de', ]
# for api in sources:
# # print('https://newsapi.org/v1/articles?source=' + api + '&sortBy=latest&apiKey=b506a06468994fcc9ed9f55451000921')
# call = requests.get('https://newsapi.org/v1/articles?source=' + api + '&sortBy=top&apiKey=b506a06468994fcc9ed9f55451000921')
# payload = json.loads(call.text)
# # print(payload)
# if payload['status'] != 'error':
# print(payload['source'])
# for a in payload['articles']:
# print('\t' + a['title'])
# Instantiates a client
language_client = language.Client()
# from flask_request_handler import views;
app = Flask(__name__);
# The text to analyze
@app.route('/res.json')
def start():
articles = []
call = requests.get('https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=b506a06468994fcc9ed9f55451000921')
payload = json.loads(call.text)
# print(payload)
if payload['status'] != 'error':
# print(payload['source'])
for a in payload['articles']:
text = a['title'] #'Jet collides with truck at LAX'
document = language_client.document_from_text(text)
# # Detects the sentiment of the text
# sentiment = document.analyze_sentiment().sentiment
# print(document.analyze_sentiment())
# print('Text: {}'.format(text))
# print('Sentiment: {}, {}'.format(sentiment.score, sentiment.magnitude))
try:
entities = document.analyze_entities().entities
except:
pass
for b in entities:
# print('\t', b.name, b.entity_type)
if b.entity_type == 'LOCATION':
article = {}
article['article'] = a
article['weight'] = 1
article['location'] = b.name
articles.append(article)
# print(a['title'], b.name)
# with open('data.txt', 'w') as outfile:
return json.dumps(articles);
# print(entities[0].__dict__)