-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeals_df_from_api.py
82 lines (67 loc) · 2.54 KB
/
meals_df_from_api.py
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
# Import necessary modules
import requests
import json
import datetime
INIT_MONTH = 12
END_MONTH = 12
YEAR = 2022
EDGE_ACCESS_COOKIE = '5c8d42f18916587290e5adb93ef1'
LOCAL = "Jaraguá do Sul"
all_meals = []
#function to return all ISO Timestamp from mondays from a year
def get_all_monday(year):
first_day = datetime.date(year, INIT_MONTH, 1)
last_day = datetime.date(year, END_MONTH, 31)
first_monday = first_day + datetime.timedelta(days=-first_day.weekday(), weeks=1)
last_monday = last_day + datetime.timedelta(days=-last_day.weekday(), weeks=1)
current_monday = first_monday
mondays = []
while current_monday <= last_monday:
mondays.append(current_monday.isoformat())
current_monday += datetime.timedelta(days=7)
return mondays
mondays = get_all_monday(YEAR)
# Set the API endpoint and headers
endpoint = "https://intranet.weg.net/br//_api/web/lists/getbytitle('Card%C3%A1pios')/items"
headers = {"accept": "application/json;odata=verbose"}
cookies = {"EdgeAccessCookie": EDGE_ACCESS_COOKIE}
# function to group the dataset by specific field
def group_by_field(dataset, field):
grouped_data = {}
for data in dataset:
if data.get(field) not in grouped_data:
grouped_data[data.get(field)] = []
grouped_data[data.get(field)].append(data)
del data[field]
return grouped_data
# Set the parameters for the API call
def get_meals(date):
params = {
"$select": "Kcal, Title, DiaSemana",
"$filter": "Semana ge datetime'{monday}T00:00:00Z' and Semana le datetime'{monday}T23:59:59Z' and substringof('{local}', Localidade/Title)".format(monday=date, local=LOCAL),
"$orderby": "TipoItem asc"
}
# Make the API call and store the response
response = requests.get(endpoint, headers=headers, cookies=cookies, params=params)
# Convert the response to a Python dictionary
data = json.loads(response.text)
# Extract the dataset from the response
dataset = data["d"]["results"]
meals = []
for meal in dataset:
meals.append({
"Title": meal.get("Title"),
"Kcal": meal.get("Kcal"),
"DiaSemana": meal.get("DiaSemana"),
})
all_meals.append({
"date": date,
"meals": group_by_field(meals, "DiaSemana")
})
#iterate over all mondays
for monday in mondays:
get_meals(monday)
print(all_meals)
# SAVE DATASET
with open('meals_dataset.json', 'w') as outfile:
json.dump(all_meals, outfile)