Skip to content

Commit

Permalink
Example of querying stats with the Adnuntius API
Browse files Browse the repository at this point in the history
  • Loading branch information
mileshampson committed Oct 2, 2024
1 parent a867013 commit bf4fa6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
11 changes: 10 additions & 1 deletion adnuntius/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from dateutil.tz import tzutc

dns_cache = {}

epoch = datetime.datetime.utcfromtimestamp(0)
half_hour_seconds = 60 * 30

def date_to_string(date):
"""
Expand Down Expand Up @@ -46,6 +47,14 @@ def generate_id():
return str(uuid.uuid4())


def half_hour(dt):
return datetime.datetime.utcfromtimestamp(int(((dt - epoch).total_seconds()) / half_hour_seconds) * half_hour_seconds)


def half_hour_round_up(dt):
return datetime.datetime.utcfromtimestamp(int(((dt - epoch).total_seconds() + half_hour_seconds - 1) / half_hour_seconds) * half_hour_seconds)


def id_reference(obj):
"""
Returns a dictionary containing an 'object reference' which is required by the API in some cases.
Expand Down
20 changes: 19 additions & 1 deletion test/example_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

import argparse
import getpass
from datetime import timedelta, datetime

from adnuntius.api import Api
from adnuntius.util import date_to_string, half_hour, half_hour_round_up


def query_order_example(api):
Expand All @@ -25,11 +27,27 @@ def query_order_example(api):
print(f"Total items to process: {total_count}")
for order in query_result['results']:
processed_count += 1
print(f"Processed {order['id']} - item {processed_count}/{total_count}")
# Query daily stats for the order for the last 7 days in NOK
stats = api.stats.query(args={'orderId': order['id'],
'startDate': date_to_string(half_hour(datetime.utcnow() - timedelta(days=7))),
'endDate': date_to_string(half_hour_round_up(datetime.utcnow())),
'currency': 'NOK',
'groupBy': 'daily'})
print(f"Item {processed_count}/{total_count} "
f"order {order['id']} had {stats['totals']['impressions']} impressions")
page += 1
print(f"Moving to page {page}")


def query_order_stats_example(api):
"""
Example of querying daily stats for an advertiser
"""
api.defaultArgs['context'] = args.network




if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Adnuntius API example which queries orders")
parser.add_argument('--api', dest='api_url', default='https://api.adnuntius.com/api')
Expand Down

0 comments on commit bf4fa6f

Please sign in to comment.