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


def get_data(numtime):
    try:
        pc = pymysql.connect(host=host, user=user, passwd=passwd, db=db, port=3306)
        cursor = pc.cursor()
        cursor.execute(
            "select distinct(user_id) from account_user_auth   where  user_id not in (select user_id from pv_maidian  where page_name ='home' and  partiton_date >= %s )",
            (numtime))
        data = cursor.fetchall()
        user_id = list(data)
        topic_id_list = []
        for i in user_id and user_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)

        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:
        numtime = time_conv(3)

        user_id = get_data(numtime)

        urge_num = random.randint(1, 2)

        for j in range(urge_num):

            try:

                for i in user_id and user_id:

                    cookies = get_cookies(i)

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

                    cook = login()

                    if cook is not None:
                        follow(cook, i)
            except:
                pass
    except:
        logging.error("catch exception,main:%s" % traceback.format_exc())