odl_near_realtime_calc.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. """odl.ad_tv_id_pre数据监听
  4. 每隔一段时间
  5. """
  6. from fty_util.common import Mysql
  7. import commands
  8. import time
  9. import sys
  10. reload(sys)
  11. sys.setdefaultencoding('utf8')
  12. c_list = [',', '、', ',', ';', ';', '/']
  13. def replace_other_character(field):
  14. if field is None:
  15. return ''
  16. if field == u'暂无信息':
  17. field = ''
  18. for c in c_list:
  19. field = field.replace(c, ' ')
  20. return field
  21. while True:
  22. conn = Mysql.createOfflineConn()
  23. print 'start heartbeat'
  24. # 情况odl.ad_tv_lib表数据
  25. sql = """
  26. select tv_id, is_run from yxb.ad_tv_id_pre where is_run = 0
  27. """
  28. rows = Mysql.getAll(sql, conn=conn)
  29. for row in rows:
  30. tv_id = row['tv_id']
  31. is_run = row['is_run']
  32. if is_run == 0:
  33. print tv_id
  34. sql = """
  35. update yxb.ad_tv_id_pre set is_run = 1 where tv_id = '%s'
  36. """
  37. sql = sql % (tv_id)
  38. Mysql.update(sql, conn=conn)
  39. sql = """
  40. select tv_name, director, scriptwritter, main_actors, types, concat(decade, first_type) as first_type, second_type, \
  41. description, pub_comp, pub_date, production, \
  42. cehua, jianzhi, chupin_comp, chupin_date, show_time, decade, first_type, categories from yxb.ad_tv_lib where id = '%s'
  43. """
  44. sql = sql % (tv_id)
  45. row = Mysql.getOne(sql, conn=conn)
  46. tv_name = row[0]
  47. director = replace_other_character(row[1])
  48. scriptwritter = replace_other_character(row[2])
  49. main_actors = replace_other_character(row[3])
  50. types = replace_other_character(row[4])
  51. first_type = replace_other_character(row[5])
  52. second_type = replace_other_character(row[6])
  53. description = row[7]
  54. pub_comp = replace_other_character(row[8])
  55. pub_date = row[9]
  56. production = replace_other_character(row[10])
  57. cehua = replace_other_character(row[11])
  58. jianzhi = replace_other_character(row[12])
  59. chupin_comp = replace_other_character(row[13])
  60. chupin_date = row[14]
  61. show_time = row[15]
  62. decade = replace_other_character(row[16])
  63. theme = replace_other_character(row[17])
  64. sql = """
  65. replace into odl.ad_tv_lib (tv_id, tv_name, director, scriptwriter, main_actors, types, first_type, second_type, description, \
  66. pub_comp, pub_date, filmer, scheming, producer, produce_comp, produce_date, show_time, is_use, decade, theme) \
  67. values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  68. """
  69. value = (tv_id, tv_name, director, scriptwritter, main_actors, types, first_type, second_type, description, pub_comp, pub_date, production, cehua, jianzhi, chupin_comp, chupin_date, show_time, '1', decade, theme)
  70. Mysql.execute(sql, param=value, conn=conn)
  71. #todo 调用预测脚本
  72. status, output = commands.getstatusoutput('sh bash_near_real_job.sh ' + str(tv_id))
  73. if status != 0:
  74. sql = """
  75. update yxb.ad_tv_id_pre set is_run = 0 where tv_id = '%s'
  76. """
  77. sql = sql % (tv_id)
  78. Mysql.update(sql, conn=conn)
  79. print 'near_real_job.sh执行失败'
  80. print output
  81. break
  82. else:
  83. sql = """
  84. delete from yxb.ad_tv_id_pre where tv_id = '%s' and is_run = 1
  85. """
  86. sql = sql % (tv_id)
  87. Mysql.execute(sql, conn=conn)
  88. Mysql.close(conn)
  89. time.sleep(60)