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 fix_member_miss_status(): """修复自动注册的用户缺失状态数据. """ print("=" * 80) try: print("Info: Work is now being prepared.") # 自动注册用户. robot_member_entities = robot_session.query(Member).all() if not robot_member_entities: print("Info: No Data.") return gen_data_count = len(robot_member_entities) # 补充自注册数据. member_status_data = FakeMemberStatus().generate(gen_data_count) member_status_list = [entity for entity in member_status_data] print("Info: member_entities_total = %s." % gen_data_count) member_status_entities = [] for index, member_entity in enumerate(robot_member_entities): print( "Info: %s %s%%." % (index, str(int(float(index) / float(gen_data_count) * 100)))) status_data = member_status_list[index] member_status = CommonMemberStatus(__uid=member_entity.dz_uid, __regip=status_data['reg_ip'], __lastip=status_data['last_ip'], __lastvisit=int(time.time()), __lastactivity=int(time.time())) member_status_entities.append(member_status) forum_session.add_all(member_status_entities) forum_session.commit() except Exception as ex: print(ex) traceback.print_exc() forum_session.rollback() else: print("Info: Well Done.") finally: forum_session.close() print("All Work Have Finished.") print("=" * 80)
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 fake_member(gen_data_count=1): """创建虚拟账户. gen_data_count的取值建议不要大, 因为不希望在时间点上跳跃性增长. :parameter gen_data_count: 生成数据数量 """ member_status_data = FakeMemberStatus().generate(gen_data_count) member_status_list = [entity for entity in member_status_data] for index, entity in enumerate(FakeMember().generate(gen_data_count)): username = entity["username"].lower() length = random.randint(6, 20) random_string = ''.join( (entity["password"], str(entity["assist_number"]))) random_string = [random.choice(random_string) for _ in range(length)] password = ''.join(random_string) # 用户中心md5后的实际密码. salt = "".join( [random.choice(string.ascii_lowercase + string.digits) for _ in range(6)]) hash_password = Utils.dz_uc_md5(password, salt) # 会员表md5后的伪密码. fake_password = Utils.md5(str(random.randint(10 * 9, 10 ** 10 - 1))) faker_user_info.info("=" * 80) faker_user_info.info("正在注册账户:%s" % username) try: common_member = CommonMember(__groupid=10, __username=username, __password=fake_password, __email=entity["email"], __regdate=int(time.time())) forum_session.add(common_member) forum_session.flush() uid = common_member.__uid center_member = CenterMember(__salt=salt, __username=username, __password=hash_password, __email=entity["email"], __regdate=int(time.time()), __uid=uid) forum_session.add(center_member) status_data = member_status_list[index] member_status = CommonMemberStatus(__uid=uid, __regip=status_data['reg_ip'], __lastip=status_data['last_ip'], __lastvisit=int(time.time()), __lastactivity=int(time.time())) forum_session.add(member_status) forum_session.commit() member = Member(username, password, entity["email"], uid) robot_session.add(member) robot_session.commit() except Exception as ex: faker_user_info.exception(ex) faker_user_info.info("注册账户失败: Error.") forum_session.rollback() robot_session.rollback() else: faker_user_info.info("注册账户成功: OK.") CacheService.cache_data_insert_model("common_member", member) finally: forum_session.close() robot_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 spread_info(subject, message, author, fid, tid=0, file_name=None, attachment=None): """发信息. :parameter subject 标题 :parameter message 内容 :parameter author 会员名 :parameter fid 论坛Id :parameter tid 主题Id :parameter file_name :parameter attachment """ aid = 0 # 使用DZ远程附件时附件Id refresh = True if not attachment_enable: message = ''.join((message, download_link % (attachment, file_name))) try: # 处理 Data too long for column 'subject'错误 subject_count = len(subject) if subject_count > 80: subject = subject[:80] if not tid: # 1: 发主题 dateline = int(time.time()) forum_thread = ForumThread( __fid=fid, __author=author[1], __authorid=author[0], __subject=subject, __dateline=dateline, __lastpost=dateline, __lastposter=author[1], __attachment=type_attachment, # 附件,0无附件 1普通附件 2有图片附件 # __status=0, ) forum_session.add(forum_thread) if refresh: forum_session.flush() forum_session.refresh(forum_thread) tid = forum_thread.__tid post_info.info("1: 发主题 ==>> (%s)" % str(tid)) # 2:发帖子 max_pid = spread_repair_post() forum_post = ForumPost( __pid=max_pid + 1, __tid=tid, __fid=fid, __subject=subject, __message=message, __author=author[1], __authorid=author[0], __first=1, # 是否是首贴 __usesig=1, __attachment=type_attachment, __dateline=int(time.time()), ) forum_session.add(forum_post) if refresh: forum_session.flush() forum_session.refresh(forum_post) pid = forum_post.__pid post_info.info("2: 发帖子 ==>> (%s)" % str(pid)) if attachment_enable: # 3: 发附件 uid = author[0] index = int(str(tid)[-1]) # 附件索引 forum_affix_index = ForumAffixIndex(__tid=tid, __pid=pid, __uid=uid, __tableid=index) ForumAffixIndex.add(forum_affix_index) aid = forum_affix_index.__aid # 附件集合 description = file_name.split(".")[0] attachment_class = ModelFactory.get_attachment(index) forum_attachment = attachment_class( __aid=aid, __tid=tid, __pid=pid, __uid=uid, __filename=file_name, __attachment=attachment, __description=description, __dateline=int(time.time()), __remote=1, # 是否远程附件 __price=0, # 附件价格 ) forum_session.add(forum_attachment) if refresh: forum_session.flush() forum_session.refresh(forum_attachment) aid = forum_attachment.__aid post_info.info("3: 发附件 ==>> (%s)" % str(aid)) forum_session.commit() except Exception as ex: traceback.print_exc() forum_session.rollback() raise ex else: spread_repair_post(True) finally: forum_session.close() return tid, pid, aid