auto_request.py 18.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
import requests
import time
import datetime
import random
import traceback
import logging
import json
import redis
import smtplib
from celery import shared_task
from libs.cache import redis_client
from email.mime.text import MIMEText
from email.utils import formataddr
from physical.settings_local import DATABASES
from physical.settings_local import REDIS_URL
from vest.data.topic_models import get_pictorial_tag_by_id, get_topic_product_info, get_edit_tag_id_list, \
    get_category_tag_id, topic_has_image, get_tag_id_list
from django.conf import settings

my_sender = 'lixiaofang@igengmei.com'
my_pass = 'tg5AVKBB8jLQGBET'
my_user6 = "lixiaofang@igengmei.com"

auto_vote_url = settings.AUTO_VOTE_URL
auto_click_url = settings.AUTO_CLICK_URL
auto_reply_url = settings.AUTO_REPLY_URL
auto_follow_url = settings.AUTO_FOLLOW_URL
auto_urge_url = settings.AUTO_URGE_URL

host = DATABASES['default']['HOST']

user = DATABASES['default']['USER']

port = DATABASES['default']['PORT']

db = DATABASES['default']['NAME']

passwd = DATABASES['default']['PASSWORD']


def get_majia_reply(user_id):
    try:
        data = open("/srv/apps/physical/vest/data/vest_user_email.txt")
        list = []
        for i in data:
            if i != user_id:
                list.append(i.strip('\n').strip(','))

        maj = random.randint(1, len(list))
        user_id = list[maj - 1]
        return user_id

    except:
        logging.error("catch exception,get_majia:%s" % traceback.format_exc())
        return None


def get_majia():
    try:
        data = open("/srv/apps/physical/vest/data/vest_user_email.txt")
        list = []
        for i in data:
            list.append(i.strip('\n').strip(','))
        maj = random.randint(1, len(list))
        user_id = list[maj - 1]
        return user_id

    except:
        logging.error("catch exception,get_majia:%s" % traceback.format_exc())
        return None


def get_cookies(user_id):
    try:
        session_key = "majia_login_session"
        data = redis_client.hget(session_key, user_id)
        logging.info("get data:%s" % data)
        logging.info("get data:%s" % json.loads(data))

        return json.loads(data)

    except:
        logging.error("catch exception,get_majia:%s" % traceback.format_exc())
        return None


def login(user_id=None):
    try:
        if not user_id:
            user_id = get_majia()
        logging.info("get user_id:%s" % user_id)

        cookies = get_cookies(user_id)
        if cookies is not None:
            return {'sessionid': cookies}

    except:

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

        return None


def logins(user_id):
    try:
        cookies = get_cookies(user_id)
        if cookies is not None:
            return {'sessionid': cookies}

    except:

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

        return None


@shared_task
def click(cookies_get, id):
    # 点赞
    try:
        topic_id = id[0]
        pictorial_id = id[1]

        if pictorial_id:
            post_dict = {
                "topic_id": topic_id,
                "pictorial_id": pictorial_id
            }
            response = requests.post(url=auto_vote_url,
                                     cookies=cookies_get,
                                     data=post_dict)

        else:
            post_dict = {
                'type': 0,
                'id': topic_id
            }
            response = requests.post(url=auto_click_url,
                                     cookies=cookies_get,
                                     data=post_dict)

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

        get_error(response.text, "click", id)
    except:

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


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

        print(response.text)
        logging.info("response.text:%s" % response.text)
        get_error(response.text, "reply", id)
    except:

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


def time_conv(minutest, minutest2):
    try:
        now = datetime.datetime.now()
        yes_time = now - datetime.timedelta(minutes=minutest)
        yes_time2 = now - datetime.timedelta(minutes=minutest2)
        return yes_time, yes_time2
    except:
        return None


def time_conv_minute(minutest, minutest2):
    try:
        now = datetime.datetime.now()
        minute = datetime.datetime.now().minute
        yes_time = now - datetime.timedelta(minutes=minutest)
        yes_time2 = now - datetime.timedelta(minutes=minutest2)
        return yes_time, yes_time2, minute
    except:
        return None


