get_cost.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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"""
  44. accounts = db.quchen_text.getData(sql)
  45. executor = ThreadPoolExecutor(max_workers=max_workers)
  46. for account in accounts:
  47. executor.submit(get_adcreatives, account[0], account[1],account[2],account[3],dt)
  48. executor.shutdown()
  49. def image(dt):
  50. sql=f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(image_id) from adcreative_info a
  51. left join (
  52. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  53. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  54. ) b on a.account_id=b.account_id
  55. where a.dt='{dt}'
  56. GROUP BY b.account_id,b.access_token,b.type"""
  57. accounts = db.quchen_text.getData(sql)
  58. executor = ThreadPoolExecutor(max_workers=max_workers)
  59. for account in accounts:
  60. executor.submit(images_info_get, account[0], account[1], account[3])
  61. executor.shutdown()
  62. def run(dt):
  63. """
  64. 1.拉取有消耗的广告
  65. 2.用有消耗的广告id 去拉取广告基础信息
  66. 3.用第2步获取的创意id 去拉取广告创意基础信息
  67. 4.用创意信息中的图片id 去获取图片的基础信息
  68. """
  69. try:
  70. ad_cost_day(dt,dt)
  71. ad(dt)
  72. adcreative(dt)
  73. image(dt)
  74. except:
  75. DingTalkUtils.send("拉取广告数据出错")
  76. def day():
  77. dt = du.getNow()
  78. run(dt)
  79. if __name__ == '__main__':
  80. # MP
  81. # account_id = 18516323
  82. # access_token = '262deda76aec00c2e144e83bd3c0b2a2'
  83. #
  84. # account_id2= 14709511
  85. # access_token2 = 'e87f7b6f860eaeef086ddcc9c3614678'
  86. # run()
  87. # ad_cost_day('2020-04-08','2021-04-07')
  88. #
  89. # day()
  90. # run('2021-04-05')
  91. for dt in list(reversed(du.getDateLists('2020-04-08','2021-04-07'))):
  92. ad(dt)
  93. adcreative(dt)
  94. image(dt)