Skip to content

Commit 495bccf

Browse files
committed
## [v8.9]() (2020-10-21)
**Added** - added `required` and `excluded` optional parameters to the TopicPage class to methods `addConcept`, `addKeyword`, `addCategory`, `addSource`, `addSourceLocation` and `addSourceGroup`. - exceptions are now raised by the SDK library in case of status codes 204, 400, 401 and 403. Previously, the requests were repeated if status code was not 200. In case of these status codes it however doesn't make sense to simply repeat the requests as the query will for sure return the same exception. The meaning of the above status codes is described on the [documentation page](https://eventregistry.org/documentation?tab=introduction).
1 parent 1963bdf commit 495bccf

6 files changed

Lines changed: 71 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
## [v8.9]() (2020-10-21)
4+
5+
**Added**
6+
7+
- added `required` and `excluded` optional parameters to the TopicPage class to methods `addConcept`, `addKeyword`, `addCategory`, `addSource`, `addSourceLocation` and `addSourceGroup`.
8+
- exceptions are now raised by the SDK library in case of status codes 204, 400, 401 and 403. Previously, the requests were repeated if status code was not 200. In case of these status codes it however doesn't make sense to simply repeat the requests as the query will for sure return the same exception. The meaning of the above status codes is described on the [documentation page](https://eventregistry.org/documentation?tab=introduction).
9+
10+
311
## [v8.8]() (2020-08-20)
412

513
**Added**

