update_cost_data.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from model.DateUtils import DateUtils
  2. from app.api_data.tx_ad_cost import get_cost_older,get_cost
  3. from app.etl.data_stat_run import do_cost
  4. from app.etl.dm.dm_pitcher_daily_overview import dm_pitcher_daily_overview
  5. from app.etl.dw.dw_channel_daily import dw_channel_daily
  6. from app.etl.dw.dw_pitcher_daily import dw_pitcher_trend
  7. from app.etl.src.src_book_info import src_book_info
  8. from app.etl.dw.dw_book_trend import book_trend
  9. from model.DateUtils import DateUtils
  10. from model.DataBaseUtils import MysqlUtils
  11. from app.api_data.tx_ad_cost.get_cost_older import get_v_data, mysql_insert_daily_vx, get_q_data, mysql_insert_daily_qq
  12. import logging
  13. import time
  14. from logging import handlers
  15. db = MysqlUtils()
  16. du = DateUtils()
  17. def get_data(st, et):
  18. # 1.获取数据
  19. print(st, et)
  20. get_cost_older.run(st, et)
  21. # 同步到ck
  22. do_cost(st, et)
  23. # 2.数据处理
  24. src_book_info() # 书籍卡点信息
  25. # book_annual_expect_profit.run() # 年预期收益
  26. dw_channel_daily()
  27. dw_pitcher_trend()
  28. book_trend()
  29. dm_pitcher_daily_overview()
  30. def get_data_vx_adinfo(channel, st, et):
  31. # 用于处理单个微信号相关信息
  32. sql = '''
  33. select account_id,wechat_account_id,access_token,refresh_token,name,
  34. ifnull(stage,''),ifnull(pitcher,''),ifnull(platform,''),ifnull(book,'') from advertiser_vx
  35. where account_id in (select account_id from channel_by_account_daily
  36. where channel ='{}' and dt in (select max(dt) from channel_by_account_daily cbad)
  37. order by dt desc )
  38. '''.format(channel)
  39. token_list_v = db.quchen_text.getData(sql)
  40. print(token_list_v)
  41. account_id=token_list_v[0][0]
  42. access_token=token_list_v[0][2]
  43. type='MP'
  44. #获取广告id
  45. get_cost.get_ad_cost_day(account_id, access_token, type, st, et)
  46. for dt in du.getDateLists(st,et):
  47. #获取广告基础信息
  48. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(ad_id) from ad_cost_day a
  49. left join (
  50. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  51. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  52. ) b on a.account_id=b.account_id
  53. where a.dt='{dt}' and a.account_id ='{account_id}'
  54. GROUP BY b.account_id,b.access_token,b.type"""
  55. accounts = db.quchen_text.getData(sql)
  56. for account in accounts:
  57. get_cost.get_ad_info(account[0], account[1], account[2], account[3], dt)
  58. #获取广告创意素材
  59. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(adcreative_id) from ad_info a
  60. left join (
  61. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  62. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  63. ) b on a.account_id=b.account_id
  64. where a.dt='{dt}' and a.account_id='{account_id}'
  65. GROUP BY b.account_id,b.access_token,b.type having b.account_id is not null """
  66. accounts = db.quchen_text.getData(sql)
  67. for account in accounts:
  68. get_cost.get_adcreatives(account[0], account[1], account[2], account[3], dt)
  69. #获取广告计划信息
  70. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(campaign_id) from ad_info a
  71. left join (
  72. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  73. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  74. ) b on a.account_id=b.account_id
  75. where a.dt='{dt}' and a.account_id='{account_id}'
  76. GROUP BY b.account_id,b.access_token,b.type having b.account_id is not null """
  77. accounts = db.quchen_text.getData(sql)
  78. for account in accounts:
  79. get_cost.get_campaign(account[0], account[1], account[2], account[3], dt)
  80. #获取图片信息
  81. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(image_id) from adcreative_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}' and a.account_id='{account_id}' and a.is_video=0
  87. GROUP BY b.account_id,b.access_token,b.type"""
  88. accounts = db.quchen_text.getData(sql)
  89. for account in accounts:
  90. get_cost.images_info_get( account[0], account[1], account[3])
  91. #获取视频信息
  92. sql = f"""SELECT b.account_id,b.access_token,b.type,GROUP_CONCAT(image_id) from adcreative_info a
  93. left join (
  94. select account_id,access_token,'MP' type from advertiser_vx where (name !='' or name is not null) union
  95. select account_id,access_token,'GDT' type from advertiser_qq where (name !='' or name is not null)
  96. ) b on a.account_id=b.account_id
  97. where a.dt='{dt}' and a.account_id='{account_id}' and a.is_video=1
  98. GROUP BY b.account_id,b.access_token,b.type"""
  99. accounts = db.quchen_text.getData(sql)
  100. for account in accounts:
  101. get_cost.video_info_get( account[0], account[1], account[3])
  102. def get_data_vx(channel, st, et):
  103. # 用于处理单个微信号相关信息
  104. sql = '''
  105. select account_id,wechat_account_id,access_token,refresh_token,name,
  106. ifnull(stage,''),ifnull(pitcher,''),ifnull(platform,''),ifnull(book,'') from advertiser_vx
  107. where account_id in (select account_id from channel_by_account_daily
  108. where channel ='{}' and dt in (select max(dt) from channel_by_account_daily cbad)
  109. order by dt desc )
  110. '''.format(channel)
  111. token_list_v = db.quchen_text.getData(sql)
  112. print(token_list_v)
  113. time1 = time.time()
  114. li = []
  115. for y in token_list_v:
  116. get_v_data(y, li, st, et)
  117. for _ in li:
  118. print(_)
  119. print('get_daily_vx:' + str(len(li)) + 'cost:' + str(int(time.time() - time1)))
  120. mysql_insert_daily_vx(li)
  121. do_cost(st, et)
  122. src_book_info() # 书籍卡点信息
  123. dw_channel_daily()
  124. dw_pitcher_trend()
  125. book_trend()
  126. dm_pitcher_daily_overview()
  127. def get_data_gdt(channel, st, et):
  128. # 用于处理单个微信号相关信息
  129. sql = '''select account_id,'',access_token,refresh_token,name,
  130. ifnull(stage,''),ifnull(pitcher,''),ifnull(platform,''),ifnull(book,'') from advertiser_qq
  131. where account_id in (select account_id from channel_by_account_daily
  132. where channel ='{}' and dt in (select max(dt) from channel_by_account_daily cbad)
  133. order by dt desc )
  134. '''.format(channel)
  135. print(sql)
  136. token_list_v = db.quchen_text.getData(sql)
  137. print(token_list_v)
  138. time1 = time.time()
  139. li = []
  140. for y in token_list_v:
  141. get_q_data(y, li, st, et)
  142. for _ in li:
  143. print(_)
  144. print('get_daily_qq:' + str(len(li)) + 'cost:' + str(int(time.time() - time1)))
  145. mysql_insert_daily_qq(li)
  146. do_cost(st, et)
  147. src_book_info() # 书籍卡点信息
  148. dw_channel_daily()
  149. dw_pitcher_trend()
  150. book_trend()
  151. dm_pitcher_daily_overview()
  152. if __name__ == "__main__":
  153. logging.basicConfig(
  154. handlers=[
  155. logging.handlers.RotatingFileHandler('./cost_data.log',
  156. maxBytes=10 * 1024 * 1024,
  157. backupCount=5,
  158. encoding='utf-8')
  159. , logging.StreamHandler() # 供输出使用
  160. ],
  161. level=logging.INFO,
  162. format="%(asctime)s - %(levelname)s %(filename)s %(funcName)s %(lineno)s - %(message)s"
  163. )
  164. st = du.get_n_days(-365)
  165. et = du.get_n_days(0)
  166. # get_data_vx(channel='丹青文阅', st=st, et=et)
  167. # get_data_gdt(channel='落枫文海', st=st, et=et)
  168. # get_data(st,et)
  169. # from app.etl import data_stat_task
  170. # data_stat_task.dw_daily_channel_cost('2019-07-12')
  171. get_data_vx_adinfo(channel='丹青文阅', st='2021-06-01', et='2021-06-05')