get_cost.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. from app.api_data.tx_ad_cost.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. import logging
  7. db = MysqlUtils()
  8. du = DateUtils()
  9. max_workers = 10
  10. def get_accounts(filter=None):
  11. if filter:
  12. if filter == 'MP':
  13. return db.quchen_text.getData(
  14. "select account_id,access_token,name channel from advertiser_vx where (name !='' or name is not null)")
  15. else:
  16. return db.quchen_text.getData(
  17. "select account_id,access_token,name channel from advertiser_qq where (name !='' or name is not null)")
  18. return db.quchen_text.getData(
  19. "select account_id,access_token,name channel,'MP' flag from advertiser_vx where (name !='' or name is not null) union "
  20. "select account_id,access_token,name channel,'GDT' flag from advertiser_qq where (name !='' or name is not null)")
  21. def ad(dt):
  22. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(ad_id) from ad_cost_day a
  23. left join (
  24. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  25. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  26. ) b on a.account_id=b.account_id
  27. where a.dt='{dt}'
  28. GROUP BY b.account_id,b.access_token,b.type"""
  29. accounts = db.quchen_text.getData(sql)
  30. executor = ThreadPoolExecutor(max_workers=max_workers)
  31. for account in accounts:
  32. executor.submit(get_ad_info, account[0], account[1], account[2], account[3], dt)
  33. executor.shutdown()
  34. """广告日消耗"""
  35. def ad_cost_day(st, et):
  36. executor = ThreadPoolExecutor(max_workers=max_workers)
  37. for account in get_accounts():
  38. executor.submit(get_ad_cost_day, account[0], account[1], account[3], st, et)
  39. executor.shutdown()
  40. def adcreative(dt):
  41. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(adcreative_id) from ad_info a
  42. left join (
  43. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  44. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  45. ) b on a.account_id=b.account_id
  46. where a.dt='{dt}'
  47. GROUP BY b.account_id,b.access_token,b.type having b.account_id is not null """
  48. accounts = db.quchen_text.getData(sql)
  49. # print(accounts)
  50. executor = ThreadPoolExecutor(max_workers=max_workers)
  51. for account in accounts:
  52. executor.submit(get_adcreatives, account[0], account[1], account[2], account[3], dt)
  53. executor.shutdown()
  54. def image(dt):
  55. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(image_id) from adcreative_info a
  56. left join (
  57. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  58. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  59. ) b on a.account_id=b.account_id
  60. where a.dt='{dt}' and a.is_video=0
  61. GROUP BY b.account_id,b.access_token,b.type"""
  62. accounts = db.quchen_text.getData(sql)
  63. executor = ThreadPoolExecutor(max_workers=max_workers)
  64. for account in accounts:
  65. executor.submit(images_info_get, account[0], account[1], account[3])
  66. executor.shutdown()
  67. def video(dt):
  68. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(image_id) from adcreative_info a
  69. left join (
  70. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  71. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  72. ) b on a.account_id=b.account_id
  73. where a.dt='{dt}' and a.is_video=1
  74. GROUP BY b.account_id,b.access_token,b.type"""
  75. accounts = db.quchen_text.getData(sql)
  76. executor = ThreadPoolExecutor(max_workers=max_workers)
  77. for account in accounts:
  78. executor.submit(video_info_get, account[0], account[1], account[3])
  79. executor.shutdown()
  80. def campaign(dt):
  81. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(campaign_id) from ad_info a
  82. left join (
  83. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  84. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  85. ) b on a.account_id=b.account_id
  86. where a.dt='{dt}'
  87. GROUP BY b.account_id,b.access_token,b.type having b.account_id is not null """
  88. accounts = db.quchen_text.getData(sql)
  89. executor = ThreadPoolExecutor(max_workers=max_workers)
  90. for account in accounts:
  91. executor.submit(get_campaign, account[0], account[1], account[2], account[3], dt)
  92. executor.shutdown()
  93. def run(dt):
  94. """
  95. 1.拉取有消耗的广告
  96. 2.用有消耗的广告id 去拉取广告基础信息
  97. 3.用第2步获取的创意id 去拉取广告创意基础信息
  98. 4.用创意信息中的图片id 去获取图片的基础信息
  99. """
  100. try:
  101. logging.info('获取广告id,开始')
  102. ad_cost_day(dt, dt)
  103. logging.info('获取广告id,结束')
  104. logging.info('获取广告基础信息,开始')
  105. ad(dt)
  106. logging.info('获取广告基础信息,结束')
  107. logging.info('获取广告创意素材,开始')
  108. adcreative(dt)
  109. logging.info('获取广告创意素材,结束')
  110. logging.info('获取广告计划信息,开始')
  111. campaign(dt)
  112. logging.info('获取广告计划信息,结束')
  113. logging.info('获取图片信息,开始')
  114. image(dt)
  115. logging.info('获取图片信息,结束')
  116. logging.info('获取视频信息,开始')
  117. video(dt)
  118. logging.info('获取视频信息,结束')
  119. except:
  120. DingTalkUtils().send("拉取广告数据出错")
  121. def day():
  122. ad_cost_day(du.get_n_days(-10), du.get_n_days(0))
  123. def hourly():
  124. dt = du.getNow()
  125. run(dt)
  126. if __name__ == '__main__':
  127. from logging import handlers
  128. logging.basicConfig(
  129. handlers=[
  130. logging.handlers.RotatingFileHandler('./dw_image_cost.log',
  131. maxBytes=10 * 1024 * 1024,
  132. backupCount=5,
  133. encoding='utf-8')
  134. , logging.StreamHandler() # 供输出使用
  135. ],
  136. level=logging.INFO,
  137. format="%(asctime)s - %(levelname)s %(filename)s %(funcName)s %(lineno)s - %(message)s"
  138. )
  139. # MP
  140. # account_id = 18516323
  141. # access_token = '262deda76aec00c2e144e83bd3c0b2a2'
  142. #
  143. # account_id2= 14709511
  144. # access_token2 = 'e87f7b6f860eaeef086ddcc9c3614678'
  145. # run()
  146. # ad_cost_day('2020-04-08','2021-04-07')
  147. # day()
  148. #
  149. # day()
  150. # run('2021-05-10')
  151. # adcreative('2021-05-11')
  152. # video('2021-05-14')
  153. # campaign('2021-05-14')
  154. # ad_cost_day(du.get_n_days(-365), du.get_n_days(0))
  155. for dt in list(du.getDateLists(du.get_n_days(-10), du.get_n_days(0))):
  156. print(dt)
  157. campaign(dt)
  158. # run(dt)
  159. # ad(dt)
  160. # adcreative(dt)
  161. # image(dt)
  162. # video(dt)
  163. # campaign(du.get_n_days(0))
  164. run(du.get_n_days(0))