get_cost.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 run(dt):
  77. """
  78. 1.拉取有消耗的广告
  79. 2.用有消耗的广告id 去拉取广告基础信息
  80. 3.用第2步获取的创意id 去拉取广告创意基础信息
  81. 4.用创意信息中的图片id 去获取图片的基础信息
  82. """
  83. try:
  84. ad_cost_day(dt, dt)
  85. ad(dt)
  86. adcreative(dt)
  87. image(dt)
  88. video(dt)
  89. except:
  90. DingTalkUtils.send("拉取广告数据出错")
  91. def day():
  92. ad_cost_day(du.get_n_days(-10), du.get_n_days(0))
  93. def hourly():
  94. dt = du.getNow()
  95. run(dt)
  96. if __name__ == '__main__':
  97. # MP
  98. # account_id = 18516323
  99. # access_token = '262deda76aec00c2e144e83bd3c0b2a2'
  100. #
  101. # account_id2= 14709511
  102. # access_token2 = 'e87f7b6f860eaeef086ddcc9c3614678'
  103. # run()
  104. # ad_cost_day('2020-04-08','2021-04-07')
  105. # day()
  106. #
  107. # day()
  108. # run('2021-05-10')
  109. # adcreative('2021-05-11')
  110. video('2021-05-14')
  111. # for dt in list(reversed(du.getDateLists('2020-04-08','2021-04-09'))):
  112. # print(dt)
  113. # ad(dt)
  114. # adcreative(dt)
  115. # image(dt)