MaterialDataClean.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. """
  2. desc : 素材库数据清洗
  3. 每日运行一次
  4. """
  5. from model.DataBaseUtils import MysqlUtils
  6. db = MysqlUtils()
  7. def title():
  8. sql = """select REPLACE(REPLACE(title, CHAR(10), ''), CHAR(13), '') content,
  9. cast(sum(cost) as float) consume_amount,
  10. sum(click_count) click_times,
  11. sum(view_count) view_times,
  12. cast(count(*) as decimal(10,2)) use_times,
  13. group_concat(distinct book) novels,
  14. max(dt) end_date,min(dt) start_date
  15. from dw_image_cost_day where title!='' and title is not null GROUP BY REPLACE(REPLACE(title, CHAR(10), ''), CHAR(13), '')
  16. """
  17. df = db.dm.pd_data_sql(sql)
  18. df["data_type"] = 'all'
  19. df['type'] = '1'
  20. df['create_by'] = '0'
  21. key = ["content", "type"]
  22. tag = ["view_times", "click_times", "novels", "start_date", "end_date", "create_by", 'data_type',
  23. 'consume_amount']
  24. table = "t_ads_content"
  25. db.zx_ads.dfsave2mysql(df, table, key, tag)
  26. def description():
  27. sql = """select REPLACE(REPLACE(description, CHAR(10), ''), CHAR(13), '') content,
  28. sum(cost) consume_amount,
  29. sum(click_count) click_times,
  30. sum(view_count) view_times,
  31. cast(count(*) as decimal(10,2)) use_times,
  32. group_concat(distinct book) novels,
  33. max(dt) end_date,min(dt) start_date
  34. from dw_image_cost_day where description!='' and description is not null GROUP BY REPLACE(REPLACE(description, CHAR(10), ''), CHAR(13), '')
  35. """
  36. df = db.dm.pd_data_sql(sql)
  37. df["data_type"] = 'all'
  38. df['type'] = '2'
  39. df['create_by'] = '0'
  40. key = ["content", "type"]
  41. tag = ['use_times', "view_times", "click_times", "novels", "start_date", "end_date", "create_by", 'data_type',
  42. 'consume_amount']
  43. table = "t_ads_content"
  44. db.zx_ads.dfsave2mysql(df, table, key, tag)
  45. def image():
  46. sql = """select signature,sum(consume_amount) consume_amount,
  47. sum(click_times) click_times,
  48. sum(view_times) view_times,
  49. sum(use_times) use_times,
  50. group_concat(distinct novels) novels ,
  51. max(end_date) end_date,
  52. min(start_date) start_date,
  53. min(content) content,
  54. min(type) type,
  55. if(locate(',',signature)>0,0,1) single_img,
  56. min(width ) width ,
  57. min(height ) height ,
  58. min(media_size) media_size ,
  59. min(media_format) media_format,
  60. min(video_length) video_length,
  61. min(video_bit_rate) video_bit_rate,
  62. 0 max_media_size,
  63. min(width)/if(min(height),min(height),1) aspect_ratio
  64. from (select replace(signature,' ,','') as signature ,
  65. sum(cost) consume_amount,
  66. sum(click_count) click_times,
  67. sum(use_times) use_times,
  68. sum(view_count) view_times,
  69. group_concat(distinct book) novels ,
  70. max(dt) end_date,
  71. min(dt) start_date,
  72. replace (min(preview_url),' ,','') as content,
  73. if(is_video=1,2,1) type,
  74. if(locate(',',signature)>0,0,1) single_img,
  75. min(width) width ,
  76. min(height) height,
  77. min(replace(if(left (size ,2)='0,',substring(size ,3),size) ,',0','')) media_size,
  78. min(replace(format ,' ,','')) media_format,
  79. min(video_length) video_length,
  80. min(video_bit_rate) video_bit_rate
  81. from dw_image_cost_day
  82. where signature is not null and signature !=''
  83. and length (replace (replace (signature,',',''),' ',''))>0
  84. GROUP BY signature,is_video) as foo
  85. group by signature
  86. """
  87. # df = db.dm.getData_pd(sql)
  88. df = db.dm.pd_data_sql(sql)
  89. # print(df)
  90. # 进行数据转换-----添加max_media_size
  91. for i in range(len(df['media_size'])):
  92. if not df['media_size'][i]:
  93. continue
  94. size_list = df['media_size'][i].split(',')
  95. max_size = 0
  96. for size_data in size_list:
  97. if size_data != 'None':
  98. if float(size_data) > float(max_size):
  99. max_size = str(size_data)
  100. df['max_media_size'][i] = max_size
  101. df['create_by'] = '0'
  102. df["data_type"] = 'all'
  103. key = ["signature"]
  104. tag = ['media_size', 'media_format', 'video_length', 'video_bit_rate',
  105. 'max_media_size', 'use_times', "view_times",
  106. "click_times",
  107. "novels", "start_date", "end_date", "create_by", "single_img",
  108. "content", 'consume_amount', 'type', 'width', 'height', 'aspect_ratio']
  109. table = "t_ads_media"
  110. db.zx_ads.dfsave2mysql(df, table, key, tag)
  111. def adcreative():
  112. sql = """select signature,title,article,
  113. sum(click_times) click_times,
  114. sum(view_times) view_times,
  115. sum(use_times) use_times,
  116. sum(consume_amount) consume_amount,
  117. group_concat(distinct novels) novels,
  118. min(start_date) start_date,
  119. max(end_date) end_date,
  120. min(media) media,
  121. min(channel) channel ,
  122. min(type) type,
  123. if(locate(',',signature)>0,0,1) single_img,
  124. min(width) width,
  125. min(height) height,
  126. min(media_size) media_size ,
  127. min(media_format) media_format,
  128. min(video_length) video_length,
  129. min(video_bit_rate) video_bit_rate,
  130. 0 max_media_size,
  131. min(width)/if(min(height),min(height),1) aspect_ratio
  132. from
  133. (select replace(signature ,' ,','') as signature,title,description article,
  134. sum(click_count) click_times,
  135. sum(view_count) view_times,
  136. sum(use_times) use_times,
  137. sum(cost) consume_amount,
  138. group_concat(distinct book) novels,
  139. min(dt) start_date,max(dt) end_date,
  140. min(replace(preview_url ,' ,','')) media,
  141. min(width) width ,
  142. min(height) height,
  143. min(replace(if(left (size ,2)='0,',substring(size ,3),size) ,',0','')) media_size ,
  144. min(replace(format ,' ,','')) media_format,
  145. min(video_length) video_length,
  146. min(video_bit_rate) video_bit_rate,
  147. type channel,
  148. if(is_video=1,2,1) type,
  149. if(locate(',',signature)>0,0,1) single_img
  150. from dw_image_cost_day where signature is not null and signature!=''
  151. GROUP BY signature,title,description,type,is_video) as foo
  152. group by signature ,title ,article
  153. """
  154. # df = db.dm.getData_pd(sql)
  155. df = db.dm.pd_data_sql(sql)
  156. # 进行数据转换-----添加max_media_size
  157. for i in range(len(df['media_size'])):
  158. if not df['media_size'][i]:
  159. continue
  160. size_list = df['media_size'][i].split(',')
  161. max_size = 0
  162. for size_data in size_list:
  163. if size_data != 'None':
  164. if float(size_data) > float(max_size):
  165. max_size = str(size_data)
  166. df['max_media_size'][i] = max_size
  167. key = ["signature", 'title', 'article']
  168. tag = ['media_size', 'media_format', 'video_length', 'video_bit_rate', 'max_media_size',
  169. 'use_times', "view_times",
  170. "click_times", "novels", "start_date", "end_date", "type", "channel",
  171. 'consume_amount', 'single_img', 'media', 'width', 'height', 'aspect_ratio']
  172. table = "t_ads_idea"
  173. db.zx_ads.dfsave2mysql(df, table, key, tag)
  174. def run():
  175. title()
  176. description()
  177. image()
  178. adcreative()
  179. if __name__ == '__main__':
  180. run()
  181. # title()
  182. # description()
  183. # image()
  184. # adcreative()