def time_conv_hour(minutest, minutest2):
    try:
        now = datetime.datetime.now()
        yes_time = now - datetime.timedelta(hours=minutest)
        yes_time2 = now - datetime.timedelta(hours=minutest2)
        return yes_time, yes_time2
    except:
        return None


def time_now(minutest):
    try:
        now = datetime.datetime.now()
        yes_time = now - datetime.timedelta(hours=minutest)
        return yes_time, now
    except:
        return None


def time_convs(numtime, numtime2):
    '''
    params: 1, 2
    datetime.now(): 2019-10-09 10:35:50.231463
    return: 2019-10-08 23:59:59.000000 2019-10-07 00:00:00.000000
    '''
    try:
        now = datetime.datetime.now()
        yes_time = now - datetime.timedelta(days=numtime)
        yes_time_str = yes_time.strftime('%Y-%m-%d')
        yes_time_str = yes_time_str + ' 23:59:59.000000'

        yes_time2 = now - datetime.timedelta(days=numtime2)
        yes_time_str2 = yes_time2.strftime('%Y-%m-%d')
        yes_time_str2 = yes_time_str2 + ' 00:00:00.000000'

        logging.info("get yes_time_str:%s" % yes_time_str)
        logging.info("get yes_time_str2:%s" % yes_time_str2)
        return yes_time_str, yes_time_str2
    except:
        return None


def get_comment():
    try:
        data = open("/srv/apps/physical/vest/data/guanshui.txt", "r")
        list_guanshui = []
        for i in data:
            list_guanshui.append(i)

        num = random.randint(0, len(list_guanshui))

        comment = list_guanshui[num - 1]
        return comment

    except:

        return None


def get_comments():
    try:
        data = open("/srv/apps/physical/vest/data/guanshui.txt", "r")
        list_guanshui = []
        for i in data:
            list_guanshui.append(i)

        num1 = random.randint(0, 200)
        num2 = random.randint(200, 400)
        num3 = random.randint(400, len(list_guanshui))

        comment1 = list_guanshui[num1 - 1]
        comment2 = list_guanshui[num2 - 1]
        comment3 = list_guanshui[num3 - 1]

        comment_list = [comment1, comment2, comment3]

        return comment_list

    except:

        return None


@shared_task
def follow(cookies_get, id):
    try:
        post_dict = {
            'type': 1,
            'id': id
        }
        response = requests.post(url=auto_follow_url,
                                 cookies=cookies_get,
                                 data=post_dict)

        print(response.text)
        logging.info("response.text:%s" % response.text)
        get_error(response.text, "follow", id)
    except:

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


def Urge(cookies_get, id):
    try:
        post_dict = {
            'id': id
        }
        response = requests.post(url=auto_urge_url,
                                 cookies=cookies_get,
                                 data=post_dict)

        logging.info("response.text:%s" % response.text)
        get_error(response.text, "Urge", id)
    except:

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


def get_offline_comment():
    try:
        data = open("/srv/apps/physical/vest/data/offline_comment.txt", "r")

        list_guanshui = []
        for i in data:
            list_guanshui.append(i)
        num1 = random.randint(0, 200)
        num2 = random.randint(200, 300)
        num3 = random.randint(300, 400)
        num4 = random.randint(400, len(list_guanshui))

        comment1 = list_guanshui[num1 - 1]
        comment2 = list_guanshui[num2 - 1]
        comment3 = list_guanshui[num3 - 1]
        comment4 = list_guanshui[num4 - 1]

        comment_list = [comment1, comment2, comment3, comment4]
        return comment_list

    except:

        return None


def get_user_id():
    user_data = open("/srv/apps/physical/vest/data/get_user_id.txt", "r")
    user_list = []

    for i in user_data.readlines():
        user_list.append(int(i.strip()))

    return user_list


