MaterialDataClean.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. # print(df)
  38. df["data_type"] = 'all'
  39. df['type'] = '2'
  40. df['create_by'] = '0'
  41. key = ["content", "type"]
  42. tag = ['use_times', "view_times", "click_times", "novels", "start_date", "end_date", "create_by", 'data_type',
  43. 'consume_amount']
  44. table = "t_ads_content"
  45. db.zx_ads.dfsave2mysql(df, table, key, tag)
  46. def image():
  47. sql = """select signature,sum(consume_amount) consume_amount,
  48. sum(click_times) click_times,
  49. sum(view_times) view_times,
  50. sum(use_times) use_times,
  51. group_concat(distinct novels) novels ,
  52. max(end_date) end_date,
  53. min(start_date) start_date,
  54. min(content) content,
  55. min(type) type,
  56. if(locate(',',signature)>0,0,1) single_img,
  57. min(width ) width ,
  58. min(height ) height ,
  59. min(media_size) media_size ,
  60. min(media_format) media_format,
  61. min(video_length) video_length,
  62. min(video_bit_rate) video_bit_rate,
  63. 0 max_media_size
  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(replace(if(left (width ,2)='0,',substring(width ,3),width) ,',0','')) width ,
  76. min(replace(if(left (height ,2)='0,',substring(height ,3),height) ,',0','')) 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) > 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', 'use_times', "view_times", "click_times", "novels", "start_date", "end_date", "create_by", "single_img",
  105. "content", 'consume_amount', 'type', 'width', 'height']
  106. table = "t_ads_media"
  107. db.zx_ads.dfsave2mysql(df, table, key, tag)
  108. def adcreative():
  109. sql = """select signature,title,article,
  110. sum(click_times) click_times,
  111. sum(view_times) view_times,
  112. sum(use_times) use_times,
  113. sum(consume_amount) consume_amount,
  114. group_concat(distinct novels) novels,
  115. min(start_date) start_date,
  116. max(end_date) end_date,
  117. min(media) media,
  118. min(channel) channel ,
  119. min(type) type,
  120. if(locate(',',signature)>0,0,1) single_img,
  121. min(width) width ,
  122. min(height) height,
  123. min(media_size) media_size ,
  124. min(media_format) media_format,
  125. min(video_length) video_length,
  126. min(video_bit_rate) video_bit_rate,
  127. 0 max_media_size
  128. from
  129. (select replace(signature ,' ,','') as signature,title,description article,
  130. sum(click_count) click_times,
  131. sum(view_count) view_times,
  132. sum(use_times) use_times,
  133. sum(cost) consume_amount,
  134. group_concat(distinct book) novels,
  135. min(dt) start_date,max(dt) end_date,
  136. min(replace(preview_url ,' ,','')) media,
  137. min(replace(if(left (width ,2)='0,',substring(width ,3),width) ,',0','')) width ,
  138. min(replace(if(left (height ,2)='0,',substring(height ,3),height) ,',0','')) height ,
  139. min(replace(if(left (size ,2)='0,',substring(size ,3),size) ,',0','')) media_size ,
  140. min(replace(format ,' ,','')) media_format,
  141. min(video_length) video_length,
  142. min(video_bit_rate) video_bit_rate,
  143. type channel,
  144. if(is_video=1,2,1) type,
  145. if(locate(',',signature)>0,0,1) single_img
  146. from dw_image_cost_day where signature is not null and signature!=''
  147. GROUP BY signature,title,description,type,is_video) as foo
  148. group by signature ,title,article """
  149. # df = db.dm.getData_pd(sql)
  150. df = db.dm.pd_data_sql(sql)
  151. # 进行数据转换-----添加max_media_size
  152. for i in range(len(df['media_size'])):
  153. if not df['media_size'][i]:
  154. continue
  155. size_list = df['media_size'][i].split(',')
  156. max_size = 0
  157. for size_data in size_list:
  158. if size_data != 'None':
  159. if float(size_data) > max_size:
  160. max_size = str(size_data)
  161. df['max_media_size'][i] = max_size
  162. key = ["signature", 'title', 'article']
  163. tag = ['media_size', 'media_format', 'video_length', 'video_bit_rate', 'max_media_size', 'use_times', "view_times",
  164. "click_times", "novels", "start_date", "end_date", "type", "channel",
  165. 'consume_amount', 'single_img', 'media', 'width', 'height']
  166. table = "t_ads_idea"
  167. db.zx_ads.dfsave2mysql(df, table, key, tag)
  168. def run():
  169. title()
  170. description()
  171. image()
  172. adcreative()
  173. if __name__ == '__main__':
  174. run()
  175. # title()
  176. # description()
  177. # image()
  178. # adcreative()