GA_Keywords.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import traceback
  2. import copy
  3. import operator
  4. import codecs
  5. import sys
  6. import os
  7. import searchconsole
  8. import dataset
  9. import datetime
  10. import time
  11. fname=os.path.abspath(__file__)
  12. elmts=fname.split(os.path.sep)
  13. path2=os.path.sep.join(elmts[0:-1])
  14. keysdir=path2+os.path.sep+'keys'+os.path.sep
  15. account = searchconsole.authenticate(client_config=keysdir+'client_secret.json',credentials=keysdir+'credentials.json')
  16. #account = searchconsole.authenticate(client_config=keysdir+'client_secret.json',serialize=keysdir+'credentials.json')
  17. #account = searchconsole.authenticate(client_config=keysdir+'client_secrets.json',
  18. # serialize=keysdir+'credentials.json')
  19. webproperty = account['https://hhh.com.tw/']
  20. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  21. #db = dataset.connect('mysql://choozmo:pAssw0rd@127.0.0.1:3306/hhh?charset=utf8mb4')
  22. #db.query('delete from hhh_weekly_keywords')
  23. table=db['hhh_weekly_keywords']
  24. records=[]
  25. def gen_report(weeknum,begindate,enddate):
  26. #report=webproperty.query.range('2021-01-01', '2020-01-02').dimension('page','query').get()
  27. report=webproperty.query.range(begindate, enddate).dimension('query').get()
  28. # report=webproperty.query.range(begindate, days=30).dimension('query').get()
  29. # report=webproperty.query.range(begindate, days=30).dimension('query').get()
  30. ts = time.time()
  31. sessionid=int(ts)
  32. for r in report:
  33. # print(r)
  34. query=r[0]
  35. clicks=int(r[1])
  36. impressions=r[2]
  37. ctr=r[3]
  38. position=r[4]
  39. if clicks >=3:
  40. records.append({'sessionid':sessionid,'query':query,'clicks':clicks,'impressions':int(impressions),'ctr':float(ctr),'position':float(position),'dt':datetime.datetime.now(),'weeknum':weeknum})
  41. #gen_report('21','2021-05-24','2020-05-30')
  42. #gen_report('22','2021-05-31','2020-06-06')
  43. #gen_report('23','2021-06-07','2020-06-13')
  44. #gen_report('24','2021-06-14','2020-06-18')
  45. #gen_report('25','2021-06-19','2020-06-25')
  46. #gen_report('25','2021-06-19','2020-06-25')
  47. #gen_report('26','2021-06-01','2021-06-27')
  48. #gen_report('27','2021-06-28','2021-07-02')
  49. #gen_report('27','2021-08-08','2021-08-14')
  50. #gen_report('28','2021-08-13','2021-08-19')
  51. #gen_report('29','2021-08-20','2021-08-27')
  52. #gen_report('28','2021-08-15','2021-08-21')
  53. #gen_report('2','2021-12-29','2022-01-05')
  54. #gen_report('2','2021-12-29','2022-01-05')
  55. #gen_report('3','2022-01-06','2022-01-12')
  56. #gen_report('4','2022-01-13','2022-01-21')
  57. #gen_report('5','2022-01-22','2022-01-28')
  58. #gen_report('6','2022-01-29','2022-02-04')
  59. #gen_report('7','2022-02-05','2022-02-11')
  60. #gen_report('8','2022-02-12','2022-02-18')
  61. #gen_report('9','2022-02-19','2022-02-25')
  62. #gen_report('10','2022-02-26','2022-03-04')
  63. gen_report('11','2022-03-05','2022-03-11')
  64. print('complete')
  65. db.begin()
  66. for r in records:
  67. try:
  68. table.insert(r)
  69. db.commit()
  70. except:
  71. db.rollback()
  72. print('done')