#!/usr/bin/env python #coding=utf-8 """odl.ad_tv_id_pre数据监听 每隔一段时间 """ from fty_util.common import Mysql import commands import time import sys reload(sys) sys.setdefaultencoding('utf8') c_list = [',', '、', ',', ';', ';', '/'] def replace_other_character(field): if field is None: return '' if field == u'暂无信息': field = '' for c in c_list: field = field.replace(c, ' ') return field while True: conn = Mysql.createOfflineConn() print 'start heartbeat' # 情况odl.ad_tv_lib表数据 sql = """ select tv_id, is_run from yxb.ad_tv_id_pre where is_run = 0 """ rows = Mysql.getAll(sql, conn=conn) for row in rows: tv_id = row['tv_id'] is_run = row['is_run'] if is_run == 0: print tv_id sql = """ update yxb.ad_tv_id_pre set is_run = 1 where tv_id = '%s' """ sql = sql % (tv_id) Mysql.update(sql, conn=conn) sql = """ select tv_name, director, scriptwritter, main_actors, types, concat(decade, first_type) as first_type, second_type, \ description, pub_comp, pub_date, production, \ cehua, jianzhi, chupin_comp, chupin_date, show_time, decade, first_type, categories from yxb.ad_tv_lib where id = '%s' """ sql = sql % (tv_id) row = Mysql.getOne(sql, conn=conn) tv_name = row[0] director = replace_other_character(row[1]) scriptwritter = replace_other_character(row[2]) main_actors = replace_other_character(row[3]) types = replace_other_character(row[4]) first_type = replace_other_character(row[5]) second_type = replace_other_character(row[6]) description = row[7] pub_comp = replace_other_character(row[8]) pub_date = row[9] production = replace_other_character(row[10]) cehua = replace_other_character(row[11]) jianzhi = replace_other_character(row[12]) chupin_comp = replace_other_character(row[13]) chupin_date = row[14] show_time = row[15] decade = replace_other_character(row[16]) theme = replace_other_character(row[17]) sql = """ replace into odl.ad_tv_lib (tv_id, tv_name, director, scriptwriter, main_actors, types, first_type, second_type, description, \ pub_comp, pub_date, filmer, scheming, producer, produce_comp, produce_date, show_time, is_use, decade, theme) \ values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """ 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) Mysql.execute(sql, param=value, conn=conn) #todo 调用预测脚本 status, output = commands.getstatusoutput('sh bash_near_real_job.sh ' + str(tv_id)) if status != 0: sql = """ update yxb.ad_tv_id_pre set is_run = 0 where tv_id = '%s' """ sql = sql % (tv_id) Mysql.update(sql, conn=conn) print 'near_real_job.sh执行失败' print output break else: sql = """ delete from yxb.ad_tv_id_pre where tv_id = '%s' and is_run = 1 """ sql = sql % (tv_id) Mysql.execute(sql, conn=conn) Mysql.close(conn) time.sleep(60)