dw_image_cost_day.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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,e.channel,pitcher,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,e.channel,pitcher
  21. """
  22. # print(sql)
  23. data = db.quchen_text.get_data_list(sql)
  24. # print(data)
  25. # 图片链接拼接
  26. li = []
  27. for i in data:
  28. # print(i)
  29. li.extend(i[-1].split(','))
  30. # print(li)
  31. sql3 = f"select image_id,preview_url,signature from image_info where image_id in ({str(set(li))[1:-1]})"
  32. image_di = {}
  33. image_data = db.quchen_text.getData(sql3)
  34. for x in image_data:
  35. image_di[x[0]] = (x[1],x[2])
  36. # print(image_di)
  37. for i in data:
  38. y = ''
  39. z = ''
  40. for j in i[-1].split(','):
  41. if image_di.get(j):
  42. y = y + ',' + image_di.get(j)[0]
  43. z = z + ',' + image_di.get(j)[1]
  44. i.append(y[1:])
  45. i.append(z[1:])
  46. # print(data)
  47. # print(data)
  48. # exit(0)
  49. db.dm.execute(f'delete from dw_image_cost_day where dt="{dt}"')
  50. 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,%s,%s,%s)",data)
  51. def hourly():
  52. try:
  53. run(du.getNow())
  54. except:
  55. DingTalkUtils.send("广告数据清洗失败")
  56. def day():
  57. for i in du.getDateLists(du.get_n_days(-10), du.get_n_days(-1)):
  58. # print(i)
  59. run(i)
  60. if __name__ == '__main__':
  61. run('2021-05-10')
  62. # for i in du.getDateLists('2021-04-01','2021-04-13'):
  63. # print(i)
  64. # run(i)