get_ad_hourly.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Mon Jul 6 10:23:11 2020
  5. @author: chencong
  6. """
  7. import requests
  8. import time
  9. import json
  10. import random
  11. import pymysql
  12. from datetime import datetime
  13. import token_list as tl
  14. import importlib
  15. from concurrent.futures import ThreadPoolExecutor
  16. importlib.reload(tl)
  17. token_list_q = tl.token_list_qq
  18. token_list_v = tl.token_list_vx
  19. def hourly_reports_get(access_token,account_id,date,level,fields) :
  20. interface = 'hourly_reports/get'
  21. url = 'https://api.e.qq.com/v1.1/' + interface
  22. common_parameters = {
  23. 'access_token': access_token,
  24. 'timestamp': int(time.time()),
  25. 'nonce': str(time.time()) + str(random.randint(0, 999999)),
  26. }
  27. parameters = {
  28. "account_id": account_id,
  29. "level":level,
  30. "date_range":
  31. {
  32. "start_date": date,
  33. "end_date": date
  34. },
  35. "page":1,
  36. "page_size":1000,
  37. "fields":fields
  38. }
  39. parameters.update(common_parameters)
  40. for k in parameters:
  41. if type(parameters[k]) is not str:
  42. parameters[k] = json.dumps(parameters[k])
  43. r = requests.get(url, params = parameters)
  44. return r.json()
  45. def mysql_insert_daily_vx(data):
  46. db = pymysql.connect('rm-bp1c9cj79872tx3aaro.mysql.rds.aliyuncs.com','superc','Cc719199895','quchen_text')
  47. cursor = db.cursor()
  48. time1 = time.time()
  49. #sql1 = 'delete from daily_vx where date = %s'
  50. sql2 = 'insert ignore into hourly_vx (cost,view_count,valid_click_count,ctr,official_account_follow_rate,order_amount,order_roi,order_count,order_rate,order_unit_price,web_order_cost,first_day_order_amount,first_day_order_count,account_id,date) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
  51. try:
  52. cursor.executemany(sql2,data)
  53. db.commit()
  54. cost_time =round((time.time()-time1)/60,1)
  55. print('insert_hourly_vx access',len(data),'cost_minutes:',cost_time)
  56. except:
  57. db.rollback()
  58. print('insert_hourly_vx defeat')
  59. def mysql_insert_daily_qq(data):
  60. db = pymysql.connect('rm-bp1c9cj79872tx3aaro.mysql.rds.aliyuncs.com','superc','Cc719199895','quchen_text')
  61. cursor = db.cursor()
  62. time1 = time.time()
  63. #sql1 = 'delete from daily_qq where date = %s'
  64. sql2 = 'insert ignore into hourly_qq (view_count,valid_click_count,ctr,cpc,cost,web_order_count,web_order_rate,web_order_cost,follow_count,account_id,date) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
  65. try:
  66. cursor.executemany(sql2,data)
  67. db.commit()
  68. cost_time =round((time.time()-time1)/60,1)
  69. print('insert_hourly_qq access',len(data),'cost_minutes:',cost_time)
  70. except:
  71. db.rollback()
  72. print('insert_hourly_qq defeat')
  73. def get_qq_data(x,start_time,start_date_unixtime,q_li):
  74. a = hourly_reports_get(x[2], x[0], start_time, "REPORT_LEVEL_ADVERTISER", ('hour', 'view_count', 'valid_click_count', 'ctr', 'cpc', 'cost', 'web_order_count', 'web_order_rate','web_order_cost', 'follow_count'))
  75. if 'data' in a.keys():
  76. for b in a['data']['list']:
  77. if b['hour'] == int((time.time() - ((time.time() + 8 * 3600) // 86400 * 86400 - 8 * 3600)) // 3600) - 1:
  78. b['account_id'] = x[0]
  79. b['date'] = time.strftime("%Y-%m-%d %H", time.localtime(start_date_unixtime + int(b['hour']) * 3600))
  80. del b['hour']
  81. b = tuple(b.values())
  82. q_li.append(b)
  83. # print(q_li)
  84. def get_vx_data(y,start_time,start_date_unixtime,v_li):
  85. c = hourly_reports_get(y[2], y[0], start_time, "REPORT_LEVEL_ADVERTISER_WECHAT", ('hour', 'cost', 'view_count', 'valid_click_count', 'ctr', 'official_account_follow_rate', 'order_amount','order_roi', 'order_count', 'order_rate', 'order_unit_price', 'web_order_cost', 'first_day_order_amount','first_day_order_count'))
  86. if 'data' in c.keys():
  87. for d in c['data']['list']:
  88. if d['hour'] == int((time.time() - ((time.time() + 8 * 3600) // 86400 * 86400 - 8 * 3600)) // 3600) - 1:
  89. d['account_id'] = y[0]
  90. d['date'] = time.strftime("%Y-%m-%d %H", time.localtime(start_date_unixtime + int(d['hour']) * 3600))
  91. del d['hour']
  92. d = tuple(d.values())
  93. v_li.append(d)
  94. def start_all_job():
  95. start_date_unixtime=int((time.time()+8*3600)//86400*86400-8*3600)
  96. start_time = time.strftime("%Y-%m-%d",time.localtime(start_date_unixtime))
  97. print(time.strftime("%Y-%m-%d %H",time.localtime(time.time()//3600*3600)))
  98. executor = ThreadPoolExecutor(max_workers=30)
  99. start=time.time()
  100. q_li=[]
  101. for x in token_list_q :
  102. executor.submit(get_qq_data,x,start_time,start_date_unixtime,q_li)
  103. executor.shutdown()
  104. mysql_insert_daily_qq(tuple(q_li))
  105. end=time.time()
  106. print(end-start)
  107. executor = ThreadPoolExecutor(max_workers=30)
  108. v_li=[]
  109. for y in token_list_v :
  110. executor.submit(get_vx_data, y, start_time, start_date_unixtime, v_li)
  111. executor.shutdown()
  112. mysql_insert_daily_vx(tuple(v_li))
  113. end2=time.time()
  114. print(end2-end)
  115. if __name__ == '__main__':
  116. print(datetime.today())
  117. start_all_job()