SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 81b2884c authored by Jean-Sébastien Caux's avatar Jean-Sébastien Caux
Browse files

Add nr_minutes argument to mailgun_get_events command

parent ddd0cbe3
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" ...@@ -2,6 +2,7 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)"
__license__ = "AGPL v3" __license__ = "AGPL v3"
import time
import requests import requests
from django.conf import settings from django.conf import settings
...@@ -11,7 +12,7 @@ from ...exceptions import APIMailError ...@@ -11,7 +12,7 @@ from ...exceptions import APIMailError
from ...models import Domain, Event from ...models import Domain, Event
def get_and_save_events(url=None, domain_name=None): def get_and_save_events(url=None, domain_name=None, nr_minutes=2):
""" """
For the given domain, get events from Mailgun Events API. For the given domain, get events from Mailgun Events API.
...@@ -19,14 +20,29 @@ def get_and_save_events(url=None, domain_name=None): ...@@ -19,14 +20,29 @@ def get_and_save_events(url=None, domain_name=None):
If no url is given, get the first page. If no url is given, get the first page.
Returns the paging JSON, if present, so traversing can be performed. Returns the paging JSON, if present, so traversing can be performed.
""" """
response = {}
if url is None and domain_name is None: if url is None and domain_name is None:
raise APIMailError('Please provide either a url or domain_name to get_and_save_events.') raise APIMailError('Please provide either a url or domain_name to get_and_save_events.')
response = requests.get( elif url:
url if url else "https://api.eu.mailgun.net/v3/%s/events" % domain_name, response = requests.get(
auth=("api", settings.MAILGUN_API_KEY) url,
).json() auth=("api", settings.MAILGUN_API_KEY)
).json()
else:
print("Fetching items for the last %d minutes" % nr_minutes)
begin_time = int(time.time()) - 60*nr_minutes
response = requests.get(
"https://api.eu.mailgun.net/v3/%s/events" % domain_name,
auth=("api", settings.MAILGUN_API_KEY),
params={
"begin": begin_time,
"ascending": "yes"
}
).json()
print(response)
try: try:
events = response['items'] events = response['items']
print("Retrieved %d events" % len(response['items']))
for item in events: for item in events:
if not Event.objects.filter(data__timestamp=item['timestamp'], if not Event.objects.filter(data__timestamp=item['timestamp'],
data__id=item['id']).exists(): data__id=item['id']).exists():
...@@ -44,12 +60,19 @@ class Command(BaseCommand): ...@@ -44,12 +60,19 @@ class Command(BaseCommand):
""" """
Perform a GET request to harvest Events from the Mailgun API, saving them to the DB. Perform a GET request to harvest Events from the Mailgun API, saving them to the DB.
""" """
help = 'Gets Events from the Mailgun Events API and saves them to the DB.' help = 'Gets Events from the Mailgun Events API and saves them to the DB.'
def add_arguments(self, parser):
parser.add_argument(
'--nr_minutes', action='store', dest='nr_minutes', type=int,
help='number of minutes in the past where events are to be retrieved'
)
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
nr_minutes = kwargs.get('nr_minutes', 2)
print("Getting events for the last %d minutes" % nr_minutes)
for domain in Domain.objects.active(): for domain in Domain.objects.active():
info = get_and_save_events(domain_name=domain.name) info = get_and_save_events(domain_name=domain.name, nr_minutes=nr_minutes)
ctr = 1 # Safety: ensure no runaway requests ctr = 1 # Safety: ensure no runaway requests
while ctr < 100 and info['nitems'] > 0: while ctr < 100 and info['nitems'] > 0:
info = get_and_save_events(url=info['paging']['next']) info = get_and_save_events(url=info['paging']['next'])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment