import MySQLdb import time import os
def comment_update(commentid): try: conn=MySQLdb.connect(host='xxx.xxx.xxx.xxx',user='xx',passwd='xxxxx',db='xxxx',port=xxxx,charset='utf8') cur=conn.cursor() if cur.execute('update comment set content = "xxxxx" where id = ' + commentid) == 1: conn.commit() cur.close() conn.close() return 0 else: return 1 except MySQLdb.Error,e: return 1 if __name__ == '__main__':
while 1: if os.path.exists('CommentID.txt'): time.sleep(5) os.rename('CommentID.txt','CommentID_process.txt') else: for i in range(0,30): print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) + " Countdown " + str(30-i) time.sleep(1) continue
if os.path.exists('CommentResult.txt'): os.remove('CommentResult.txt') commentid_object = open('CommentID_process.txt', 'r') commentresult_object=open('CommentResult.txt','w+') try: for line in commentid_object: if comment_update(line) == 0: print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) +" "+ line.replace("\n","")+" processed successfully" commentresult_object.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) +" "+ line.replace("\n","")+" processed successfully \n") else: print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) +" "+ line.replace("\n","")+" processed failed" commentresult_object.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) +" "+ line.replace("\n","")+" processed failed \n") finally: commentid_object.close() commentresult_object.close()
os.rename('CommentID_process.txt','CommentID_'+time.strftime('%Y%m%d%H%M%S',time.localtime(time.time()))+'.txt')
|