answer_reply5.py 2.23 KB
import pymysql
import traceback
import logging
import json
from vest.request.auto_request import logins, time_convs, get_answer_data, reply_answer, get_majia, \
    set_reply_to_redis
from vest.request.auto_request import host, user, db, passwd
from libs.error import logging_exception
import random


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_reply5():
    try:
        logging.info("comment  answer five")
        numtime1, numtime2 = time_convs(5, 5)
        user_id = get_data(numtime1, numtime2)
        redis_key = "cybertron:set_reply_id:five"
        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()
                    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"]
                            reply_data_comment.append(
                                {"id": reply_id, "reply_user_id": reply_user_id, "answer": comment2, "topic_id": i[1],
                                 "majia_user_id": majia_user_id})

        redis_client = set_reply_to_redis()
        redis_client.set(redis_key, json.dumps(reply_data_comment))

    except:
        logging_exception()
        logging.error("catch exception,main:%s" % traceback.format_exc())