-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
99 lines (79 loc) · 2.78 KB
/
api.py
File metadata and controls
99 lines (79 loc) · 2.78 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
import urllib, base64, requests, json, os
from textblob import TextBlob
def get_search_results(query):
headers = {
# Request headers
'Ocp-Apim-Subscription-Key': os.environ['Ocp-Apim-Subscription-Key'],
}
params = urllib.urlencode({
# Request parameters
'q': query,
'category': 'stocks',
'count': '80',
'offset': '0',
'freshness': 'Month',
'mkt': 'en-us',
'safeSearch': 'Moderate',
})
r = requests.get('https://api.cognitive.microsoft.com/bing/v5.0/news/search?%s' % params, headers=headers)
news_articles = json.loads(r.text)
article_urls = []
articles = []
for article in news_articles["value"]:
article_urls.append(article["url"])
articles.append(article)
article_thumbnails = []
for article in news_articles["value"]:
if 'image' in article.keys() and len(article_thumbnails) < 8:
article_thumbnails.append([article["name"], article["url"], article["image"]["thumbnail"]["contentUrl"], article["description"]])
return articles, article_thumbnails
def get_textblob_sentiment(text_list):
sentiments = []
for text in text_list:
blob = TextBlob(text)
for sentence in blob.sentences:
sentiments.append(sentence.sentiment.polarity)
return sentiments
def get_sentiment_scores(text_list):
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': os.environ['Ocp-Apim-Subscription-Key'],
}
params = {
'documents': []
}
for n, text in enumerate(text_list):
text_info = {
"language": "en",
"id": str(n),
"text": text
}
params['documents'].append(text_info)
params = json.dumps(params)
r = requests.post('https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment', data=params, headers=headers)
sentiments = json.loads(r.text)["documents"]
return [x["score"] for x in sentiments]
def get_text_topics(text_list):
headers = {
# Request headers
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': os.environ['Ocp-Apim-Subscription-Key'],
}
params = {
'documents': []
}
for n, text in enumerate(text_list):
text_info = {
"language": "en",
"id": str(n),
"text": text
}
params['documents'].append(text_info)
params = json.dumps(params)
r = requests.post('https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases', data=params, headers=headers)
sentiments = json.loads(r.text)["documents"]
out = []
for phrases in sentiments:
out.extend(phrases["keyPhrases"])
return out