Skip to content

Commit

Permalink
Send aggregated Apple data for weekly/monthly trends
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed Jan 11, 2024
1 parent c41f2b1 commit fe9e228
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions 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 @@ -188,10 +195,19 @@ def get_request_lambda(f, *args, **kwargs):
# For weekly aggregated data:
FetchParams(
openpodcast_endpoint="episodes",
call=lambda: apple_connector.episodes(),
start_date=date_range.start,
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

0 comments on commit fe9e228

Please sign in to comment.