def collect_google_scores(terms, start, end): logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR) logger.info('Querying %d terms between %s and %s' % (len(terms), start, end)) logger.debug(', '.join(t.term for t in terms)) service = build( 'trends', 'v1beta', developerKey=os.environ["GOOGLE_API_KEY"], discoveryServiceUrl='https://www.googleapis.com/discovery/v1/apis/trends/v1beta/rest', cache_discovery=False ) graph = service.getTimelinesForHealth( terms=[t.term for t in terms], geoRestriction_region='GB-ENG', time_startDate=start.strftime('%Y-%m-%d'), time_endDate=end.strftime("%Y-%m-%d"), timelineResolution='day') try: response = graph.execute() except HttpError as e: logger.exception(e) raise e for line in response['lines']: term = next(t for t in terms if t.term == line['term']) for point in line['points']: day = datetime.strptime(point['date'], "%b %d %Y").date() gs = get_google_score(term, day) gs.value = float(point['value']) yield gs
def find_matching_index(headers, possible, required=False): for i, h in enumerate(headers): if h in possible: return i if required: e = FluDetectorError('No %s header in CSV file' % ','.join(possible)) logger.exception(e) raise e
def find_region_index(headers): indexes = {} for code, name in REGIONS.iteritems(): index = find_matching_index(headers, [code, name]) if index: indexes[code] = index if not indexes: e = FluDetectorError('No region headers found') logger.exception(e) raise e return indexes
def calculate_score(model, day, engine_runner): averages = list(calculate_moving_averages(model, day)) if not averages: return ms = get_model_score(model, day) try: if engine_runner.conf is CalculatorType.LEGACY: ms.value = send_to_matlab(model, averages) else: ms.value = engine_runner.calculateModelScore(model, averages) except ErrorReturnCode as e: logger.exception(e) raise e return ms