generated from worldbank/template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
100 additions
and
14 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import os | ||
import json | ||
import pandas as pd | ||
import requests | ||
# !pip install google-api-python-client | ||
from googleapiclient.discovery import build | ||
from googleapiclient.errors import HttpError | ||
# local import | ||
from scripts.python.config import GoogleAPIkey | ||
|
||
SERVICE_NAME = 'trends' | ||
SERVICE_VERSION = 'v1beta' | ||
_DISCOVERY_SERVICE_URL = 'https://www.googleapis.com/discovery/v1/apis/trends/v1beta/rest' | ||
|
||
|
||
class GT: | ||
def __init__(self, _GOOGLE_API_KEY = GoogleAPIkey): | ||
self.service = build( | ||
serviceName=SERVICE_NAME, | ||
version=SERVICE_VERSION, | ||
discoveryServiceUrl=_DISCOVERY_SERVICE_URL, | ||
developerKey=_GOOGLE_API_KEY, | ||
cache_discovery=False) | ||
self.block_until = None | ||
|
||
def get_health_trends(self, terms, timelineResolution="month"): | ||
graph = self.service.getTimelinesForHealth( | ||
terms=terms, | ||
timelineResolution=timelineResolution | ||
) | ||
|
||
try: | ||
response = graph.execute() | ||
return response | ||
|
||
except HttpError as http_error: | ||
data = json.loads(http_error.content.decode('utf-8')) | ||
code = data['error']['code'] | ||
reason = data['error']['errors'][0]['reason'] | ||
if code == 403 and reason == 'dailyLimitExceeded': | ||
self.block_until = datetime.combine( | ||
date.today() + timedelta(days=1), dtime.min) | ||
raise RuntimeError('%s: blocked until %s' % | ||
(reason, self.block_until)) | ||
import logging | ||
logging.warning(http_error) | ||
return [] | ||
|
||
|
||
def get_graph(self, terms, | ||
restrictions_geo, | ||
restrictions_startDate="2004-01"): | ||
graph = self.service.getGraph( | ||
terms=terms, | ||
restrictions_geo=restrictions_geo, | ||
restrictions_startDate=restrictions_startDate | ||
) | ||
|
||
try: | ||
response = graph.execute() | ||
return response | ||
|
||
except HttpError as http_error: | ||
import logging | ||
logging.warning(http_error) | ||
|
||
return [] | ||
|
||
|
||
@staticmethod | ||
def to_df(result:json) -> pd.DataFrame: | ||
df = pd.json_normalize(result["lines"], meta=["term"], record_path=["points"]) | ||
if "date" in df.columns: | ||
df["date"] = pd.to_datetime(df["date"]) | ||
|
||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters