|
11 | 11 | import pandas as pd
|
12 | 12 | import googleapiclient
|
13 | 13 | import googleapiclient.discovery
|
| 14 | +from tenacity import retry, wait_fixed, stop_after_attempt |
14 | 15 |
|
15 | 16 | DISCOVERY_URL = "https://www.googleapis.com/discovery/v1/apis/trends/v1beta/rest"
|
16 | 17 | TERMS_IDS = "anosmia_ms"
|
@@ -80,7 +81,7 @@ def query(self, start_date: str, end_date: str, geo_id, dma=False) -> dict:
|
80 | 81 |
|
81 | 82 | return data
|
82 | 83 |
|
83 |
| - |
| 84 | +@retry(wait=wait_fixed(60), stop = stop_after_attempt(5)) |
84 | 85 | def get_counts_states(
|
85 | 86 | ght: GoogleHealthTrends,
|
86 | 87 | start_date: str,
|
@@ -127,7 +128,7 @@ def get_counts_states(
|
127 | 128 |
|
128 | 129 | return state_df
|
129 | 130 |
|
130 |
| - |
| 131 | +@retry(wait=wait_fixed(60), stop = stop_after_attempt(5)) |
131 | 132 | def get_counts_dma(
|
132 | 133 | ght: GoogleHealthTrends,
|
133 | 134 | start_date: str,
|
@@ -211,23 +212,22 @@ def _get_counts_geoid(
|
211 | 212 | output_dates = set(pd.date_range(start_date, end_date).to_native_types())
|
212 | 213 | cache_dates = set(dt["timestamp"].values)
|
213 | 214 | req_dates = list(output_dates - cache_dates)
|
214 |
| - |
215 | 215 | try:
|
216 | 216 | if req_dates:
|
217 | 217 | sdate = min(req_dates)
|
218 | 218 | edate = max(req_dates)
|
219 | 219 | new_data = _api_data_to_df(
|
220 |
| - ght.query(start_date=sdate, end_date=edate, geo_id=geo_id, dma=dma), |
221 |
| - geo_id=geo_id, |
222 |
| - ) |
| 220 | + ght.query(start_date=sdate, end_date=edate, geo_id=geo_id, dma=dma), |
| 221 | + geo_id=geo_id, |
| 222 | + ) |
223 | 223 | new_data = new_data[new_data["timestamp"].isin(req_dates)]
|
224 | 224 | dt = dt.append(new_data).sort_values("timestamp")
|
225 | 225 | dt = dt.drop_duplicates(subset="timestamp")
|
226 | 226 | _write_cached_file(dt, geo_id, data_dir)
|
227 | 227 | dt = _load_cached_file(geo_id, data_dir)
|
228 | 228 | except googleapiclient.errors.HttpError:
|
229 |
| - # This is thrown in there is no data yet for the given days. Need to |
230 |
| - # investigate this further. |
| 229 | + # This is thrown in there is no data yet for the given days. Need to |
| 230 | + # investigate this further. |
231 | 231 | pass
|
232 | 232 |
|
233 | 233 | dt = dt[dt["timestamp"].isin(output_dates)]
|
|
0 commit comments