def fake_recommend(gen_data_count=1):
    """虚拟对主题顶帖.

        :parameter gen_data_count: 生成数据数量
    """

    for entity in FakeRecommend().generate(gen_data_count):
        print(entity)
        tid = entity["tid"]
        uid = entity["uid"]
        opinion = entity["opinion"]

        print(tid, uid, opinion)

        faker_recommend_info.info("=" * 80)
        faker_recommend_info.info("(%s)正在评帖(%s)" % (uid, tid))

        # 查询是否顶过帖
        recommend_entities = forum_session.query(ForumMemberRecommend).filter(
            ForumMemberRecommend.__tid == tid,
            ForumMemberRecommend.__recommenduid == uid).all()

        if recommend_entities:
            faker_recommend_info.info("返回:之前已评过该帖!")
            continue

        try:
            forum_member_recommend = ForumMemberRecommend(
                __tid=tid,
                __recommenduid=uid,
                __dateline=int(
                    time.time()))
            forum_thread = forum_session.query(ForumThread).filter(
                ForumThread.__tid == tid).first()
            forum_thread.__views += 1  # 查看次数
            forum_thread.__recommends += 1  # 推荐指数
            if opinion < 85:
                forum_thread.__recommend_add += 1  # 支持人数
            else:
                forum_thread.__recommend_sub += 1  # 反对人数

            forum_session.add(forum_member_recommend)
            forum_session.add(forum_thread)
            forum_session.commit()
        except Exception as ex:
            faker_recommend_info.exception(ex)
            faker_recommend_info.info("评帖失败: Error.")
            forum_session.rollback()
        else:
            faker_recommend_info.info("评帖成功: OK.")
        finally:
            forum_session.close()
Exemple #2
0
def get_thread_entities():
    """规避使用'autoload'缺陷而刻意从类独立出.
    """

    thread_entities = None
    try:
        thread_entities = forum_session.query(ForumThread).all()
    except Exception as ex:
        print(ex)
        traceback.print_exc()
    finally:
        forum_session.close()

    return thread_entities
def get_thread_entities():
    """规避使用'autoload'缺陷而刻意从类独立出.
    """

    thread_entities = None
    try:
        thread_entities = forum_session.query(ForumThread).all()
    except Exception as ex:
        print(ex)
        traceback.print_exc()
    finally:
        forum_session.close()

    return thread_entities
Exemple #4
0
def fake_post(gen_data_count=1):
    """虚拟对主题回帖.

        :parameter gen_data_count: 生成数据数量
    """

    for entity in FakePost().generate(gen_data_count):
        uid, tid, fid = entity["uid"], entity["tid"], entity["fid"]
        username, message = entity["username"], entity["message"]

        faker_post_info.info("=" * 80)
        faker_post_info.info("message = %s" % message)
        faker_post_info.info("(%s)正在回帖(%s)" % (username, tid))
        # print("uid = %s; tid = %s; fid = %s; username = %s;" % (
        # uid, tid, fid, username))

        pid = spread_post(uid, tid, fid, username, message)

        if pid:
            try:
                # 更新主题帖最后回帖信息
                forum_thread = forum_session.query(ForumThread).filter(
                    ForumThread.__tid == tid).first()
                forum_thread.__lastposter = username
                forum_thread.__lastpost = int(time.time())
                forum_session.add(forum_thread)
                forum_session.commit()

                post = Post(uid, tid, pid, fid)
                robot_session.add(post)
                robot_session.commit()
            except Exception as ex:
                robot_session.rollback()
                faker_post_info.exception(ex)
                faker_post_info.info("回帖成功但记录失败: OK.")
                time_now = datetime.datetime.now().strftime("%Y-%m-%d %X")
                faker_post_error.info(faker_post_only %
                                      (uid, tid, pid, time_now))
            else:
                faker_post_info.info("回帖成功: OK.")
            finally:
                robot_session.close()
        else:
            faker_post_info.info("回帖失败: Error.")
