import requests
import time
import datetime
import pymysql
import random
import traceback
from log_settings import *
import logging
from auto_request import login, time_convs, follow
from auto_request import host, user, port, db, passwd


def get_commnet_id(numtime):
    pc = pymysql.connect(host=host, user=user, passwd=passwd, db=db, port=3306)
    cursor = pc.cursor()

    cursor.execute(
        "SELECT  user_id FROM reply WHERE  create_time LIKE '%%%%%s%%%%' " % numtime)

    data_comment = cursor.fetchall()

    cursor.execute(

        "SELECT  t.user_id  from topic t left join reply r on  r.topic_id = t.id WHERE  r.create_time LIKE '%%%%%s%%%%'" % numtime)

    data_reply_topic = cursor.fetchall()

    cursor.execute(

        "SELECT  r.user_id  from reply r left join reply p on  r.replied_id = p.replied_id  WHERE  p.create_time LIKE '%%%%%s%%%%'" % numtime)

    data_reply_reply = cursor.fetchall()

    cursor.execute(

        "SELECT  user_id FROM group_user_role  WHERE  create_time LIKE '%%%%%s%%%%' " % numtime)

    data_group_follow = cursor.fetchall()

    cursor.execute(

        "select  user_id from account_user  WHERE  create_time LIKE '%%%%%s%%%%'" % numtime)

    data_new_user = cursor.fetchall()

    data_id = list(data_comment)
    data1_id = list(data_reply_topic)
    data2_id = list(data_group_follow)
    data3_id = list(data_new_user)
    data_reply = list(data_reply_reply)

    topic_id_list = []
    for i in data_id:
        cursor.execute(
            "select user_id from user_extra where user_id =" + str(i[0]) + " and is_shadow =0 ")
        data = cursor.fetchall()
        user_id = list(data)
        if user_id:
            topic_id_list.append(i)

    for i in data1_id:
        cursor.execute(
            "select user_id from user_extra where user_id =" + str(i[0]) + " and is_shadow =0 ")
        data = cursor.fetchall()
        user_id = list(data)
        if user_id:
            topic_id_list.append(i)

    for i in data2_id:
        cursor.execute(
            "select user_id from user_extra where user_id =" + str(i[0]) + " and is_shadow =0 ")
        data = cursor.fetchall()
        user_id = list(data)
        if user_id:
            topic_id_list.append(i)

    for i in data3_id:
        cursor.execute(
            "select user_id from user_extra where user_id =" + str(i[0]) + " and is_shadow =0 ")
        data = cursor.fetchall()
        user_id = list(data)
        if user_id:
            topic_id_list.append(i)

    for i in data1_id:
        cursor.execute(
            "select user_id from user_extra where user_id =" + str(i[0]) + " and is_shadow =0 ")
        data = cursor.fetchall()
        user_id = list(data)
        if user_id:
            topic_id_list.append(i)

    for i in data_reply:
        cursor.execute(
            "select user_id from user_extra where user_id =" + str(i[0]) + " and is_shadow =0 ")
        data = cursor.fetchall()
        user_id = list(data)
        if user_id:
            topic_id_list.append(i)

    logging.info("Database version : %s " % topic_id_list)

    pc.close()

    return topic_id_list


if __name__ == "__main__":

    try:

        numtime = time_convs(1)

        user_id = get_commnet_id(numtime)

        for j in user_id:

            id = int(j[0])

            follow_num = random.randint(0, 1)

            for i in range(follow_num):

                time.sleep(random.randint(10, 50))

                cookies = login()

                if cookies is not None:
                    follow(cookies, id)

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