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


def get_data(numtime1, numtime2):
    try:
        pc = pymysql.connect(host=host, user=user, passwd=passwd, db=db, port=3306)
        cursor = pc.cursor()
        cursor.execute(
            "select distinct(user_id) from topic where is_online=1 and create_time > '%s' and create_time < '%s'  and is_shadow = 0" % (
                numtime1, numtime2))
        data = cursor.fetchall()
        user_id = list(data)
        logging.info("Database version : %s " % user_id)
        pc.close()
        return user_id
    except:
        logging.error("catch exception,get_data:%s" % traceback.format_exc())
        return None


if __name__ == "__main__":
    try:
        numtime1, numtime2 = time_now(10)

        user_id = get_data(numtime1, numtime2)

        for i in user_id and user_id:

            cookies = get_cookies(i)

            cook = login()

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