-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreviousMonth.py
More file actions
92 lines (71 loc) · 2.62 KB
/
Copy pathPreviousMonth.py
File metadata and controls
92 lines (71 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import requests
import json
from dotenv import load_dotenv, dotenv_values
import os
import pandas as pd
import datetime
from time import sleep
load_dotenv()
import time
from pathlib import Path
import calendar
# Dictionary with locations
# locations = {
# "L1025768": "Franklin Delano Roosevelt (FDR) Park",
# "L1069194": "Philadelphia Naval Yard (restricted access)",
# "L1145863": "Wissahickon Valley Park--Houston Meadow",
# "L3041917": "Dixon Meadow Preserve",
# "L504403": "John Heinz NWR--impoundment (Philadelphia Co.)"
# }
locations = {
"L1025768": "FDR",
"L1069194": "PhiladelphiaNavalYard",
"L1145863": "WissahickonValley",
"L3041917": "DixonMeadowPreserve",
"L504403": "JohnHeinz"
}
def dataFetcher(last_month, previous_days, current_year, locationId, locationName):
string_month = str(last_month)
string_year = str(current_year)
csv_file_name = locationName + string_month + string_year
path = '/Users/raulbazan/Desktop/BirdData/HistoricalData/NewData' + '/' + csv_file_name + '.csv'
print(path)
filepath = Path(path)
df = pd.DataFrame()
client_api = os.getenv('API_KEY')
# Set the API key in the request headers
headers = {
'X-eBirdApiToken': client_api
}
# For loop to iterate through the days of the previous month and create a csv file with the historic data
for i in range(1, previous_days + 1):
#current_date = START_DATE + datetime.timedelta(days=i)
#print(current_date)
url = f"https://api.ebird.org/v2/data/obs/{locationId}/historic/{current_year}/{last_month}/{i}"
response = requests.get(url, headers=headers)
if response.status_code == 200:
# Get the raw response text
response_text = response.text
data = json.loads(response_text)
#print(data)
if data:
day_df = pd.DataFrame(data)
df = pd.concat([df, day_df], ignore_index=True)
else:
print(" no data for this date" , " ", last_month , " " , i)
else:
print("better luck next time")
time.sleep(1)
if not df.empty and 'obsDt' in df.columns:
df['obsDt'] = pd.to_datetime(df['obsDt'], errors='coerce')
df.to_csv(filepath, index=False)
#Get previous month and days to get data from previous month.
for locationId,locationName in locations.items():
today = datetime.date.today()
first = today.replace(day=1)
last_month = first - datetime.timedelta(days=1)
previous_month = last_month.month
previous_days = last_month.day
current_year = last_month.year
locationName = ''.join(e for e in locationName if e.isalnum())
dataFetcher(previous_month, previous_days, current_year, locationId, locationName)