def fake_post(gen_data_count=1):
    """虚拟对主题回帖.

        :parameter gen_data_count: 生成数据数量
    """

    for entity in FakePost().generate(gen_data_count):
        uid, tid, fid = entity["uid"], entity["tid"], entity["fid"]
        username, message = entity["username"], entity["message"]

        faker_post_info.info("=" * 80)
        faker_post_info.info("message = %s" % message)
        faker_post_info.info("(%s)正在回帖(%s)" % (username, tid))
        # print("uid = %s; tid = %s; fid = %s; username = %s;" % (
        # uid, tid, fid, username))

        pid = spread_post(uid, tid, fid, username, message)

        if pid:
            try:
                # 更新主题帖最后回帖信息
                forum_thread = forum_session.query(ForumThread).filter(
                    ForumThread.__tid == tid).first()
                forum_thread.__lastposter = username
                forum_thread.__lastpost = int(time.time())
                forum_session.add(forum_thread)
                forum_session.commit()

                post = Post(uid, tid, pid, fid)
                robot_session.add(post)
                robot_session.commit()
            except Exception as ex:
                robot_session.rollback()
                faker_post_info.exception(ex)
                faker_post_info.info("回帖成功但记录失败: OK.")
                time_now = datetime.datetime.now().strftime("%Y-%m-%d %X")
                faker_post_error.info(
                    faker_post_only % (uid, tid, pid, time_now))
            else:
                faker_post_info.info("回帖成功: OK.")
            finally:
                robot_session.close()
        else:
            faker_post_info.info("回帖失败: Error.")
def fake_visitor(gen_data_count=1):
    """虚拟访客状态更新.

        :parameter gen_data_count: 生成数据数量
    """

    try:
        faker_user_status_info.info("=" * 80)

        # 随便从cache里找出uid
        user_ids = [member_status["uid"] for member_status in
                    FakeVisitor().generate(gen_data_count)]

        # 查出uid的会员状态更新数据
        member_status_entities = forum_session.query(
            CommonMemberStatus).filter(
            CommonMemberStatus.__uid.in_(user_ids)).all()

        if not member_status_entities:
            print("Info: No Data.")
            return

        member_status_list = []
        for member_status in member_status_entities:
            member_status.__lastvisit = int(time.time())
            member_status.__lastactivity = int(time.time())
            member_status_list.append(member_status)
            time.sleep(random.randint(5, 100))

        forum_session.add_all(member_status_list)
        forum_session.commit()
    except Exception as ex:
        faker_user_status_info.info(ex)
        traceback.print_exc()
        forum_session.rollback()
    else:
        faker_user_status_info.info(user_ids)
    finally:
        forum_session.close()
def scat_content_to_user():
    """分发自动发帖数据到部分用户.
    """

    print("=" * 80)
    print("Info: Work is now being prepared.")

    split_groups = 5
    username = '******'

    # uid在[56,200]之间.
    thread_range = (56, 200)

    # create_datetime在["2015-11-20 00:00:00","2015-11-21 23:00:00"]之间.
    datetime_range = ('2015-11-20 00:00:00', '2015-11-21 23:00:00')

    try:
        # 分发用户(荣堂)数据.
        thread_entities = forum_session.query(ForumThread).filter(
            ForumThread.__author == username).all()
        if not thread_entities:
            print("Info: No Data.")
            return

        # 查询出分发用户数据.
        author_entities = robot_session.query(Member).filter(
            Member.dz_uid.between(thread_range[0], thread_range[1])).all()
        author_list = [(author.dz_uid, author.username) for author in
                       author_entities]

        # 查询出自动发帖数据.
        thread_logs = robot_session.query(Thread.thread_id,
                                          Thread.post_id).filter(
            Thread.create_datetime.between(datetime_range[0],
                                           datetime_range[1])).all()

        post_ids = [thread_entity.post_id for thread_entity in thread_logs]
        thread_ids = [thread_entity.thread_id for thread_entity in thread_logs]

        unit_entities = []
        threads_total = len(thread_entities)
        thread_normal, thread_moved, thread_retain = 0, 0, 0
        print("Info: threads_total = %s." % threads_total)
        print("=" * 80)

        for index, thread_entity in enumerate(thread_entities):
            print("Info: %s %s%%." % (
                index, str(int(float(index) / float(threads_total) * 100))))

            # 只分发自动发帖数据, 跳过正常从网站发出数据.
            if thread_entity.__tid not in thread_ids:
                thread_normal += 1
                continue

            # 将用户(荣堂)数据分五份(保留一份给本人,四份分发).
            if index % split_groups == 0:
                thread_retain += 1
                continue

            # 更新主题用户信息
            author = random.choice(author_list)
            thread_entity.__author = author[1],
            thread_entity.__authorid = author[0],

            # 更新帖子用户信息
            post_entity = forum_session.query(ForumPost).filter(
                ForumPost.__tid == thread_entity.__tid).first()

            if not post_entity or post_entity.__pid not in post_ids:
                continue

            post_entity.__author = author[1],
            post_entity.__authorid = author[0],

            unit_entities.append(thread_entity)
            unit_entities.append(post_entity)

        thread_moved = len(unit_entities) / 2
        forum_session.add_all(unit_entities)
        forum_session.commit()
    except Exception as ex:
        print(ex)
        traceback.print_exc()
        forum_session.rollback()
    else:
        print("=" * 80)
        print("Info: About User(%s) In %s Info:" % (username, datetime_range))
        print("Info: 论坛正常帖数 %(d)s 分发出贴数 %(d)s 保留的帖数" % {"d": " " * 20})
        print("Info: thread_normal = %d thread_moved = %d thread_retain = %d."
              % (thread_normal, thread_moved, thread_retain))
        print("Info: Well Done.")
    finally:
        forum_session.close()
        print("All Work Have Finished.")
        print("=" * 80)