eventregistry/EventRegistry.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ def __init__(self,
8383
if printHostInfo:
8484
print("Event Registry host: %s" % (self._host))
8585
print("Text analytics host: %s" % (self._hostAnalytics))
86+
87+
# list of status codes - when we get them as a response from the call, we don't want to repeat the query as the response will likely always be the same
88+
self._stopStatusCodes = set([
89+
204, # Information not available. Request succeeded, but the requested information is not available.
90+
400, # Bad request. The request was unacceptable, most likely due to invalid or missing parameter.
91+
401, # User's limit reached. The user reached the limit of the tokens in his account. The requests are rejected.
92+
403, # Invalid account. The user's IP or account is disabled, potentially due to misuse.
93+
])
94+
8695
# check what is the version of your module compared to the latest one
8796
self.checkVersion()
8897

@@ -301,9 +310,10 @@ def jsonRequest(self, methodUrl, paramDict, customLogFName = None, allowUseOfArc
301310
if self._verboseOutput:
302311
print("endpoint: %s\nParams: %s" % (url, json.dumps(paramDict, indent=4)))
303312
self.printLastException()
304-
# in case of invalid input parameters, don't try to repeat the search
305-
if respInfo != None and (respInfo.status_code == 400 or respInfo.status_code == 530 or respInfo.status_code == 204):
306-
break
313+
# in case of invalid input parameters, don't try to repeat the search but we simply raise the same exception again
314+
if respInfo != None and respInfo.status_code in self._stopStatusCodes:
315+
raise ex
316+
# in case of the other exceptions (maybe the service is temporarily unavailable) we try to repeat the query
307317
print("The request will be automatically repeated in 3 seconds...")
308318
time.sleep(3) # sleep for X seconds on error
309319
self._lock.release()

eventregistry/TopicPage.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,67 +209,79 @@ def clearLocations(self):
209209
self.topicPage["locations"] = []
210210

211211

212-
def addConcept(self, conceptUri, weight, label = None, conceptType = None, required = False):
212+
def addConcept(self, conceptUri, weight, label = None, conceptType = None, required = False, excluded = False):
213213
"""
214214
add a relevant concept to the topic page
215215
@param conceptUri: uri of the concept to be added
216216
@param weight: importance of the provided concept (typically in range 1 - 50)
217+
@param required: if true, then all results will HAVE TO be annotated with this concept
218+
@param excluded: if true, then all results annotated with this concept will be ignored
217219
"""
218220
assert isinstance(weight, (float, int)), "weight value has to be a positive or negative integer"
219-
concept = {"uri": conceptUri, "wgt": weight, "required": required}
221+
assert not (required == True and excluded == True), "Parameters required and excluded can not be True at the same time"
222+
concept = {"uri": conceptUri, "wgt": weight, "required": required, "excluded": excluded }
220223
if label != None: concept["label"] = label
221224
if conceptType != None: concept["type"] = conceptType
222225
self.topicPage["concepts"].append(concept)
223226

224227

225-
def addKeyword(self, keyword, weight):
228+
def addKeyword(self, keyword, weight, required = False, excluded = False):
226229
"""
227230
add a relevant keyword to the topic page
228231
@param keyword: keyword or phrase to be added
229232
@param weight: importance of the provided keyword (typically in range 1 - 50)
233+
@param required: if true, then all results will HAVE TO mention this keyword to appear in the results
234+
@param excluded: if true, then no results that mention this keyword will be returned
230235
"""
231236
assert isinstance(weight, (float, int)), "weight value has to be a positive or negative integer"
232-
self.topicPage["keywords"].append({"keyword": keyword, "wgt": weight})
237+
assert not (required == True and excluded == True), "Parameters required and excluded can not be True at the same time"
238+
self.topicPage["keywords"].append({"keyword": keyword, "wgt": weight, "required": required, "excluded": excluded })
233239

234240

235-
def addCategory(self, categoryUri, weight):
241+
def addCategory(self, categoryUri, weight, required = False, excluded = False):
236242
"""
237243
add a relevant category to the topic page
238244
@param categoryUri: uri of the category to be added
239245
@param weight: importance of the provided category (typically in range 1 - 50)
246+
@param required: if true, then all results will HAVE TO be annotated with this category to appear in the results
247+
@param excluded: if true, then no results with this category will be returned
240248
"""
241249
assert isinstance(weight, (float, int)), "weight value has to be a positive or negative integer"
242-
self.topicPage["categories"].append({"uri": categoryUri, "wgt": weight})
250+
assert not (required == True and excluded == True), "Parameters required and excluded can not be True at the same time"
251+
self.topicPage["categories"].append({"uri": categoryUri, "wgt": weight, "required": required, "excluded": excluded })
243252

244253

245-
def addSource(self, sourceUri, weight):
254+
def addSource(self, sourceUri, weight, excluded = False):
246255
"""
247256
add a news source to the topic page
248257
@param sourceUri: uri of the news source to add to the topic page
249258
@param weight: importance of the news source (typically in range 1 - 50)
259+
@param excluded: if true, then the results from these sources will be ignored
250260
"""
251261
assert isinstance(weight, (float, int)), "weight value has to be a positive or negative integer"
252-
self.topicPage["sources"].append({"uri": sourceUri, "wgt": weight})
262+
self.topicPage["sources"].append({"uri": sourceUri, "wgt": weight, "excluded": excluded })
253263

254264

255-
def addSourceLocation(self, sourceLocationUri, weight):
265+
def addSourceLocation(self, sourceLocationUri, weight, excluded = False):
256266
"""
257267
add a list of relevant sources by identifying them by their geographic location
258268
@param sourceLocationUri: uri of the location where the sources should be geographically located
259269
@param weight: importance of the provided list of sources (typically in range 1 - 50)
270+
@param excluded: if true, then the results from the sources from this location will be ignored
260271
"""
261272
assert isinstance(weight, (float, int)), "weight value has to be a positive or negative integer"
262-
self.topicPage["sourceLocations"].append({"uri": sourceLocationUri, "wgt": weight})
273+
self.topicPage["sourceLocations"].append({"uri": sourceLocationUri, "wgt": weight, "excluded": excluded })
263274

264275

265-
def addSourceGroup(self, sourceGroupUri, weight):
276+
def addSourceGroup(self, sourceGroupUri, weight, excluded = False):
266277
"""
267278
add a list of relevant sources by specifying a whole source group to the topic page
268279
@param sourceGroupUri: uri of the source group to add
269280
@param weight: importance of the provided list of sources (typically in range 1 - 50)
281+
@param excluded: if true, then the results from sources from this group will be ignored
270282
"""
271283
assert isinstance(weight, (float, int)), "weight value has to be a positive or negative integer"
272-
self.topicPage["sourceGroups"].append({"uri": sourceGroupUri, "wgt": weight})
284+
self.topicPage["sourceGroups"].append({"uri": sourceGroupUri, "wgt": weight, "excluded": excluded })
273285

274286

275287
def addLocation(self, locationUri, weight):

eventregistry/examples/QueryArticlesExamples.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"""
44
from eventregistry import *
55

6-
er = EventRegistry(allowUseOfArchive=False)
7-
# er = EventRegistry()
6+
er = EventRegistry(apiKey = "6e8ab56e-b4d8-4032-94cc-db5138e0370c", allowUseOfArchive=False)
87

98

109
# search for the phrase "Barack Obama" - both words have to appear together
File renamed without changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ def testGetEventsForTopicPage(self):
3737
uriSet.add(event.get("uri"))
3838

3939

40+
def testCreateTopicPage(self):
41+
topic = TopicPage(self.er)
42+
appleUri = self.er.getConceptUri("apple")
43+
msoftUri = self.er.getConceptUri("microsoft")
44+
iphoneUri = self.er.getConceptUri("iphone")
45+
businessUri = self.er.getCategoryUri("business")
46+
topic.addConcept(appleUri, 50, required = False)
47+
topic.addConcept(msoftUri, 50, required = True)
48+
topic.addConcept(iphoneUri, 50, excluded = True)
49+
topic.addCategory(businessUri, 50, required=True)
50+
for page in range(1, 10):
51+
res = topic.getArticles(page = page, returnInfo = ReturnInfo(articleInfo=ArticleInfoFlags(concepts=True, categories=True, maxConceptsPerType=100)))
52+
for art in res.get("articles").get("results"):
53+
foundConcept = False
54+
foundCategory = False
55+
for conceptObj in art.get("concepts", []):
56+
assert iphoneUri != conceptObj["uri"], "Found iphone in the article"
57+
if msoftUri == conceptObj["uri"]:
58+
foundConcept = True
59+
for categoryObj in art.get("categories", []):
60+
if categoryObj["uri"].startswith(businessUri):
61+
foundCategory = True
62+
assert foundConcept, "Article did not have a required concept"
63+
assert foundCategory, "Article did not have a required category"
64+
4065

4166
if __name__ == "__main__":
4267
suite = unittest.TestLoader().loadTestsFromTestCase(TestTopicPage)

0 commit comments

Comments
 (0)