def send_email(stat_data):
    ret = True
    now = datetime.datetime.now()
    yes_time = now - datetime.timedelta(days=1)
    yes_time_str = yes_time.strftime('%Y%m%d')
    try:
        msg = MIMEText(stat_data, 'plain', 'utf-8')
        msg['From'] = formataddr(["李小芳", my_sender])
        msg["To"] = formataddr(["李小芳", my_user6])
        msg['Subject'] = str(datetime.date.today()) + "like的" + str(settings.SERVICE_NAME) + "环境的自动点赞出问题了"
        server = smtplib.SMTP_SSL("smtp.exmail.qq.com", 465)
        server.login(my_sender, my_pass)
        server.sendmail(my_sender, [my_user6], msg.as_string())
        server.quit()
        return ret
    except Exception:
        ret = False
        logging.error("catch exception,main:%s" % traceback.format_exc())


def get_error(line, type, id):
    try:
        if len(line) > 1:
            data_dict = json.loads(line)
            if data_dict["error"] != 0:
                stat_data = str(line) + str(type) + str(id)
                ret = send_email(stat_data)
                if ret:
                    logging.info('like query统计邮件发送成功')
                else:
                    logging.info('like query统计邮件发送失败')
            else:
                pass
    except:
        logging.error("catch exception,err_msg:%s" % traceback.format_exc())


def get_answer_data():
    try:

        # 获取第一条评论
        data = open("/srv/apps/physical/vest/data/reply_answer_data1.txt", "r")

        answer_data1 = []
        for i in data:
            answer_data1.append(i)
        num1 = random.randint(0, len(answer_data1))

        comment1 = answer_data1[num1 - 1]

        # 获取第二条评论
        data = open("/srv/apps/physical/vest/data/reply_answer_data2.txt", "r")

        answer_data2 = []
        for i in data:
            answer_data2.append(i)
        comment2 = answer_data2[num1 - 1]

        logging.info("get comment1:%s" % comment1)
        logging.info("get comment2:%s" % comment2)

        return comment1, comment2

    except:

        return ["", ""]


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

        logging.info("response.text:%s" % response.text)
        get_error(response.text, "reply", id)

        return response.text
    except:

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


def set_reply_to_redis():
    try:
        redis_client = redis.StrictRedis.from_url(REDIS_URL)
        return redis_client
    except:
        logging.error("catch exception,logins:%s" % traceback.format_exc())


@shared_task
def reply2(cookies_get, id, content, replied_id):
    try:
        post_dict = {
            'topic_id': id,
            'content': content,
            "type": 4,
            "replied_id": replied_id
        }
        response = requests.post(url=auto_reply_url,
                                 cookies=cookies_get,
                                 data=post_dict)

        print(response.text)
        logging.info("response.text:%s" % response.text)
        get_error(response.text, "reply2", id)
    except:

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


@shared_task
def pictorial_reply(cookies_get, id, content):
    try:
        post_dict = {
            'pictorial_id': id,
            'content': content,
            "type": 4
        }
        response = requests.post(url=auto_reply_url,
                                 cookies=cookies_get,
                                 data=post_dict)

        print(response.text)
        logging.info("response.text:%s" % response.text)
        get_error(response.text, "pictorial_reply", id)
    except:
        logging.error("catch exception,logins:%s" % traceback.format_exc())


def get_pictorial_comment():
    try:
        data = open("/srv/apps/physical/vest/data/pictorial_reply_data.txt", "r")
        list_guanshui = []
        for i in data:
            list_guanshui.append(i)

        num = random.randint(0, len(list_guanshui))

        comment = list_guanshui[num - 1]

        logging.info("get comment:%s" % comment)

        return comment

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


def get_product_comment():
    try:
        data = open("/srv/apps/physical/vest/data/product_reply_data.txt", "r")
        list_guanshui = []
        for i in data:
            list_guanshui.append(i)

        num = random.randint(0, len(list_guanshui))

        comment = list_guanshui[num - 1]

        return comment

    except:

        return None


def get_face_comment():
    try:
        data = open("/srv/apps/physical/vest/data/face_reply_data.txt", "r")
        list_guanshui = []
        for i in data:
            list_guanshui.append(i)

        num = random.randint(0, len(list_guanshui))

        comment = list_guanshui[num - 1]

        return comment

    except:

        return None


