1
1
import glob
2
- import http .cookiejar
3
2
import json
4
3
import logging
5
4
import os
6
- import time
7
5
from abc import ABC , abstractmethod
8
6
from datetime import date , timedelta
9
7
from typing import Any , Dict , List , Optional , cast
10
8
11
9
import pandas as pd
12
- from requests import Session
13
10
11
+ from pygazpar .api_client import APIClient , ConsumptionType
12
+ from pygazpar .api_client import Frequency as APIClientFrequency
14
13
from pygazpar .enum import Frequency , PropertyName
15
14
from pygazpar .excelparser import ExcelParser
16
15
from pygazpar .jsonparser import JsonParser
17
- from pygazpar .api_client import APIClient , ConsumptionType , Frequency as APIClientFrequency
18
-
19
- SESSION_TOKEN_URL = "https://connexion.grdf.fr/api/v1/authn"
20
- SESSION_TOKEN_PAYLOAD = """{{
21
- "username": "{0}",
22
- "password": "{1}",
23
- "options": {{
24
- "multiOptionalFactorEnroll": "false",
25
- "warnBeforePasswordExpired": "false"
26
- }}
27
- }}"""
28
-
29
- AUTH_TOKEN_URL = "https://connexion.grdf.fr/login/sessionCookieRedirect"
30
- AUTH_TOKEN_PARAMS = """{{
31
- "checkAccountSetupComplete": "true",
32
- "token": "{0}",
33
- "redirectUrl": "https://monespace.grdf.fr"
34
- }}"""
35
16
36
17
Logger = logging .getLogger (__name__ )
37
18
@@ -75,46 +56,6 @@ def load(
75
56
76
57
return res
77
58
78
- # ------------------------------------------------------
79
- def _login (self , username : str , password : str ) -> str :
80
-
81
- session = Session ()
82
- session .headers .update ({"domain" : "grdf.fr" })
83
- session .headers .update ({"Content-Type" : "application/json" })
84
- session .headers .update ({"X-Requested-With" : "XMLHttpRequest" })
85
-
86
- payload = SESSION_TOKEN_PAYLOAD .format (username , password )
87
-
88
- response = session .post (SESSION_TOKEN_URL , data = payload )
89
-
90
- if response .status_code != 200 :
91
- raise ValueError (
92
- f"An error occurred while logging in. Status code: { response .status_code } - { response .text } "
93
- )
94
-
95
- session_token = response .json ().get ("sessionToken" )
96
-
97
- Logger .debug ("Session token: %s" , session_token )
98
-
99
- jar = http .cookiejar .CookieJar ()
100
-
101
- self ._session = Session () # pylint: disable=attribute-defined-outside-init
102
- self ._session .headers .update ({"Content-Type" : "application/json" })
103
- self ._session .headers .update ({"X-Requested-With" : "XMLHttpRequest" })
104
-
105
- params = json .loads (AUTH_TOKEN_PARAMS .format (session_token ))
106
-
107
- response = self ._session .get (AUTH_TOKEN_URL , params = params , allow_redirects = True , cookies = jar ) # type: ignore
108
-
109
- if response .status_code != 200 :
110
- raise ValueError (
111
- f"An error occurred while getting the auth token. Status code: { response .status_code } - { response .text } "
112
- )
113
-
114
- auth_token = self ._session .cookies .get ("auth_token" , domain = "monespace.grdf.fr" )
115
-
116
- return auth_token # type: ignore
117
-
118
59
@abstractmethod
119
60
def _loadFromSession (
120
61
self , pceIdentifier : str , startDate : date , endDate : date , frequencies : Optional [List [Frequency ]] = None
@@ -125,8 +66,6 @@ def _loadFromSession(
125
66
# ------------------------------------------------------------------------------------------------------------
126
67
class ExcelWebDataSource (WebDataSource ): # pylint: disable=too-few-public-methods
127
68
128
- DATA_URL = "https://monespace.grdf.fr/api/e-conso/pce/consommation/informatives/telecharger?dateDebut={0}&dateFin={1}&frequence={3}&pceList[]={2}"
129
-
130
69
DATE_FORMAT = "%Y-%m-%d"
131
70
132
71
FREQUENCY_VALUES = {
@@ -173,19 +112,18 @@ def _loadFromSession( # pylint: disable=too-many-branches
173
112
frequencyList = list (set (frequencies ))
174
113
175
114
for frequency in frequencyList :
176
- # Inject parameters.
177
- downloadUrl = ExcelWebDataSource .DATA_URL .format (
178
- startDate .strftime (ExcelWebDataSource .DATE_FORMAT ),
179
- endDate .strftime (ExcelWebDataSource .DATE_FORMAT ),
180
- pceIdentifier ,
181
- ExcelWebDataSource .FREQUENCY_VALUES [frequency ],
182
- )
183
115
184
116
Logger .debug (
185
117
f"Loading data of frequency { ExcelWebDataSource .FREQUENCY_VALUES [frequency ]} from { startDate .strftime (ExcelWebDataSource .DATE_FORMAT )} to { endDate .strftime (ExcelWebDataSource .DATE_FORMAT )} "
186
118
)
187
119
188
- response = self ._api_client .get_pce_consumption_excelsheet (ConsumptionType .INFORMATIVE , startDate , endDate , APIClientFrequency (ExcelWebDataSource .FREQUENCY_VALUES [frequency ]), [pceIdentifier ])
120
+ response = self ._api_client .get_pce_consumption_excelsheet (
121
+ ConsumptionType .INFORMATIVE ,
122
+ startDate ,
123
+ endDate ,
124
+ APIClientFrequency (ExcelWebDataSource .FREQUENCY_VALUES [frequency ]),
125
+ [pceIdentifier ],
126
+ )
189
127
190
128
filename = response ["filename" ]
191
129
content = response ["content" ]
@@ -249,12 +187,6 @@ def load(
249
187
# ------------------------------------------------------------------------------------------------------------
250
188
class JsonWebDataSource (WebDataSource ): # pylint: disable=too-few-public-methods
251
189
252
- DATA_URL = (
253
- "https://monespace.grdf.fr/api/e-conso/pce/consommation/informatives?dateDebut={0}&dateFin={1}&pceList[]={2}"
254
- )
255
-
256
- TEMPERATURES_URL = "https://monespace.grdf.fr/api/e-conso/pce/{0}/meteo?dateFinPeriode={1}&nbJours={2}"
257
-
258
190
INPUT_DATE_FORMAT = "%Y-%m-%d"
259
191
260
192
OUTPUT_DATE_FORMAT = "%d/%m/%Y"
0 commit comments