def check_member_in_group(group_id, user_id): # 检查用户是否为组员 url = 'https://www.douban.com/group/' + group_id + '/member_search?q=' + user_id hearder = {'User-Agent': random.choice(my_headers)} doc = pq(url, headers=hearder) util.random_sleep(0, 5, 'checked if %s in group' % user_id) if '没有包含' in doc('.sbjtd').text(): return False else: return True
def dd_post(session, current): # 顶帖 with open('resources/dd_urls.json', 'r') as f: dd_url = json.load(f) count = 0 for topic_id, interval in dd_url.items(): if current % interval == 0: count += 1 reply_to_post(session, '', 'dd', topic_id=topic_id) util.random_sleep(60, 120, 'dd post ' + topic_id) return count
def get_all_reviewer(session, topic_id, self_intro=True, page=1): # 获取在报名帖中报名参与审核的用户id # 如果self_intro=True, 向新报名用户打招呼,便于以后发送私信 user_id_list = group_get.get_comment(topic_id, 'user_id', page=page) with open('applications/reviewer.txt', 'r') as f: reviewers = f.readlines() # ids reviewers = [r.strip('\n') for r in reviewers] with open('applications/canceled_reviewer.txt', 'r') as f: no_reviewers = f.readlines() # ids no_reviewers = [r.strip('\n') for r in no_reviewers] count = 0 for user_id in user_id_list: if user_id not in reviewers and user_id not in no_reviewers: count += 1 if self_intro: doumail.self_intro(session, user_id) util.random_sleep(5, 15, 'self intro') reviewers.append(user_id) reviewers = list(set(reviewers)) reviewers = [r + '\n' for r in reviewers] with open('applications/reviewer.txt', 'w') as f: f.writelines(reviewers) return count
def do_work(self, message, browser): util.random_sleep() args = self.prepare_args(message) if args is None: return return eval('self.'+args['message_type'])(args, browser)
def process_new_doumail(session, group_id, newonly=True, force_range=[]): # 处理用户消息 with open('resources/member-%s.json' % group_id, 'r') as f: member_dict = json.load(f) with open('applications/wait_to_reply.json', 'r') as f: wait_to_reply = json.load(f) with open('applications/replied_nonmember.json', 'r') as f: replied_nonmember = json.load(f) Reply = RespGen() if not force_range: doumails = get_all_doumail(session, newonly=newonly) else: doumails = get_all_doumail(session, newonly=newonly, n=force_range[0], ignore=force_range[1]) for mail in doumails: #{'time':time,'sender':sender,'preview':preview,'link':link} print(mail) if mail['user_id'] in member_dict or group_get.check_member_in_group( group_id, mail['user_id']): # 如果用户为组员 if '取消帮助审核' in mail['preview']: with open('applications/reviewer.txt', 'r') as f: reviewers = f.readlines() # ids reviewers.remove(mail['user_id'] + '\n') with open('applications/reviewer.txt', 'w') as f: f.writelines(reviewers) with open('applications/canceled_reviewer.txt', 'r') as f: no_reviewers = f.readlines() # ids no_reviewers.append(mail['user_id'] + '\n') with open('applications/canceled_reviewer.txt', 'w') as f: f.writelines(no_reviewers) content = '🤖取消成功' elif '此用户申请' in mail['preview']: text = mail['preview'] text = text.replace(':', ':') try: opinion, user_id = text.split('此用户申请:') except: text = get_doumail_from_user( session, mail['user_id'], n=2, user_only=False)['msgs'][0]['text'] text = text.replace(':', ':') opinion, user_id = text.split('此用户申请:') if opinion == '同意': review.add_review(mail['user_id'], user_id, approve=1) else: review.add_review(mail['user_id'], user_id, approve=0) content = '🤖审核成功' elif '顶帖' in mail['preview']: text = mail['preview'] #print('full text:',text) try: _, url, hours = text.split(' ') hours = int(hours) if hours > 6: iter = int(hours * 60 // 2) with open('resources/dd_urls.json', 'r') as f: dd_url = json.load(f) dd_url[url] = iter with open('resources/dd_urls.json', 'w') as f: json.dump(dd_url, f) content = '🤖已记录' else: content = '🤖间隔小时不能小于6,如有需求请私信开发者' except: content = '🤖无法识别,请输入"顶帖 帖子id(网址最后的数字) 间隔小时数",不要有多余的空格' else: # 非特定关键词消息,随机回复 content = '' if '?' in mail['preview'] or '?' in mail['preview']\ or random.choice([0,1,2])==0: content = '🤖' + Reply.getResp_AI( mail['preview'].strip('.').replace('机器人名字', '菲菲'), username=mail['username']) if not content or '无法理解' in content: content = '🤖' + Reply.getResp( '机器人', username=mail['username']) # + ' \n -- Hi,这是一条自动回复 ' util.random_sleep(0, 15, 'wait before reply') sent_doumail(session, mail['user_id'], content) util.random_sleep(60, 90, 'processed message') else: # 非组员 if '申请入组' in mail['preview']: content = '🤖小组自动审核方式:先发送文字、截图等能证明你符合入组要求的资料,最后发送"申请入组"四个字。如已发送好,将随机邀请多名组员审核,请耐心等待' sent_doumail(session, mail['user_id'], content) review.collect_review_content(session, mail['user_id']) util.random_sleep(60, 90, 'processed message') elif mail['user_id'] not in replied_nonmember: #尚未回复过消息 content = '🤖小组自动审核方式:先发送文字、截图等能证明你符合入组要求的资料,最后发送"申请入组"四个字' sent_doumail(session, mail['user_id'], content) replied_nonmember.append(mail['user_id']) with open('applications/replied_nonmember.json', 'w') as f: json.dump(replied_nonmember, f) util.random_sleep(60, 90, 'processed message') else: # 已经回复过消息 if mail['user_id'] in wait_to_reply: wait_to_reply.remove(mail['user_id']) with open('resources/wait_to_reply.json', 'w') as f: json.dump(wait_to_reply, f) content = '🤖消息已收到,记得发送"申请入组"四个字开始自动审核' sent_doumail(session, mail['user_id'], content) util.random_sleep(60, 90, 'processed message') logging.info(mail['preview'] + ' sent by id =' + mail['user_id']) return len(doumails)
def main_review(session): s = session for count in range(1, 100000): print('iteration', count) if count % 1000 == -1: group_get.get_group_members(group_id) util.random_sleep(120, 240, 'checked members') elif count % 100 == -1: if get_all_reviewer(s, reviewer_topic_id): util.random_sleep(120, 240, 'got reviewers') if count % 10 == -1: if get_all_applied_user(s, group_id): util.random_sleep(30, 60, 'got applications') if review_all(s, group_name, group_id, [], review_thre=2, verify_members=True): util.random_sleep(90, 180, 'reviewed requests') else: count = doumail.process_new_doumail(s, group_id) util.flush_cookies(s) if count == 0: util.random_sleep(300, 600, 'no new message to process') else: util.random_sleep(180, 300, 'processed all %s new messages' % str(count)) if random.choice(list(range(10))) == 0: util.random_sleep(0, 3600, 'rest') util.flush_cookies(s)
def review_all(session, group_name, group_id, admin_ids, review_thre=3, verify_members=False, notify_admin=False): # 处理所有入组申请 if os.path.isfile('applications/wait_to_review.json'): with open('applications/wait_to_review.json', 'r') as f: wait_to_review = json.load(f) else: return with open('applications/user_request_dict.json', 'r') as f: user_request_dict = json.load(f) with open('applications/wait_to_reply.json', 'r') as f: wait_to_reply = json.load(f) with open('applications/member-%s.json' % group_id, 'r') as f: member_dict = json.load(f) with open('applications/replied_nonmember.json', 'r') as f: replied_nonmember = json.load(f) if verify_members: #检查待处理的入组申请是否已经被(其他管理员)处理过 #或者用户是否已经在组内 keys = list(wait_to_review.keys()) for user_id in keys: if user_id not in user_request_dict \ or user_id in member_dict \ or group_get.check_member_in_group(group_id, user_id): del wait_to_review[user_id] else: wait_to_review[user_id]['passed'] = False with open('applications/wait_to_review.json', 'w') as f: json.dump(wait_to_review, f) for user_id in wait_to_reply: if user_id not in user_request_dict \ or user_id in member_dict \ or group_get.check_member_in_group(group_id, user_id): wait_to_reply.remove(user_id) with open('applications/wait_to_reply.json', 'w') as f: json.dump(wait_to_reply, f) count = 0 for user_id in user_request_dict.keys(): if group_get.check_member_in_group(group_id, user_id): continue if user_id not in wait_to_review: if user_id not in replied_nonmember and user_id not in wait_to_reply: # if not reviewed, not replied, and not self-intro # self intro and put in wait_to_reply list # after user replied, will reply and put in 'replied_nonmember' list in process_new_doumail function doumail.self_intro(session, user_id) util.random_sleep(5, 15, 'self intro') wait_to_reply.append(user_id) with open('applications/wait_to_reply.json', 'w') as f: json.dump(wait_to_reply, f) continue # check user status user = wait_to_review[user_id] if user['passed']: continue if user['invited'] == False: with open('applications/review_content/%s.json' % user_id, 'r') as f: review_content = json.load(f) ask_for_review(session, review_content, user_request_dict[user_id]['content'], group_name, n=6) count += 1 else: # waiting reviewer's opinions reviews = list(user['reviews'].values()) positive_count = sum([1 for r in reviews if r > 0]) negative_count = len(reviews) - positive_count if positive_count >= review_thre: # notify admin to approve this user content = '已有%s同意,%s拒绝此用户入组申请:\n%s \n用户主页:%s' % ( str(positive_count), str(negative_count), user['username'], user['userpage']) if notify_admin: for admin in admin_ids: doumail.sent_doumail(session, admin, content) sleept = random.choice(list(range(40, 80))) print('已通知管理员%s同意%s申请,sleep' % (admin, user_id), sleept) time.sleep(sleept) else: print(content) accept_user( session, group_id, requestid=user_request_dict[user_id]['request_id']) sleept = random.choice(list(range(20, 40))) print('已同意%s申请,sleep' % (user_id), sleept) time.sleep(sleept) elif negative_count >= review_thre: # notify admin to approve this user content = '已有%s同意,%s拒绝此用户入组申请:\n%s \n用户主页:%s' % ( str(positive_count), str(negative_count), user['username'], user['userpage']) if notify_admin: for admin in admin_ids: doumail.sent_doumail(session, admin, content) sleept = random.choice(list(range(40, 80))) print('已通知管理员%s拒绝%s申请,sleep' % (admin, user_id), sleept) time.sleep(sleept) else: print(content) reject_user( session, group_id, requestid=user_request_dict[user_id]['request_id']) sleept = random.choice(list(range(20, 40))) print('已拒绝%s申请,sleep' % (user_id), sleept) time.sleep(sleept) util.flush_cookies(session) return count
def writer(c): with fs_.cloned() as fs: for i in range(10): fs.write(fd, f'client {c} log line {i}\n'.encode()) random_sleep()
def writer(c): with fs_.cloned() as fs: fd = fs.open('/log.txt', append=True) for i in range(10): fs.write(fd, f'client {c} log line {i}\n'.encode()) random_sleep()