Skip to content

Commit 8949341

Browse files
committed
## [v8.12]() (2022-03-11)
**Updated** - changed the logger. No longer printing out to console information about the host, etc - removed two parameters from `EventRegistry` constructor: - `logging` - if you wish to log requests to file, please call now `EventRegistry.setLogging(True)` - `printHostInfo` - if you wish to see the host info, call `logging.getLogger("eventregistry").setLevel(logging.DEBUG)` - `checkVersion()` is no longer automatically called in `EventRegistry` constructor. If you wish to see if there is an updated version of the module, please call the method manually.
1 parent 920b6c5 commit 8949341

File tree

10 files changed

+61
-56
lines changed

10 files changed

+61
-56
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Change Log
22

3+
## [v8.12]() (2022-03-11)
4+
5+
**Updated**
6+
- changed the logger. No longer printing out to console information about the host, etc
7+
- removed two parameters from `EventRegistry` constructor:
8+
- `logging` - if you wish to log requests to file, please call now `EventRegistry.setLogging(True)`
9+
- `printHostInfo` - if you wish to see the host info, call `logging.getLogger("eventregistry").setLevel(logging.DEBUG)`
10+
- `checkVersion()` is no longer automatically called in `EventRegistry` constructor. If you wish to see if there is an updated version of the module, please call the method manually.
11+
12+
313
## [v8.11]() (2022-01-05)
414

515
**Updated**
@@ -271,7 +281,7 @@
271281
- Changed constructor parameters in `GetRecentEvents` and `GetRecentArticles`.
272282