def get_hellolike_comment():
    try:
        data = open("/srv/apps/physical/vest/data/hellolike.txt", "r")
        list_guanshui = []
        for i in data:
            list_guanshui.append(i)

        num = random.randint(0, len(list_guanshui))

        comment = list_guanshui[num - 1]

        return comment

    except:

        return None


def get_category_reply_commtent(category_id):
    try:
        # 发型42、穿搭46、美妆3、护肤4、美甲21972、香水17576、瘦身32605、配饰5305608
        file = ""
        if category_id == 3:  # 美妆
            logging.info("3-------------")
            file = "/srv/apps/physical/vest/data/beauty_reply_data.txt"
        elif category_id == 42:  # 发型
            logging.info("42-------------")
            file = "/srv/apps/physical/vest/data/hairstyle_reply_data.txt"
        elif category_id == 4:  # 护肤
            logging.info("4-------------")
            file = "/srv/apps/physical/vest/data/skincare_reply_data.txt"
        elif category_id == 46:  # 穿搭
            logging.info("46-------------")
            file = "/srv/apps/physical/vest/data/wear_repy_data.txt"
        else:
            comment = get_comment()
            return comment
        data = open(file, "r")
        list_guanshui = []
        for i in data:
            list_guanshui.append(i)
        num = random.randint(0, len(list_guanshui))
        comment = list_guanshui[num - 1]
        logging.info("get comment:%s" % comment)
        return comment
    except:
        logging.error("catch exception,logins:%s" % traceback.format_exc())
        return None


def judge_topic_info_get_comment(topic_id):
    try:
        comment = ""
        # 先判断是不是编辑标签
        edit_tag_list = get_edit_tag_id_list(topic_id)
        logging.info("get topic_tag:%s" % edit_tag_list)
        tag_list = get_tag_id_list(topic_id)
        if len(tag_list) > 0:
            comment = get_hellolike_comment()

        elif len(edit_tag_list) > 0:
            # 如果有编辑标签
            # 去拿他的所属分类  根据分类去拿对应的标签(随机拿一个)
            category_tag_id = get_category_tag_id(edit_tag_list)
            if category_tag_id > 0:
                comment = get_category_reply_commtent(category_tag_id)
            else:
                product_judge = get_topic_product_info(topic_id)
                logging.info("get product_judge:%s" % product_judge)
                if product_judge:
                    comment = get_product_comment()
                else:
                    # 判断有没有人脸

                    face_judge = topic_has_image(topic_id)
                    logging.info("get face_judge:%s" % face_judge)

                    if face_judge:
                        comment = get_face_comment()
                    # 原始评论
                    else:
                        # 返回空就会去拿多级评论
                        if random.randint(0, 1):
                            comment = get_comment()

        else:
            # 判断有没有商品信息
            product_judge = get_topic_product_info(topic_id)
            if product_judge:
                comment = get_product_comment()
                logging.info("get product_judge:%s" % product_judge)

            else:
                # 判断有没有人脸
                face_judge = topic_has_image(topic_id)
                logging.info("get face_judge:%s" % face_judge)

                if face_judge:
                    comment = get_face_comment()
                # 原始评论
                else:
                    # 返回空就会去拿多级评论
                    if random.randint(0, 1):
                        comment = get_comment()

        logging.info("get judge_topic_info_get_comment:%s" % comment)
        return comment
    except:
        logging.error("catch exception,logins:%s" % traceback.format_exc())
        return None


def judge_pictorial_info_get_comment(pictorial_id):
    try:
        comment = ""
        # 先判断是不是编辑标签
        edit_tag_list = get_pictorial_tag_by_id(pictorial_id)
        if len(edit_tag_list) > 0:
            # 如果有编辑标签
            # 去拿他的所属分类  根据分类去拿对应的标签(随机拿一个)
            category_tag_id = get_category_tag_id(edit_tag_list)
            logging.info("get category_tag_id:%s" % category_tag_id)
            if category_tag_id > 0:
                comment = get_category_reply_commtent(category_tag_id)
            else:
                logging.info("get -------------+++++++++++")
                comment = get_pictorial_comment()
        else:
            # 原始评论
            comment = get_pictorial_comment()

        logging.info("get judge_topic_info_get_comment:%s" % comment)
        return comment
    except:

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