def scat_content_to_user():
    """分发自动发帖数据到部分用户.
    """

    print("=" * 80)
    print("Info: Work is now being prepared.")

    split_groups = 5
    username = '******'

    # uid在[56,200]之间.
    thread_range = (56, 200)

    # create_datetime在["2015-11-20 00:00:00","2015-11-21 23:00:00"]之间.
    datetime_range = ('2015-11-20 00:00:00', '2015-11-21 23:00:00')

    try:
        # 分发用户(荣堂)数据.
        thread_entities = forum_session.query(ForumThread).filter(
            ForumThread.__author == username).all()
        if not thread_entities:
            print("Info: No Data.")
            return

        # 查询出分发用户数据.
        author_entities = robot_session.query(Member).filter(
            Member.dz_uid.between(thread_range[0], thread_range[1])).all()
        author_list = [(author.dz_uid, author.username)
                       for author in author_entities]

        # 查询出自动发帖数据.
        thread_logs = robot_session.query(Thread.thread_id,
                                          Thread.post_id).filter(
                                              Thread.create_datetime.between(
                                                  datetime_range[0],
                                                  datetime_range[1])).all()

        post_ids = [thread_entity.post_id for thread_entity in thread_logs]
        thread_ids = [thread_entity.thread_id for thread_entity in thread_logs]

        unit_entities = []
        threads_total = len(thread_entities)
        thread_normal, thread_moved, thread_retain = 0, 0, 0
        print("Info: threads_total = %s." % threads_total)
        print("=" * 80)

        for index, thread_entity in enumerate(thread_entities):
            print("Info: %s %s%%." %
                  (index, str(int(float(index) / float(threads_total) * 100))))

            # 只分发自动发帖数据, 跳过正常从网站发出数据.
            if thread_entity.__tid not in thread_ids:
                thread_normal += 1
                continue

            # 将用户(荣堂)数据分五份(保留一份给本人,四份分发).
            if index % split_groups == 0:
                thread_retain += 1
                continue

            # 更新主题用户信息
            author = random.choice(author_list)
            thread_entity.__author = author[1],
            thread_entity.__authorid = author[0],

            # 更新帖子用户信息
            post_entity = forum_session.query(ForumPost).filter(
                ForumPost.__tid == thread_entity.__tid).first()

            if not post_entity or post_entity.__pid not in post_ids:
                continue

            post_entity.__author = author[1],
            post_entity.__authorid = author[0],

            unit_entities.append(thread_entity)
            unit_entities.append(post_entity)

        thread_moved = len(unit_entities) / 2
        forum_session.add_all(unit_entities)
        forum_session.commit()
    except Exception as ex:
        print(ex)
        traceback.print_exc()
        forum_session.rollback()
    else:
        print("=" * 80)
        print("Info: About User(%s) In %s Info:" % (username, datetime_range))
        print("Info: 论坛正常帖数 %(d)s 分发出贴数 %(d)s 保留的帖数" % {"d": " " * 20})
        print(
            "Info: thread_normal = %d thread_moved = %d thread_retain = %d." %
            (thread_normal, thread_moved, thread_retain))
        print("Info: Well Done.")
    finally:
        forum_session.close()
        print("All Work Have Finished.")
        print("=" * 80)