import requests import time import datetime import pymysql import random import traceback import logging from log_settings import * from auto_request import login, time_convs, reply, get_comment from auto_request import host, user, db, passwd def get_topic_id(numtime): try: pc = pymysql.connect(host=host, user=user, passwd=passwd, db=db, port=3306) cursor = pc.cursor() cursor.execute( "SELECT id,user_id FROM topic WHERE is_online=1 and create_time LIKE '%%%%%s%%%%' " % numtime ) data = cursor.fetchall() topic_id = list(data) logging.info("Database version : %s " % topic_id) topic_id_list = [] for i in topic_id: cursor.execute( "select user_id from user_extra where user_id =" + str(i[1]) + " 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] for i in time_list: numtime = time_convs(i) topic_id = get_topic_id(numtime) dicts = {} for i in topic_id: if i[1] not in dicts.keys(): dicts.setdefault(i[1], []) dicts[i[1]].append(i[0]) else: dicts[i[1]].append(i[0]) logging.info("get dicts:%s" % dicts) for key, value in dicts.items(): rand_num = random.randint(0, len(value) - 1) rand_id = value[rand_num] comment = get_comment() logging.info("get comment:%s" % comment) for i in range(1): time.sleep(random.randint(10, 20)) cook = login() print(cook) if cook is not None: reply(cook, key, comment) time.sleep(300) except: logging.error("catch exception,main :%s" % traceback.format_exc())