get_ad_hourly.py 5.2 KB

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