GA_GSC_Sheet_yearly.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/python3
  2. import sys
  3. import codecs
  4. import traceback
  5. import requests
  6. import re
  7. import pandas as pd
  8. import random
  9. import urllib
  10. import dataset
  11. import json
  12. import gspread
  13. import datetime
  14. from gspread_pandas import Spread, Client
  15. from oauth2client.service_account import ServiceAccountCredentials
  16. import os
  17. import threading
  18. def save_sheet(df,filename,tabname,startpos='A1'):
  19. scope = ['https://spreadsheets.google.com/feeds',
  20. 'https://www.googleapis.com/auth/drive']
  21. credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\keys\\spread2.json', scope)
  22. # credentials = ServiceAccountCredentials.from_json_keyfile_name('/var/keys/spread2.json', scope)
  23. gc = gspread.authorize(credentials)
  24. spread = Spread(filename,creds=credentials)
  25. spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)
  26. def do_jobs():
  27. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  28. 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")
  29. df = pd.DataFrame(columns=('query','users','newusers','pageviews','bouncerate','pvpersession','url'))
  30. idx=0
  31. for c in cursor:
  32. df.loc[idx]=[c['query'],c['users'],c['newusers'],c['pageviews'],c['bouncerate'],c['pvpersession'],c['url']]
  33. # df.loc[idx]=['okok',333]
  34. idx+=1
  35. # save_sheet(df,'ga_x_gsc','results')
  36. save_sheet(df,'ga_x_gsc_yearly','with_url')
  37. t = threading.Thread(target = do_jobs)
  38. t.start()
  39. t.join()