designer_report.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 * FROM hhh.rank_details where dt > '2021-06-29 00:00:00' order by dt desc")
  29. win_lst={}
  30. c100_lst={}
  31. searchome_lst={}
  32. hhh_lst={}
  33. other_lst={}
  34. lost_lst={}
  35. for c in cursor:
  36. # print(c['designer'],c['title'],c['dt'],c['searchome'],c['c_100'],c['hhh'])
  37. searchome=int(c['searchome'])
  38. c_100=int(c['c_100'])
  39. hhh=int(c['hhh'])
  40. designer=c['designer']
  41. if hhh > 0 and c_100==-1 and searchome ==-1:
  42. win_lst[designer]=hhh
  43. if hhh > 0 and searchome ==-1:
  44. if c_100 !=-1 and hhh < c_100:
  45. win_lst[designer]=hhh
  46. if c_100 !=-1 and hhh > c_100:
  47. c100_lst[designer]=1
  48. if hhh > 0 and searchome!=-1 and searchome<hhh:
  49. if win_lst.get(designer) is None:
  50. if c100_lst.get(designer) is None:
  51. searchome_lst[designer]=searchome
  52. lost_lst[designer]=hhh
  53. if hhh > 0 and searchome!=-1 and hhh<searchome:
  54. if win_lst.get(designer) is None:
  55. if searchome_lst.get(designer) is None:
  56. hhh_lst[designer]=hhh
  57. if win_lst.get(designer) is None and hhh_lst.get(designer) is None and searchome_lst.get(designer) is None and c100_lst.get(designer) is None:
  58. print(designer)
  59. other_lst[designer]=1
  60. print(len(win_lst.keys()))
  61. print(len(searchome_lst.keys()))
  62. print(len(hhh_lst.keys()))
  63. print(len(c100_lst.keys()))
  64. print(len(other_lst.keys()))
  65. vv=0
  66. for k,v in lost_lst.items():
  67. vv+=v
  68. ww=0
  69. for k,v in searchome_lst.items():
  70. ww+=v
  71. print(vv/len(lost_lst.items()))
  72. print(ww/len(searchome_lst.items()))
  73. print(hhh_lst)
  74. # df = pd.DataFrame(columns=('query','users','newusers','pageviews','bouncerate','pvpersession','url'))
  75. # idx=0
  76. # for c in cursor:
  77. # df.loc[idx]=[c['query'],c['users'],c['newusers'],c['pageviews'],c['bouncerate'],c['pvpersession'],c['url']]
  78. # df.loc[idx]=['okok',333]
  79. # idx+=1
  80. # save_sheet(df,'ga_x_gsc','results')
  81. # save_sheet(df,'ga_x_gsc','with_url')
  82. t = threading.Thread(target = do_jobs)
  83. t.start()
  84. t.join()