def get_user_history(db, thread_id, start_time=-1, end_time=-1): collection = db.user_history out = open('output/user_history.txt', 'w') reply_dict = get_text_from_db() start_time_formatted, end_time_formatted = get_time_interval( start_time, end_time) for each in collection.find({'thread_id': thread_id}): if start_time_formatted > each['user_history'][0][-1] \ or end_time_formatted < each['user_history'][-1][-1]: continue out.write('==================================\n') for each_round in each['user_history']: #print(each_round) timestamp = datetime.datetime.fromtimestamp(int( each_round[5])).strftime('%c') print(each_round[5], timestamp) out.write('{}:\n'.format(timestamp)) for each_sentence in each_round[ 3]: #reply_dict[each_round[0]][each_round[1]].texts[each_round[3]][each_round[2]]: # each_sentence = each_sentence.replace("’","'") out.write("BOT: {}\n".format(each_sentence)) out.write("USER:{}\n".format(each_round[4])) out.write('==================================\n') out.close()
def date_report(start_time=-1, end_time=-1, weekday=()): pymongo_client = MongoClient() db = pymongo_client.chatbot collection = db.user_history reply_dict = get_text_from_db() start_time_formatted, end_time_formatted = get_time_interval( start_time, end_time) user_set = set() bot_choice = defaultdict(list) weekday_choice = defaultdict(int) out = open('output/time_report.txt', 'w') params = Params() for each in collection.find({}): if start_time_formatted > each['user_history'][0][-1] \ or end_time_formatted < each['user_history'][-1][-1]: continue each_weekday = get_weekday(each['user_history'][0][-1]) if len(weekday) != 0 and each_weekday not in weekday: continue user_id = each['thread_id'] user_set.add(user_id) bot_choice[each['user_history'][0][0]].append(user_id) weekday_choice[each_weekday] += 1 out.write('total number of user: {}\n'.format(len(user_set))) out.write('==================================\n') out.write('user list\n') for each in user_set: out.write("{}\n".format(each)) out.write('==================================\n') if len(weekday) != 0: for key, val in weekday_choice.items(): #print(key, val) out.write("There are {} users on {}\n".format(val, weekdays[key])) out.write('==================================\n') for key, val in bot_choice.items(): out.write("{} is used {} time(s)\n".format(params.bot_name_list[key], len(val))) out.write("{}\n".format(val)) if len(user_set) > 0: out.write('==================================\n') out.close()
opts, args = getopt.getopt(sys.argv[1:],'',['stime=', 'bot=', 'mode=']) except getopt.GetoptError: print usage_tips sys.exit() add_bot_ctl = {} for opt, arg in opts: if opt == '--stime': add_bot_ctl['sleeping_time'] = int(arg) elif opt == '--bot': add_bot_ctl['bot_choice'] = int(arg) elif opt == '--mode': add_bot_ctl['mode'] = str(arg) else: print usage_tips sys.exit() #collection = db.user_history reply_dict = get_text_from_db() if os.path.exists("/commuter/nltk_data"): nltk.data.path = ["/commuter/nltk_data"] + nltk.data.path password_file = open('password.txt','r') email = password_file.readline().strip() password = password_file.readline().strip() print('email: {}, password: {}'.format(email, password)) client = StressBot(email, password, reply_dict, add_bot_ctl=add_bot_ctl) client.listen()