12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #!/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 p1.`ga:pageTitle` as title ,p1.`ga:users` as prev_week ,p2.`ga:users` as cur_week, (p2.`ga:users`-p1.`ga:users`)/p1.`ga:users`*100 as growth FROM hhh.hhh_weekly_pagetitle p1, hhh_weekly_pagetitle p2 where p1.`ga:pageTitle`= p2.`ga:pageTitle` and p1.`ga:isoWeek`='22' and p2.`ga:isoWeek`=23 order by (p2.`ga:users`-p1.`ga:users`)/p1.`ga:users` desc;")
- df = pd.DataFrame(columns=('title','prev_week','cur_week','growth'))
- idx=0
- for c in cursor:
- df.loc[idx]=[c['title'],c['prev_week'],c['cur_week'],c['growth']]
- # df.loc[idx]=['okok',333]
- idx+=1
- save_sheet(df,'WeeklyReport','pagetitle')
- t = threading.Thread(target = do_jobs)
- t.start()
- t.join()
|