get_cost.py 5.4 KB

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