tmp_tv_category_stat.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. """电视台对应电视剧及类型关系数据
  4. 电视台播放的电视剧根据类型分别保存记录
  5. province_input: odl.ad_television odl.ad_tv_lib
  6. province_output: tmp.tv_category_stat
  7. area_input: odl.area_ad_television odl.ad_tv_lib
  8. area_output: tmp.area_tv_category_stat
  9. """
  10. import sys
  11. from fty_util.common import Mysql, Util
  12. reload(sys)
  13. sys.setdefaultencoding('utf8')
  14. class tv_category_stat():
  15. def province(self):
  16. conn = Mysql.createOfflineConn()
  17. sql = """
  18. truncate table tmp.tv_category_stat
  19. """
  20. Mysql.execute(sql, conn=conn)
  21. # 电视台播放电视剧分类数据
  22. station_dict = {}
  23. sql = """
  24. select oat.tv_id, oat.channel, oat.theater_attribute, oatl.categories from odl.ad_television oat
  25. left join odl.ad_tv_lib oatl on oat.tv_id = oatl.tv_id
  26. where oat.tv_id is not null and oat.theater_attribute != '' and oat.theater_attribute is not null
  27. group by tv_id, channel, theater_attribute
  28. """
  29. rows = Mysql.getAll(sql, conn=conn)
  30. sql = """
  31. insert into tmp.tv_category_stat (tv_id, category, channel, theater_attribute) values (%s, %s, %s, %s)
  32. """
  33. for row in rows:
  34. tv_id = row['tv_id']
  35. channel = row['channel']
  36. theater_attribute = row['theater_attribute']
  37. categories = row['categories']
  38. if categories is not None and len(categories) > 0:
  39. cate_list = categories.split(' ')
  40. data_list = []
  41. for cat in cate_list:
  42. data_list.append((tv_id, cat, channel, theater_attribute))
  43. Mysql.insertMany(sql, data_list, conn)
  44. Mysql.close(conn)
  45. def area(self):
  46. conn = Mysql.createOfflineConn()
  47. sql = """
  48. truncate table tmp.area_tv_category_stat
  49. """
  50. Mysql.execute(sql, conn=conn)
  51. # 电视台播放电视剧分类数据
  52. station_dict = {}
  53. sql = """
  54. select oat.tv_id, oat.channel, oat.theater_attribute, oatl.categories from odl.area_ad_television oat
  55. left join odl.ad_tv_lib oatl on oat.tv_id = oatl.tv_id
  56. where oat.tv_id is not null
  57. group by tv_id, channel, theater_attribute
  58. """
  59. rows = Mysql.getAll(sql, conn=conn)
  60. sql = """
  61. insert into tmp.area_tv_category_stat (tv_id, category, channel, theater_attribute) values (%s, %s, %s, %s)
  62. """
  63. for row in rows:
  64. tv_id = row['tv_id']
  65. channel = row['channel']
  66. theater_attribute = row['theater_attribute']
  67. categories = row['categories']
  68. if categories is not None and len(categories) > 0:
  69. cate_list = categories.split(' ')
  70. data_list = []
  71. for cat in cate_list:
  72. data_list.append((tv_id, cat, channel, theater_attribute))
  73. Mysql.insertMany(sql, data_list, conn)
  74. Mysql.close(conn)
  75. if __name__ == '__main__':
  76. if len(sys.argv) != 2:
  77. print '没有输入参数,退出'
  78. sys.exit(0)
  79. print 'method name is ' + sys.argv[1]
  80. obj = tv_category_stat()
  81. try:
  82. getattr(obj, sys.argv[1])()
  83. except Exception, e:
  84. print e