get_cost.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(dt):
  32. st = et = dt
  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"""
  45. accounts = db.quchen_text.getData(sql)
  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}'
  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 day():
  64. """
  65. 1.拉取有消耗的广告
  66. 2.用有消耗的广告id 去拉取广告基础信息
  67. 3.用第2步获取的创意id 去拉取广告创意基础信息
  68. 4.用创意信息中的图片id 去获取图片的基础信息
  69. """
  70. try:
  71. dt = du.getNow()
  72. ad_cost_day(dt)
  73. ad(dt)
  74. adcreative(dt)
  75. image(dt)
  76. except:
  77. DingTalkUtils.send("拉取广告数据出错")
  78. if __name__ == '__main__':
  79. # MP
  80. # account_id = 18516323
  81. # access_token = '262deda76aec00c2e144e83bd3c0b2a2'
  82. #
  83. # account_id2= 14709511
  84. # access_token2 = 'e87f7b6f860eaeef086ddcc9c3614678'
  85. # run()
  86. # ad_cost_day()
  87. #
  88. day()