vest_click_reply.py 2.73 KB
import requests
import pymysql
import random
import traceback
import logging
import json
import datetime
from libs.cache import redis_client
from vest.request.auto_request import get_offline_comment
from vest.request.auto_request import host, user, db, passwd
from libs.error import logging_exception

auto_reply_url = "http://saturn.iyanzhi.com/api/v1/reply/create_for_inner"


def reply(id, content, user_id):
    try:
        post_dict = {
            'user_id': user_id,
            'topic_id': id,
            'content': content,
            "type": 4
        }
        response = requests.post(url=auto_reply_url,
                                 data=post_dict)

        logging.info("response.text:%s" % response.text)

    except:

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


def get_data(now, noww):
    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 ( create_time > '%s' and  create_time <= '%s' )  and is_online = 1  and  content_level in (4,5,6) " % (
                now, noww))
        data = cursor.fetchall()
        topic_id = list(data)
        topic_id_list = []
        for id in topic_id:
            cursor.execute(
                "select user_id,is_shadow from user_extra where user_id =" + str(
                    id[1]) + " and is_online =1 and  is_deleted =0")
            data = cursor.fetchall()
            user_id = list(data)
            if user_id and user_id[0][1] == 1:
                topic_id_list.append(id)
        pc.close()
        logging.info("get  majia  topic_id_list:%s" % topic_id_list)
        return topic_id_list
    except:
        logging.error("catch exception,get_data:%s" % traceback.format_exc())
        return None


def vest_click_reply():
    try:
        logging.info("get majia data11111111111")
        now = datetime.datetime.now()
        yes_time = now - datetime.timedelta(minutes=5)
        data = json.loads(redis_client.get("get_user_id_data"))
        user_list = []
        for i in data:
            user_list.append(i[0])
        topic_id = get_data(yes_time, now)
        if topic_id:
            for id in topic_id:
                rand_num = random.randint(0, 4)
                for i in range(rand_num):
                    num = random.randint(0, len(user_list) - 1)
                    user_id = user_list[num]
                    if user_id != id[1]:
                        comment_list = get_offline_comment()
                        comment = comment_list[i]
                        reply(id[0], comment, user_id)
    except:
        logging_exception()
        logging.error("catch exception,main :%s" % traceback.format_exc())