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, click
from auto_request import host, user, db, passwd


def get_star_user_id(numtime1, 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 (content_level =4 or content_level =5)  and create_time  > '%s' and create_time < '%s' " % (
            numtime1, numtime2))
        data = cursor.fetchall()
        user_id_list = list(data)
        logging.info("Database version : %s " % user_id_list)
        topic_id_list = []
        if user_id_list:
            for i in user_id_list:
                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
    except:
        logging.error("catch exception,get_data:%s" % traceback.format_exc())
        return None


if __name__ == "__main__":

    try:
        time_list = [1, 2, 3, 4, 5, 6, 7]

        for i in time_list:

            numtime1, numtime2 = time_convs(i + 1, i - 1)

            user_topic_id = get_star_user_id(numtime1,numtime2)

            try:

                for j in user_topic_id and user_topic_id:

                    id = int(j[1])

                    urge_num = random.randint(0, 1)

                    for i in range(urge_num):

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

                        cookies = login()

                        if cookies is not None:
                            click(cookies, id)
            except:
                pass

            user_id = get_star_user_id(numtime1)

            try:

                for j in user_id and user_id:

                    id = int(j[0])

                    for i in range(1):

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

                        cookies = login()

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

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