Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send aggregated Apple data for weekly/monthly trends #80

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion apple/job/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import job.apple as apple

from loguru import logger
from appleconnector import AppleConnector, Metric, Dimension
from appleconnector import AppleConnector, Metric, Dimension, Mode

print("Initializing environment")

Expand Down Expand Up @@ -127,6 +127,13 @@ def get_request_lambda(f, *args, **kwargs):
# Define a list of FetchParams objects with the parameters for each API call
endpoints = []

def last_week_monday():
"""
Get the date of Monday of the last week
"""
today = dt.date.today()
return today - dt.timedelta(days=today.weekday(), weeks=1)

for chunk_id, (start_date, end_date) in enumerate(date_range.chunks(DAYS_PER_CHUNK)):
print(f"Chunk {chunk_id} from {start_date} to {end_date}...")
endpoints += [
Expand Down Expand Up @@ -184,6 +191,23 @@ def get_request_lambda(f, *args, **kwargs):
start_date=date_range.start,
end_date=date_range.end,
),

# For weekly aggregated data:
FetchParams(
openpodcast_endpoint="episodes",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: should this endpoint be changed? E.g. episodes_aggregated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might work like this because the grouping is part of the payload and can be differentiated on the API side.

call=get_request_lambda(
apple_connector.episodes,
# Weekly aggregated data returns the 7 days
# starting from start date
last_week_monday(),
mode=Mode.WEEKLY,
),
# We discern the weekly aggregated data from the daily
# aggregated data by setting the dates from Monday to Sunday
# of the last week
start_date=last_week_monday(),
end_date=last_week_monday() + dt.timedelta(days=6),
),
]

# Fetch all episodes to get the episode IDs
Expand Down
Loading