get_cost.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. db = MysqlUtils()
  6. du = DateUtils()
  7. max_workers =10
  8. def get_accounts(filter=None):
  9. if filter:
  10. if filter=='MP':
  11. return db.quchen_text.getData("select account_id,access_token,name channel from advertiser_vx where (name !='' or name is not null)")
  12. else:
  13. return db.quchen_text.getData("select account_id,access_token,name channel from advertiser_qq where (name !='' or name is not null)")
  14. 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 "
  15. "select account_id,access_token,name channel,'GDT' flag from advertiser_qq where (name !='' or name is not null)")
  16. def ad(dt):
  17. sql =f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(ad_id) from ad_cost_day a
  18. left join (
  19. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  20. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  21. ) b on a.account_id=b.account_id
  22. where a.dt='{dt}'
  23. GROUP BY b.account_id,b.access_token,b.type"""
  24. accounts = db.quchen_text.getData(sql)
  25. executor = ThreadPoolExecutor(max_workers=max_workers)
  26. for account in accounts:
  27. executor.submit(get_ad_info, account[0], account[1], account[2],account[3],dt)
  28. executor.shutdown()
  29. """广告日消耗"""
  30. def ad_cost_day(dt):
  31. st = et = dt
  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 day():
  63. """
  64. 1.拉取有消耗的广告
  65. 2.用有消耗的广告id 去拉取广告基础信息
  66. 3.用第2步获取的创意id 去拉取广告创意基础信息
  67. 4.用创意信息中的图片id 去获取图片的基础信息
  68. """
  69. dt = du.getNow()
  70. ad_cost_day(dt)
  71. ad(dt)
  72. adcreative(dt)
  73. image(dt)
  74. if __name__ == '__main__':
  75. # MP
  76. # account_id = 18516323
  77. # access_token = '262deda76aec00c2e144e83bd3c0b2a2'
  78. #
  79. # account_id2= 14709511
  80. # access_token2 = 'e87f7b6f860eaeef086ddcc9c3614678'
  81. # run()
  82. # ad_cost_day()
  83. #
  84. day()