Skip to content
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
50 changes: 34 additions & 16 deletions pysuez/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,41 @@ def _get_cookie(self):


def _fetch_data(self):
"""Fetch latest data from Suez."""
now = datetime.datetime.now()
today_year = now.strftime("%Y")
today_month = now.strftime("%m")
yesterday = datetime.datetime.now() - datetime.timedelta(1)
yesterday_year = yesterday.strftime('%Y')
yesterday_month = yesterday.strftime('%m')
yesterday_day = yesterday.strftime('%d')
url = BASE_URI+API_ENDPOINT_DATA
url += '{}/{}/{}'.format(
yesterday_year,
yesterday_month, self._counter_id
)

self._get_cookie()

data = requests.get(url, headers=self._headers)
data_not_fetched = True
iterations = 0
while data_not_fetched:
"""Fetch latest data from Suez."""
now = datetime.datetime.now() - datetime.timedelta(iterations)
today_year = now.strftime("%Y")
today_month = now.strftime("%m")
yesterday = datetime.datetime.now() - datetime.timedelta(iterations+1)
yesterday_year = yesterday.strftime('%Y')
yesterday_month = yesterday.strftime('%m')
yesterday_day = yesterday.strftime('%d')
url = BASE_URI+API_ENDPOINT_DATA
url += '{}/{}/{}'.format(
yesterday_year,
yesterday_month, self._counter_id
)

self._get_cookie()

data = requests.get(url, headers=self._headers)

# Check if data returned
data_json = data.json()
if data_json[0] == "ERR":
iterations = iterations + 1
continue
else:
# we have found some data, check if it overlaps the first day of the month and the last day of the previous month
if yesterday_month != today_month:
# we're still at a month boundary, loop again one more time
iterations = iterations + 1
continue
else:
data_not_fetched = False

try:
self.state = int(float(data.json()[int(
Expand Down