"""Hello Analytics Reporting API V4."""
#!/usr/bin/python3
import sys
import codecs
import traceback
import requests
import re
import pandas as pd
import random
import urllib
import dataset
import json
import gspread
import datetime
from gspread_pandas import Spread, Client
from oauth2client.service_account import ServiceAccountCredentials
import os
import threading
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import dataset

db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
#db.query('delete from hhh_weekly_affinity')

table=db['hhh_weekly_pagetitle']

SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = 'c:\\keys\\choozmo-ga-beee24b7a4c1.json'
VIEW_ID = '188916214'


def initialize_analyticsreporting():
  """Initializes an Analytics Reporting API V4 service object.

  Returns:
    An authorized Analytics Reporting API V4 service object.
  """
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)

  # Build the service object.
  analytics = build('analyticsreporting', 'v4', credentials=credentials)

  return analytics


def get_report(analytics,body):
  """Queries the Analytics Reporting API V4.

  Args:
    analytics: An authorized Analytics Reporting API V4 service object.
  Returns:
    The Analytics Reporting API V4 response.
  """
  return analytics.reports().batchGet(
      body={
        'reportRequests':body
        # [
        #{
#          'viewId': VIEW_ID,
#          'dateRanges': [{'startDate': '14daysAgo', 'endDate': 'today'}],
#          'dateRanges': [{'startDate': '2021-05-30', 'endDate': '2021-06-05'}],

#          'metrics': [{'expression': 'ga:users'}],
#          'dimensions': [{'name': 'ga:sourceMedium'}]
#          'dimensions': [{'name': 'ga:date'},{'name': 'ga:sourceMedium'}]

#        }]
      }
  ).execute()


def print_response(response):
  """Parses and prints the Analytics Reporting API V4 response.

  Args:
    response: An Analytics Reporting API V4 response.
  """
  result=[]
  for report in response.get('reports', []):
    columnHeader = report.get('columnHeader', {})
    dimensionHeaders = columnHeader.get('dimensions', [])
    metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])

    for row in report.get('data', {}).get('rows', []):
      dimensions = row.get('dimensions', [])
      dateRangeValues = row.get('metrics', [])
      ga_dict={}

      for header, dimension in zip(dimensionHeaders, dimensions):
        print(header + ': ', dimension)
        ga_dict[header]=dimension
      
      for i, values in enumerate(dateRangeValues):
        print('Date range:', str(i))
        for metricHeader, value in zip(metricHeaders, values.get('values')):
          ga_dict[metricHeader.get('name')]=value
#          print(metricHeader.get('name') + ':', value)
        result.append(ga_dict)
    return result
#        print(ga_dict)

def main():
  analytics = initialize_analyticsreporting()

  body=[{ 'viewId': VIEW_ID,
#  'dateRanges': [{'startDate': '2021-05-30', 'endDate': '2021-06-05'}],
  'dateRanges': [{'startDate': '2021-05-28', 'endDate': '2021-06-11'}],
  'metrics': [{'expression': 'ga:users'}],

#  'metrics': [{'expression': 'ga:users'},{'expression': 'ga:newusers'},{'expression': 'ga:sessions'},{'expression': 'ga:pageviews'},{'expression': 'ga:bounceRate'},{'expression': 'ga:pageviewsPerSession'}],
#  'metrics': [{'expression': 'ga:users'},{'expression': 'ga:newusers'},{'expression': 'ga:sessions'},{'expression': 'ga:pageviews'},{'expression': 'ga:bounceRate'},{'expression': 'ga:pageviewsPerSession'}],
#  'dimensions': [{'name': 'ga:date'},{'name': 'ga:sourceMedium'}]
#  'dimensions': [{'name': 'ga:date'}]
  'dimensions': [{'name': 'ga:pageTitle'},{'name': 'ga:isoWeek'},]

  }]

  response = get_report(analytics,body)
  ga_dict=print_response(response)
#  print(ga_dict)
#  print(ga_dict)
  for elmt in ga_dict:
#    elmt['ga:users']=int(elmt['ga:users'])
    if int(elmt['ga:users']) > 2:
#     print(elmt['ga:interestAffinityCategory'])
      table.insert(elmt)

#        print(elmt['ga:sourceMedium'])


if __name__ == '__main__':
  main()