273283
**Removed**
274-
- removed `EventRegistry.login()` method to login the user. The user should authenticate using the API key that he can obtain on [his settings page](http://eventregistry.org/me?tab=settings).
284+
- removed `EventRegistry.login()` method to login the user. The user should authenticate using the API key that he can obtain on [his settings page](https://newsapi.ai/dashboard).
275285

276286

277287

eventregistry/Base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import six, warnings, os, sys, re, datetime, time
6-
6+
from eventregistry.Logger import logger
77

88
mainLangs = ["eng", "deu", "zho", "slv", "spa"]
99
allLangs = [ "eng", "deu", "spa", "cat", "por", "ita", "fra", "rus", "ara", "tur", "zho", "slv", "hrv", "srp" ]
@@ -226,7 +226,7 @@ def _setQueryArrVal(self, value, propName, propOperName, defaultOperName):
226226
if propOperName != None:
227227
self.queryParams[propOperName] = defaultOperName
228228
if len(value) > 1:
229-
print("Warning: The value of parameter '%s' was provided as a list and '%s' operator was used implicitly between the items. We suggest specifying the list using the QueryItems.AND() or QueryItems.OR() to ensure the appropriate operator is used." % (propName, defaultOperName))
229+
logger.warning("Warning: The value of parameter '%s' was provided as a list and '%s' operator was used implicitly between the items. We suggest specifying the list using the QueryItems.AND() or QueryItems.OR() to ensure the appropriate operator is used." % (propName, defaultOperName))
230230

231231
# there should be no other valid types
232232
else:

eventregistry/EventRegistry.py

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
22
main class responsible for obtaining results from the Event Registry
33
"""
4-
import six, os, sys, traceback, json, re, requests, time
5-
import threading
4+
import six, os, sys, traceback, json, re, requests, time, logging, threading
65

76
from eventregistry.Base import *
87
from eventregistry.ReturnInfo import *
8+
from eventregistry.Logger import logger
99

1010

1111
class EventRegistry(object):
@@ -17,34 +17,30 @@ def __init__(self,
1717
apiKey = None,
1818
host = None,
1919
hostAnalytics = None,
20-
logging = False,
2120
minDelayBetweenRequests = 0.5,
2221
repeatFailedRequestCount = -1,
2322
allowUseOfArchive = True,
2423
verboseOutput = False,
25-
printHostInfo = True,
2624
settingsFName = None):
2725
"""
2826
@param apiKey: API key that should be used to make the requests to the Event Registry. API key is assigned to each user account and can be obtained on
29-
this page: http://eventregistry.org/me?tab=settings
27+
this page: https://newsapi.ai/dashboard
3028
@param host: host to use to access the Event Registry backend. Use None to use the default host.
3129
@param hostAnalytics: the host address to use to perform the analytics api calls
32-
@param logging: log all requests made to a 'requests_log.txt' file
3330
@param minDelayBetweenRequests: the minimum number of seconds between individual api calls
3431
@param repeatFailedRequestCount: if a request fails (for example, because ER is down), what is the max number of times the request
3532
should be repeated (-1 for indefinitely)
3633
@param allowUseOfArchive: default is True. Determines if the queries made should potentially be executed on the archive data.
3734
If False, all queries (regardless how the date conditions are set) will be executed on data from the last 31 days.
3835
Queries executed on the archive are more expensive so set it to False if you are just interested in recent data
3936
@param verboseOutput: if True, additional info about errors etc will be printed to console
40-
@param printHostInfo: print which urls are used as the hosts
4137
@param settingsFName: If provided it should be a full path to 'settings.json' file where apiKey an/or host can be loaded from.
4238
If None, we will look for the settings file in the eventregistry module folder
4339
"""
4440
self._host = host
4541
self._hostAnalytics = hostAnalytics
4642
self._lastException = None
47-
self._logRequests = logging
43+
self._logRequests = False
4844
self._minDelayBetweenRequests = minDelayBetweenRequests
4945
self._repeatFailedRequestCount = repeatFailedRequestCount
5046
self._allowUseOfArchive = allowUseOfArchive
@@ -64,17 +60,16 @@ def __init__(self,
6460
# and to read the host name from it (if custom host is not specified)
6561
currPath = os.path.split(os.path.realpath(__file__))[0]
6662
settFName = settingsFName or os.path.join(currPath, "settings.json")
67-
if apiKey and printHostInfo:
68-
print("using user provided API key for making requests")
63+
if apiKey:
64+
logger.debug("using user provided API key for making requests")
6965

7066
if os.path.exists(settFName):
7167
settings = json.load(open(settFName))
7268
self._host = host or settings.get("host", "http://eventregistry.org")
7369
self._hostAnalytics = hostAnalytics or settings.get("hostAnalytics", "http://analytics.eventregistry.org")
7470
# if api key is set, then use it when making the requests
7571
if "apiKey" in settings and not apiKey:
76-
if printHostInfo:
77-
print("found apiKey in settings file which will be used for making requests")
72+
logger.debug("found apiKey in settings file which will be used for making requests")
7873
self._apiKey = settings["apiKey"]
7974
else:
8075
self._host = host or "http://eventregistry.org"
@@ -84,9 +79,8 @@ def __init__(self,
8479
print("No API key was provided. You will be allowed to perform only a very limited number of requests per day.")
8580
self._requestLogFName = os.path.join(currPath, "requests_log.txt")
8681

87-
if printHostInfo:
88-
print("Event Registry host: %s" % (self._host))
89-
print("Text analytics host: %s" % (self._hostAnalytics))
82+
logger.debug("Event Registry host: %s" % (self._host))
83+
logger.debug("Text analytics host: %s" % (self._hostAnalytics))
9084

9185
# 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
9286
self._stopStatusCodes = set([
@@ -96,9 +90,6 @@ def __init__(self,
9690
403, # Invalid account. The user's IP or account is disabled, potentially due to misuse.
9791
])
9892

99-
# check what is the version of your module compared to the latest one
100-
self.checkVersion()
101-
10293

10394
def checkVersion(self):
10495
"""
@@ -113,9 +104,9 @@ def checkVersion(self):
113104
currentVersion = _version.__version__
114105
for (latest, current) in zip(latestVersion.split("."), currentVersion.split(".")):
115106
if int(latest) > int(current):
116-
print("==============\nYour version of the module is outdated, please update to the latest version")
117-
print("Your version is %s while the latest is %s" % (currentVersion, latestVersion))
118-
print("Update by calling: pip install --upgrade eventregistry\n==============")
107+
logger.info("==============\nYour version of the module is outdated, please update to the latest version")
108+
logger.info("Your version is %s while the latest is %s" % (currentVersion, latestVersion))
109+
logger.info("Update by calling: pip install --upgrade eventregistry\n==============")
119110
return
120111
# in case the server mistakenly has a lower version that the user has, don't report an error
121112
elif int(latest) < int(current):
@@ -145,19 +136,14 @@ def getLastException(self):
145136

146137

147138
def printLastException(self):
148-
print(str(self._lastException))
139+
logger.error(str(self._lastException))
149140

150141

151142
def format(self, obj):
152143
"""return a string containing the object in a pretty formated version"""
153144
return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '))
154145

155146

156-
def printConsole(self, text):
157-
"""print time prefix + text to console"""
158-
print(time.strftime("%H:%M:%S") + " " + str(text))
159-
160-
161147
def getRemainingAvailableRequests(self):
162148
"""get the number of requests that are still available for the user today. Information is only accessible after you make some query."""
163149
return self._remainingAvailableRequests
@@ -297,7 +283,7 @@ def jsonRequest(self, methodUrl, paramDict, customLogFName = None, allowUseOfArc
297283
raise Exception(respInfo.text)
298284
# did we get a warning. if yes, print it
299285
if self.getLastHeader("warning"):
300-
print("=========== WARNING ===========\n%s\n===============================" % (self.getLastHeader("warning")))
286+
logger.warning("=========== WARNING ===========\n%s\n===============================" % (self.getLastHeader("warning")))
301287
# remember the available requests
302288
self._dailyAvailableRequests = tryParseInt(self.getLastHeader("x-ratelimit-limit", ""), val = -1)
303289
self._remainingAvailableRequests = tryParseInt(self.getLastHeader("x-ratelimit-remaining", ""), val = -1)
@@ -307,15 +293,15 @@ def jsonRequest(self, methodUrl, paramDict, customLogFName = None, allowUseOfArc
307293
except Exception as ex:
308294
self._lastException = ex
309295
if self._verboseOutput:
310-
print("Event Registry exception while executing the request:")
311-
print("endpoint: %s\nParams: %s" % (url, json.dumps(paramDict, indent=4)))
296+
logger.error("Event Registry exception while executing the request:")
297+
logger.error("endpoint: %s\nParams: %s" % (url, json.dumps(paramDict, indent=4)))
312298
self.printLastException()
313299
# in case of invalid input parameters, don't try to repeat the search but we simply raise the same exception again
314300
if respInfo != None and respInfo.status_code in self._stopStatusCodes:
315301
break
316302
# in case of the other exceptions (maybe the service is temporarily unavailable) we try to repeat the query
317-
print("The request will be automatically repeated in 3 seconds...")
318-
time.sleep(3) # sleep for X seconds on error
303+
logger.info("The request will be automatically repeated in 3 seconds...")
304+
time.sleep(5) # sleep for X seconds on error
319305
self._lock.release()
320306
if returnData == None:
321307
raise self._lastException or Exception("No valid return data provided")
@@ -353,14 +339,14 @@ def jsonRequestAnalytics(self, methodUrl, paramDict):
353339
except Exception as ex:
354340
self._lastException = ex
355341
if self._verboseOutput:
356-
print("Event Registry Analytics exception while executing the request:")
357-
print("endpoint: %s\nParams: %s" % (url, json.dumps(paramDict, indent=4)))
342+
logger.error("Event Registry Analytics exception while executing the request:")
343+
logger.error("endpoint: %s\nParams: %s" % (url, json.dumps(paramDict, indent=4)))
358344
self.printLastException()
359345
# in case of invalid input parameters, don't try to repeat the search but we simply raise the same exception again
360346
if respInfo != None and respInfo.status_code in self._stopStatusCodes:
361347
break
362-
print("The request will be automatically repeated in 3 seconds...")
363-
time.sleep(3) # sleep for X seconds on error
348+
logger.info("The request will be automatically repeated in 3 seconds...")
349+
time.sleep(5) # sleep for X seconds on error
364350
self._lock.release()
365351
if returnData == None:
366352
raise self._lastException or Exception("No valid return data provided")

eventregistry/Logger.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import logging, os
2+
3+
logger = logging.getLogger("eventregistry")
4+
if len(logger.handlers) == 0:
5+
logger.addHandler(logging.StreamHandler())
6+
7+
# by default just log from Warning level up
8+
logger.setLevel(level = os.environ.get("LOGLEVEL", "WARNING"))

eventregistry/QueryArticles.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from eventregistry.Base import *
33
from eventregistry.ReturnInfo import *
44
from eventregistry.Query import *
5+
from eventregistry.Logger import logger
56

67

78
class QueryArticles(Query):
@@ -251,7 +252,7 @@ def count(self, eventRegistry):
251252
self.setRequestedResult(RequestArticlesInfo())
252253
res = eventRegistry.execQuery(self)
253254
if "error" in res:
254-
print(res["error"])
255+
logger.error(res["error"])
255256
count = res.get("articles", {}).get("totalResults", 0)
256257
return count
257258

@@ -328,10 +329,10 @@ def _getNextArticleBatch(self):
328329
sortBy=self._sortBy, sortByAsc=self._sortByAsc,
329330
returnInfo = self._returnInfo))
330331
if self._er._verboseOutput:
331-
print("Downloading article page %d..." % (self._articlePage))
332+
logger.debug("Downloading article page %d..." % (self._articlePage))
332333
res = self._er.execQuery(self)
333334
if "error" in res:
334-
print("Error while obtaining a list of articles: " + res["error"])
335+
logger.error("Error while obtaining a list of articles: " + res["error"])
335336
else:
336337
self._totalPages = res.get("articles", {}).get("pages", 0)
337338
results = res.get("articles", {}).get("results", [])

eventregistry/QueryEvent.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from eventregistry.ReturnInfo import *
44
from eventregistry.QueryArticles import QueryArticles, RequestArticlesInfo
55
from eventregistry.Query import *
6-
6+
from eventregistry.Logger import logger
77

88
class QueryEvent(Query):
99
"""
@@ -155,7 +155,7 @@ def count(self, eventRegistry):
155155
self.setRequestedResult(RequestEventArticles(**self.queryParams))
156156
res = eventRegistry.execQuery(self)
157157
if "error" in res:
158-
print(res["error"])
158+
logger.error(res["error"])
159159
count = res.get(self.queryParams["eventUri"], {}).get("articles", {}).get("totalResults", 0)
160160
return count
161161

@@ -197,7 +197,7 @@ def _getNextArticleBatch(self):
197197
if self._totalPages != None and self._articlePage > self._totalPages:
198198
return
199199
if self._er._verboseOutput:
200-
print("Downloading article page %d from event %s" % (self._articlePage, eventUri))
200+
logger.debug("Downloading article page %d from event %s" % (self._articlePage, eventUri))
201201

202202
self.setRequestedResult(RequestEventArticles(
203203
page = self._articlePage,
@@ -206,7 +206,7 @@ def _getNextArticleBatch(self):
206206
**self.queryParams))
207207
res = self._er.execQuery(self)
208208
if "error" in res:
209-
print(res["error"])
209+
logger.error(res["error"])
210210
else:
211211
self._totalPages = res.get(eventUri, {}).get("articles", {}).get("pages", 0)
212212
arts = res.get(eventUri, {}).get("articles", {}).get("results", [])

eventregistry/QueryEvents.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from eventregistry.Base import *
33
from eventregistry.ReturnInfo import *
44
from eventregistry.Query import *
5-
5+
from eventregistry.Logger import logger
66

77
class QueryEvents(Query):
88
def __init__(self,
@@ -226,7 +226,7 @@ def count(self, eventRegistry):
226226
self.setRequestedResult(RequestEventsInfo())
227227
res = eventRegistry.execQuery(self)
228228
if "error" in res:
229-
print(res["error"])
229+
logger.error(res["error"])
230230
count = res.get("events", {}).get("totalResults", 0)
231231
return count
232232

@@ -289,10 +289,10 @@ def _getNextEventBatch(self):
289289
returnInfo = self._returnInfo))
290290
# download articles and make sure that we set the same archive flag as it was returned when we were processing the uriList request
291291
if self._er._verboseOutput:
292-
print("Downloading event page %d..." % (self._eventPage))
292+
logger.debug("Downloading event page %d..." % (self._eventPage))
293293
res = self._er.execQuery(self)
294294
if "error" in res:
295-
print("Error while obtaining a list of events: " + res["error"])
295+
logger.error("Error while obtaining a list of events: " + res["error"])
296296
else:
297297
self._totalPages = res.get("events", {}).get("pages", 0)
298298
results = res.get("events", {}).get("results", [])

eventregistry/QueryMentions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from eventregistry.Base import *
33
from eventregistry.ReturnInfo import *
44
from eventregistry.Query import *
5-
5+
from eventregistry.Logger import logger
66

77
class QueryMentions(Query):
88
def __init__(self,
@@ -216,7 +216,7 @@ def count(self, eventRegistry):
216216
self.setRequestedResult(RequestMentionsInfo())
217217
res = eventRegistry.execQuery(self)
218218
if "error" in res:
219-
print(res["error"])
219+
logger.error(res["error"])
220220
count = res.get("mentions", {}).get("totalResults", 0)
221221
return count
222222

@@ -290,10 +290,10 @@ def _getNextMentionBatch(self):
290290
sortBy=self._sortBy, sortByAsc=self._sortByAsc,
291291
returnInfo = self._returnInfo))
292292
if self._er._verboseOutput:
293-
print("Downloading mention page %d..." % (self._mentionPage))
293+
logger.debug("Downloading mention page %d..." % (self._mentionPage))
294294
res = self._er.execQuery(self)
295295
if "error" in res:
296-
print("Error while obtaining a list of mentions: " + res["error"])
296+
logger.error("Error while obtaining a list of mentions: " + res["error"])
297297
else:
298298
self._totalPages = res.get("mentions", {}).get("pages", 0)
299299
results = res.get("mentions", {}).get("results", [])

eventregistry/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "8.11"
1+
__version__ = "8.12"

eventregistry/tests/TestERAutoSuggestions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def testSource(self):
2626
# self.assertTrue(self.er.getNewsSourceUri("topix.com") == "topix.com")
2727

2828
self.assertTrue(self.er.suggestLocations("Washington")[0].get("wikiUri") == "http://en.wikipedia.org/wiki/Washington_(state)")
29-
self.assertTrue(self.er.suggestLocations("London")[0].get("wikiUri") == "http://en.wikipedia.org/wiki/City_of_London")
29+
self.assertTrue(self.er.suggestLocations("London")[0].get("wikiUri") == "http://en.wikipedia.org/wiki/London")
3030

3131
srcList = self.er.suggestSourcesAtPlace(self.er.getConceptUri("New York City"))
3232
self.assertTrue(len(srcList) > 0)

0 commit comments

Comments
 (0)