import pymysql
import traceback
import logging
from vest.request.auto_request import login, time_conv, get_comment, reply, get_product_comment, get_face_comment, \
    get_category_tag_id, get_category_reply_commtent,judge_topic_info_get_comment
from vest.request.auto_request import host, user, db, passwd
from libs.error import logging_exception
from vest.data.topic_models import get_edit_tag_id_list, get_pictorial_tag_by_id, get_topic_product_info, \
    topic_has_image
import random


def get_data(numtime, numtime2):
    try:
        print("....")
        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' )" % (
                numtime, numtime2))
        data = cursor.fetchall()
        topic_id = list(data)
        topic_id_list = []
        try:
            for i in topic_id and topic_id:
                cursor.execute(
                    "select user_id,is_shadow from user_extra where user_id =" + str(
                        i[0]) + " and is_online =1 and  is_deleted =0")
                data = cursor.fetchall()
                user_id = list(data)
                if (user_id and user_id[0][1] == 0) or len(user_id) == 0:
                    topic_id_list.append(i)
            logging.info("Database version : %s " % topic_id_list)
            pc.close()

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

        return topic_id_list

    except:
        logging.error("catch exception,get_data:%s" % traceback.format_exc())
        return []


def true_comment_one():
    try:
        logging.info("comment one")
        numtime1, numtime2 = time_conv(5, 0)
        user_id = get_data(numtime1, numtime2)
        if user_id:
            for i in user_id:
                cook = login()
                comment = judge_topic_info_get_comment(i[1])
                if cook is not None:
                    reply(cook, i[1], comment)
            else:
                logging.error("catch exception,main:%s" % traceback.format_exc())
    except:
        logging_exception()
        logging.error("catch exception,main:%s" % traceback.format_exc())
#
#
# def true_comment_one():
#     try:
#         logging.info("comment one")
#         numtime1, numtime2 = time_conv(5, 0)
#         user_id = get_data(numtime1, numtime2)
#         if user_id:
#             for i in user_id:
#
#
#                 cookie = login()
#                 if cookie is not None:
#                     reply(cookie, i[1], comment)
#             else:
#                 logging.error("catch exception,main:%s" % traceback.format_exc())
#     except:
#         logging_exception()
#         logging.error("catch exception,main:%s" % traceback.format_exc())