dw_image_cost_day.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import logging
  2. from model.DataBaseUtils import MysqlUtils,CkUtils
  3. from model.DateUtils import DateUtils
  4. from model.DingTalkUtils import DingTalkUtils
  5. logging.getLogger().setLevel(logging.WARNING)
  6. import pandas as pd
  7. db = MysqlUtils()
  8. ck = CkUtils()
  9. du=DateUtils()
  10. def run(dt):
  11. sql=f"""SELECT a.dt,b.type,sum(a.cost),sum(view_count),sum(click_count),sum(follow_count),sum(order_count),sum(order_amount),
  12. title,description,book,platform,stage,ifnull(image_id,'')
  13. from
  14. ad_cost_day a
  15. left join ad_info b on a.ad_id=b.ad_id
  16. left join adcreative_info c on b.adcreative_id=c.adcreative_id
  17. left join channel_by_account_daily e on b.account_id=e.account_id and a.dt=e.dt
  18. left join channel_info_daily f on e.channel=f.channel and e.dt=f.dt
  19. where a.dt='{dt}'
  20. group by a.dt,b.type,title,description,book,platform,stage,image_id
  21. """
  22. data = db.quchen_text.get_data_list(sql)
  23. # print(data)
  24. # 图片链接拼接
  25. li = []
  26. for i in data:
  27. # print(i)
  28. li.extend(i[-1].split(','))
  29. # print(li)
  30. sql3 = f"select image_id,preview_url from image_info where image_id in ({str(set(li))[1:-1]})"
  31. image_di = {}
  32. image_data = db.quchen_text.getData(sql3)
  33. for x in image_data:
  34. image_di[x[0]] = x[1]
  35. # print(image_di)
  36. for i in data:
  37. y = ''
  38. for j in i[-1].split(','):
  39. if image_di.get(j):
  40. y = y + ',' + image_di.get(j)
  41. i.append(y[1:])
  42. # print(data)
  43. db.dm.execute(f'delete from dw_image_cost_day where dt="{dt}"')
  44. db.dm.executeMany("replace into dw_image_cost_day values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",data)
  45. def day():
  46. try:
  47. run(du.getNow())
  48. except:
  49. DingTalkUtils.send("广告数据清洗失败")
  50. if __name__ == '__main__':
  51. day()
  52. # run('2021-04-08')