-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalchemynewsparser.py
156 lines (139 loc) · 5.95 KB
/
alchemynewsparser.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
import json
from watson_developer_cloud import AlchemyDataNewsV1, watson_developer_cloud_service
import time
import datetime
class AlchemyNewsParser(object):
"""docstring for AlchemyNewsParser"""
def __init__(self, arg):
self.application_state = dict()
self.application_state.update({"call_count": 0})
self.application_state.update({"task_log": "parser init"})
self.arg = arg
self.alchemy_data_news = AlchemyDataNewsV1(api_key=self.arg["key"])
self.news_doc_responds = list()
self.next = 'None'
self.url_base_url = "https://access.alchemyapi.com/calls/data/GetNews?"
self.url_key = "apikey="
self.url_returns = "&return="
self.retrun_fields_default = "enriched.url.title,enriched.url.url,enriched.url.enrichedTitle.entities,enriched.url.enrichedTitle.docSentiment,enriched.url.enrichedTitle.concepts"
self.url_start_date = "&start="
self.url_end_date = "&end="
self.url_q_field = "&q."
self.q_field_default = "enriched.url.cleanedTitle="
self.url_count = "&count="
self.url_outputMode = "&outputMode=json"
"""alternatively, use requests library to call the api directly and
without using the watson cloud developers library"""
def urlMaker(self):
self.made_url = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}".format(self.url_base_url, self.url_key,self.arg["key"],self.url_returns,self.retrun_fields_default,self.url_start_date,self.start,self.url_end_date,self.end,self.url_q_field,self.q_field_default,self.text_for_query,self.url_outputMode)
# print self.made_url
def urlMakerWithNext(self):
self.made_url = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}".format(self.url_base_url, self.url_key,self.arg["key"],self.url_returns,self.retrun_fields_default,self.url_start_date,self.start,self.url_end_date,self.end,self.url_q_field,self.q_field_default,self.text_for_query,"&next=", self.next,self.url_outputMode)
def fromDateOf(self, start_date):
self.start="%s" % (start_date)
def toDateOf(self, end_date):
self.end="%s" % (end_date)
def queryFor(self, text_for_query):
self.text_for_query = text_for_query
def checkStatusAndSave(self, results, fw):
doc_resp = dict()
print "checking status"
if results["status"] == "OK":
self.application_state.update({"task_log": "results status ok, saving ..."})
print self.application_state
doc_resp.update({'page_number': self.application_state["call_count"], 'result': results["result"]})
json.dump(doc_resp,fw, indent=2)
# fw.write("\n")
# fw.flush()
def checkNext(self,results):
self.application_state.update({"task_log": "checking results.next to see if empty"})
try:
if results["result"]["next"] != '':
self.application_state.update({"task_log": "results.next is not empty"})
print self.application_state
self.next = results["result"]["next"]
else:
self.application_state.update({"task_log": "setting self.next to None before break"})
print self.application_state
self.next = 'None'
self.still_more = False
return "break"
except:
self.next = 'Done'
self.application_state.update({"task_log": "Checking next failed -- setting to Done"})
print self.application_state
return "break"
def getNews(self, fw):
page_number = 0
self.still_more = True
self.application_state.update({"task_log": "getNews method started"})
print self.application_state
while self.still_more:
time.sleep(9)
print "self.next", self.next
if (self.next == 'None') and (self.next != "Done"):
try:
self.application_state.update({"task_log": "next is none, trying the first call"})
print self.application_state
results = self.alchemy_data_news.get_news_documents(
start=self.start,
end=self.end,
return_fields=['enriched.url.title',
'enriched.url.url',
'enriched.url.author',
'enriched.url.publicationDate',
'enriched.url.enrichedTitle.entities',
'enriched.url.enrichedTitle.docSentiment',
'enriched.url.enrichedTitle.concepts',
'enriched.url.enrichedTitle.taxonomy',
'enriched.url.title'],
query_fields={'q.enriched.url.enrichedTitle.entities.entity': '|text={0}|'.format(self.text_for_query)})
self.application_state["call_count"] += 1
self.checkStatusAndSave(results, fw)
checkBreak = self.checkNext(results)
if checkBreak == "break":
break
except watson_developer_cloud_service.WatsonException:
self.application_state.update({"task_log": "First call try failed"})
print self.application_state
print watson_developer_cloud_service.WatsonException
elif self.next != "Done":
print "else of the check for next"
try:
self.application_state.update({"task_log": "try with a next-- before call"})
print self.application_state
results = self.alchemy_data_news.get_news_documents(
start=self.start,
end=self.end,
return_fields=['enriched.url.title',
'enriched.url.url',
'enriched.url.author',
'enriched.url.publicationDate',
'enriched.url.enrichedTitle.entities',
'enriched.url.enrichedTitle.docSentiment',
'enriched.url.enrichedTitle.concepts',
'enriched.url.enrichedTitle.taxonomy',
'enriched.url.title'],
query_fields={'q.enriched.url.enrichedTitle.entities.entity': '|text={0}|'.format(self.text_for_query)},
next_page=self.next)
self.application_state["call_count"] += 1
self.checkStatusAndSave(results, fw)
self.checkNext(results)
except watson_developer_cloud_service.WatsonException:
self.application_state.update({"task_log": "try with a next-- call failed"})
print self.application_state
print watson_developer_cloud_service.WatsonException
else:
self.still_more = False
break
def convertToJson(self, readfile, tofilename):
f = open(readfile, 'r')
with open(tofilename, 'a') as toconv:
for line in f:
l = line.split("\n")
for e in l:
try:
loadd = json.loads(e)
json.dump(loadd, toconv, indent=2)
except:
pass