import pymysql import traceback import logging import json import random from vest.request.auto_request import time_conv_minute, get_answer_data, reply_answer, set_reply_to_redis, \ reply2, logins, time_convs, get_majia, get_majia_reply from vest.request.auto_request import host, user, db, passwd from libs.error import logging_exception def get_data(numtime, numtime2): try: pc = pymysql.connect(host=host, user=user, passwd=passwd, db=db, port=3306) cursor = pc.cursor() cursor.execute( "SELECT user_id,id FROM topic WHERE is_online=1 and (create_time > '%s' and create_time < '%s' )" % ( numtime2, numtime)) data = cursor.fetchall() topic_id_list = list(data) return topic_id_list except: logging.error("catch exception,get_data:%s" % traceback.format_exc()) return [] # def answer_reply1(): # try: # logging.info("answer_reply_one") # numtime1, numtime2, minute = time_conv_minute(5, 0) # user_id = get_data(numtime1, numtime2) # redis_key1 = "cybertron:set_reply_id:one" # reply_data_comment = [] # if user_id: # for i in user_id: # majia_user_id = get_majia() # logging.info("get majia_user-id:%s" % majia_user_id) # cook = logins(majia_user_id) # comment1, comment2 = get_answer_data() # if cook is not None: # response = reply_answer(cook, i[1], comment1) # if len(response) > 1: # data_dict = json.loads(response) # reply_id = data_dict["data"]["id"] # reply_user_id = data_dict["data"]["user"]["id"] # topic_id = i[1] # reply_data_comment.append( # {"id": reply_id, "reply_user_id": reply_user_id, "answer": comment2, "topic_id": topic_id, # "majia_user_id": majia_user_id}) # # redis_client = set_reply_to_redis() # redis_client.hset(redis_key1, str(minute), json.dumps(reply_data_comment)) # logging.info("get eeeeeeee:%s" % str(minute - 5)) # have_reply1 = redis_client.hget(redis_key1, str(minute - 5)) # logging.info("get have_reply1:%s" % have_reply1) # if have_reply1: # result = json.loads(str(have_reply1, encoding="utf-8")) # redis_client.hdel(redis_key1, str(minute - 5)) # if result: # for item in result: # majia_user_id = get_majia_reply(item["majia_user_id"]) # cook = logins(majia_user_id) # reply2(cook, item["topic_id"], item["answer"], item["id"]) # # except: # logging_exception() # logging.error("catch exception,main:%s" % traceback.format_exc()) def answer_reply1(): try: logging.info("answer_reply_one") numtime1, numtime2 = time_convs(1, 1) user_id = get_data(numtime1, numtime2) redis_key1 = "cybertron:set_reply_id:one" reply_data_comment = [] if user_id: for i in user_id: random_num = random.randint(0, 2) logging.info("get random_num:%s" % random_num) for num in range(random_num): majia_user_id = get_majia_reply(i[0]) cook = logins(majia_user_id) comment1, comment2 = get_answer_data() if cook is not None: response = reply_answer(cook, i[1], comment1) if len(response) > 1: data_dict = json.loads(response) reply_id = data_dict["data"]["id"] reply_user_id = data_dict["data"]["user"]["id"] topic_id = i[1] reply_data_comment.append( {"id": reply_id, "reply_user_id": reply_user_id, "answer": comment2, "topic_id": topic_id, "majia_user_id": majia_user_id}) redis_client = set_reply_to_redis() redis_client.set(redis_key1, json.dumps(reply_data_comment)) except: logging_exception() logging.error("catch exception,main:%s" % traceback.format_exc())