#!/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

def save_sheet(df,filename,tabname,startpos='A1'):

    scope = ['https://spreadsheets.google.com/feeds',
            'https://www.googleapis.com/auth/drive']

    credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\keys\\spread2.json', scope)
#    credentials = ServiceAccountCredentials.from_json_keyfile_name('/var/keys/spread2.json', scope)

    gc = gspread.authorize(credentials)
    spread = Spread(filename,creds=credentials)

    spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)


def do_jobs():
    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
    cursor=db.query("SELECT gsc.query,ga.`ga:users` as users,ga.`ga:newusers` as newusers,ga.`ga:pageviews` as pageviews,ga.`ga:bounceRate` as bouncerate,ga.`ga:pageviewsPerSession` as pvpersession,gsc.`page` as url FROM hhh.ga_pagepath_yearly ga, hhh.gsc_page_query_yearly gsc where ga.`ga:pagePath`=gsc.`page` order by `ga:users` desc")
    df = pd.DataFrame(columns=('query','users','newusers','pageviews','bouncerate','pvpersession','url'))

    idx=0

    for c in cursor:
        df.loc[idx]=[c['query'],c['users'],c['newusers'],c['pageviews'],c['bouncerate'],c['pvpersession'],c['url']]
    #    df.loc[idx]=['okok',333]
        idx+=1
#    save_sheet(df,'ga_x_gsc','results')
    save_sheet(df,'ga_x_gsc_yearly','with_url')

t = threading.Thread(target = do_jobs)
t.start()
t.join()

