get_cost.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. from app.api_data.tx_ad_cost.cost_util import *
  2. from model.DateUtils import DateUtils
  3. from model.DataBaseUtils import MysqlUtils
  4. from concurrent.futures import ThreadPoolExecutor
  5. from model.DingTalkUtils import DingTalkUtils
  6. import logging
  7. db = MysqlUtils()
  8. du = DateUtils()
  9. max_workers =10
  10. def get_accounts(filter=None):
  11. if filter:
  12. if filter=='MP':
  13. return db.quchen_text.getData("select account_id,access_token,name channel from advertiser_vx where (name !='' or name is not null)")
  14. else:
  15. return db.quchen_text.getData("select account_id,access_token,name channel from advertiser_qq where (name !='' or name is not null)")
  16. return db.quchen_text.getData("select account_id,access_token,name channel,'MP' flag from advertiser_vx where (name !='' or name is not null) union "
  17. "select account_id,access_token,name channel,'GDT' flag from advertiser_qq where (name !='' or name is not null)")
  18. def ad(dt):
  19. sql =f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(ad_id) from ad_cost_day a
  20. left join (
  21. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  22. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  23. ) b on a.account_id=b.account_id
  24. where a.dt='{dt}'
  25. GROUP BY b.account_id,b.access_token,b.type"""
  26. accounts = db.quchen_text.getData(sql)
  27. executor = ThreadPoolExecutor(max_workers=max_workers)
  28. for account in accounts:
  29. executor.submit(get_ad_info, account[0], account[1], account[2],account[3],dt)
  30. executor.shutdown()
  31. """广告日消耗"""
  32. def ad_cost_day(st,et):
  33. executor = ThreadPoolExecutor(max_workers=max_workers)
  34. for account in get_accounts():
  35. executor.submit(get_ad_cost_day, account[0], account[1],account[3],st,et)
  36. executor.shutdown()
  37. def adcreative(dt):
  38. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(adcreative_id) from ad_info a
  39. left join (
  40. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  41. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  42. ) b on a.account_id=b.account_id
  43. where a.dt='{dt}'
  44. GROUP BY b.account_id,b.access_token,b.type having b.account_id is not null """
  45. accounts = db.quchen_text.getData(sql)
  46. # print(accounts)
  47. executor = ThreadPoolExecutor(max_workers=max_workers)
  48. for account in accounts:
  49. executor.submit(get_adcreatives, account[0], account[1],account[2],account[3],dt)
  50. executor.shutdown()
  51. def image(dt):
  52. sql=f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(image_id) from adcreative_info a
  53. left join (
  54. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  55. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  56. ) b on a.account_id=b.account_id
  57. where a.dt='{dt}' and a.is_video=0
  58. GROUP BY b.account_id,b.access_token,b.type"""
  59. accounts = db.quchen_text.getData(sql)
  60. executor = ThreadPoolExecutor(max_workers=max_workers)
  61. for account in accounts:
  62. executor.submit(images_info_get, account[0], account[1], account[3])
  63. executor.shutdown()
  64. def video(dt):
  65. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(image_id) from adcreative_info a
  66. left join (
  67. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  68. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  69. ) b on a.account_id=b.account_id
  70. where a.dt='{dt}' and a.is_video=1
  71. GROUP BY b.account_id,b.access_token,b.type"""
  72. accounts = db.quchen_text.getData(sql)
  73. executor = ThreadPoolExecutor(max_workers=max_workers)
  74. for account in accounts:
  75. executor.submit(video_info_get, account[0], account[1], account[3])
  76. executor.shutdown()
  77. def campaign(dt):
  78. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(campaign_id) from ad_info a
  79. left join (
  80. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  81. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  82. ) b on a.account_id=b.account_id
  83. where a.dt='{dt}'
  84. GROUP BY b.account_id,b.access_token,b.type having b.account_id is not null """
  85. accounts = db.quchen_text.getData(sql)
  86. executor = ThreadPoolExecutor(max_workers=max_workers)
  87. for account in accounts:
  88. executor.submit(get_campaign, account[0], account[1], account[2], account[3],dt)
  89. executor.shutdown()
  90. def run(dt):
  91. """
  92. 1.拉取有消耗的广告
  93. 2.用有消耗的广告id 去拉取广告基础信息
  94. 3.用第2步获取的创意id 去拉取广告创意基础信息
  95. 4.用创意信息中的图片id 去获取图片的基础信息
  96. """
  97. try:
  98. logging.info('获取广告id,开始')
  99. ad_cost_day(dt, dt)
  100. logging.info('获取广告id,结束')
  101. logging.info('获取广告基础信息,开始')
  102. ad(dt)
  103. logging.info('获取广告基础信息,结束')
  104. logging.info('获取广告创意素材,开始')
  105. adcreative(dt)
  106. logging.info('获取广告创意素材,结束')
  107. logging.info('获取图片信息,开始')
  108. image(dt)
  109. logging.info('获取图片信息,结束')
  110. logging.info('获取视频信息,开始')
  111. video(dt)
  112. logging.info('获取视频信息,结束')
  113. except:
  114. DingTalkUtils.send("拉取广告数据出错")
  115. def day():
  116. ad_cost_day(du.get_n_days(-10), du.get_n_days(0))
  117. def hourly():
  118. dt = du.getNow()
  119. run(dt)
  120. if __name__ == '__main__':
  121. # MP
  122. # account_id = 18516323
  123. # access_token = '262deda76aec00c2e144e83bd3c0b2a2'
  124. #
  125. # account_id2= 14709511
  126. # access_token2 = 'e87f7b6f860eaeef086ddcc9c3614678'
  127. # run()
  128. # ad_cost_day('2020-04-08','2021-04-07')
  129. # day()
  130. #
  131. # day()
  132. # run('2021-05-10')
  133. # adcreative('2021-05-11')
  134. # video('2021-05-14')
  135. # campaign('2021-05-14')
  136. for dt in list(reversed(du.getDateLists('2020-05-18','2021-05-17'))):
  137. print(dt)
  138. # ad(dt)
  139. adcreative(dt)
  140. # image(dt)
  141. # video(dt)