Skip to content

Commit 7d58f6d

Browse files
committed
Update example to add weekly and monthly downloads
1 parent bb8ff92 commit 7d58f6d

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

ravenpackapi/examples/daily_download.py

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# an example of how to ask for a daily dump
2+
import datetime
3+
4+
from ravenpackapi import Dataset, RPApi
5+
6+
api = RPApi(api_key="YOUR_API_KEY")
7+
dataset_id = "YOUR_DATASET_ID"
8+
9+
10+
def daily_download(dataset_id):
11+
end_date = datetime.datetime.utcnow().replace(
12+
hour=0, minute=0, second=0, microsecond=0
13+
)
14+
start_date = end_date - datetime.timedelta(days=1)
15+
print("Date range: %s-%s" % (start_date, end_date))
16+
dataset = Dataset(api=api, id=dataset_id)
17+
job = dataset.request_datafile(start_date, end_date)
18+
print("Waiting for the job to complete")
19+
job.wait_for_completion()
20+
output_filename = "dailydata-%s.csv" % start_date.strftime("%Y-%m-%d")
21+
job.save_to_file(output_filename)
22+
print("Daily download saved on:", output_filename)
23+
24+
25+
def weekly_download(dataset_id):
26+
today = datetime.datetime.utcnow().replace(
27+
hour=0, minute=0, second=0, microsecond=0
28+
)
29+
end_date = today - datetime.timedelta(days=today.weekday())
30+
start_date = end_date - datetime.timedelta(days=7)
31+
print("Date range: %s-%s" % (start_date, end_date))
32+
dataset = Dataset(api=api, id=dataset_id)
33+
job = dataset.request_datafile(start_date, end_date)
34+
print("Waiting for the job to complete")
35+
job.wait_for_completion()
36+
output_filename = "weeklydata-%s.csv" % start_date.strftime("%Y-%m-%d")
37+
job.save_to_file(output_filename)
38+
print("Weekly download saved on:", output_filename)
39+
40+
41+
def monthly_download(dataset_id):
42+
today = datetime.datetime.utcnow().replace(
43+
hour=0, minute=0, second=0, microsecond=0
44+
)
45+
end_date = datetime.datetime(today.year, today.month, 1)
46+
if today.month == 1:
47+
start_date = datetime.datetime(today.year - 1, 12, 1)
48+
else:
49+
start_date = datetime.datetime(today.year, today.month - 1, 1)
50+
print("Date range: %s-%s" % (start_date, end_date))
51+
dataset = Dataset(api=api, id=dataset_id)
52+
job = dataset.request_datafile(start_date, end_date)
53+
print("Waiting for the job to complete")
54+
job.wait_for_completion()
55+
output_filename = "monthlydata-%s.csv" % start_date.strftime("%Y-%m-%d")
56+
job.save_to_file(output_filename)
57+
print("Monthly download saved on:", output_filename)
58+
59+
60+
if __name__ == "__main__":
61+
daily_download(dataset_id)
62+
weekly_download(dataset_id)
63+
monthly_download(dataset_id)

0 commit comments

Comments
 (0)