def get_root_post_id(post_id): while True: post = kuzuha.get_log_by_id(post_id) if 'quote' not in post['_source']: break post_id = post['_source']['quote'] return post['_id']
def parse_log(log): if 'id' not in log: log['id'] = 0 body = '' if 'quote' in log: body += '>%s '% log.get('to', ' ') body += ' 投稿者:%s '% log.get('author', ' ') body += '投稿日:' body += parse_kuzuha_date(log['dt']) dt = dt_to_ymd(log['dt']) body += add_res_button(log['id'], dt) thread_id = log['id'] if 'quote' in log: thread_id = get_root_post_id(log['quote']) body += add_thread_button(thread_id, dt) body = add_tag(body, 'p') body += '\n<pre>' for (i, idx) in enumerate(('q2', 'q1')): if idx in log: if isinstance(log[idx], list): log[idx] = log[idx][0] for line in log[idx].splitlines(): body += '> ' * (2 - i) + line + '\n' body += '\n' + log.get('text', '') if 'quote' in log: kuzuha_dt = kuzuha.get_log_by_id(log['quote'])['_source']['dt'] dt = datetime.strftime(convert_datestr_to_datetime(kuzuha_dt), '%Y%m%d') body += '\n\n' + SANKOU.format(log['quote'], dt, parse_kuzuha_date(kuzuha_dt)) return body.strip() + '</pre>'
def add_child(post_ids, posts): result = '' last_idx = len(post_ids) - 1 for (i, post_id) in enumerate(post_ids): result += '└' if i == last_idx else '├' if post_id in posts: post = posts[post_id] else: post = kuzuha.get_log_by_id(post_id)['_source'] dt = dt_to_ymd(post['dt']) res_link = add_res_button(post_id, dt).strip() title_text = '" title="%s">' % parse_kuzuha_date(post['dt']) result += res_link.replace('">', title_text) + ' ' keisen = ' ' if i == last_idx else '│' if 'text' in post: result += post['text'].replace('\n', '\n' + keisen + ' ') + '\n' return result
def parse_log(log): if 'id' not in log: log['id'] = 0 body = '' if 'quote' in log: body += '>%s ' % log.get('to', ' ') body += ' 投稿者:%s ' % log.get('author', ' ') body += '投稿日:' body += parse_kuzuha_date(log['dt']) dt = dt_to_ymd(log['dt']) body += add_res_button(log['id'], dt) thread_id = log['id'] if 'quote' in log: thread_id = get_root_post_id(log['quote']) body += add_thread_button(thread_id, dt) body = add_tag(body, 'p') body += '\n<pre>' for (i, idx) in enumerate(('q2', 'q1')): if idx in log: if isinstance(log[idx], list): log[idx] = log[idx][0] for line in log[idx].splitlines(): body += '> ' * (2 - i) + line + '\n' body += '\n' + log.get('text', '') if 'quote' in log: kuzuha_dt = kuzuha.get_log_by_id(log['quote'])['_source']['dt'] dt = datetime.strftime(convert_datestr_to_datetime(kuzuha_dt), '%Y%m%d') body += '\n\n' + SANKOU.format(log['quote'], dt, parse_kuzuha_date(kuzuha_dt)) return body.strip() + '</pre>'
def generate_report(target_dt): target_day = datetime.strftime(target_dt, '%Y%m%d') use_usamin_link = datetime.today() - target_dt >= timedelta(days=7) posts = get_log(target_dt) posts = to_map(posts) prev_post = posts[min(posts)] prev_post['unixtime'] = get_unixtime(prev_post['dt']) prev_post['id'] = min(posts) id2time_table = {} warata_counter = Counter() mentions = defaultdict(list) stop_counter = defaultdict(list) response_timedeltas = defaultdict(list) for (post_id, post) in sorted(posts.items())[1:]: post['id'] = post_id post['unixtime'] = get_unixtime(post['dt']) id2time_table[post_id] = post['unixtime'] # 最長ストッパー previous_timedelta = post['unixtime'] - prev_post['unixtime'] stop_counter[previous_timedelta].append(prev_post['id']) if 'quote' in post: # 最速レス & 最遅レス if post['quote'] in id2time_table: mentioned_post_unixtime = id2time_table[post['quote']] else: mentioned_post = kuzuha.get_log_by_id(post['quote'])['_source'] if 'dt' not in mentioned_post: continue mentioned_post_unixtime = get_unixtime(mentioned_post['dt']) response_timedelta = post['unixtime'] - mentioned_post_unixtime response_timedeltas[response_timedelta].append(post_id) # 最多ワラタ if post.get('text'): if any(warata in post['text'] for warata in WARATA_EXPS): warata_counter[post['quote']] += 1 # 最多レス if 'quoted_by' in post: num_mentioned = len(post['quoted_by']) mentions[num_mentioned].append(post_id) prev_post = post # 最多ワラタ獲得賞 most_warata_posts = '' most_warata = 0 for (post_id, count) in warata_counter.most_common(): if count < most_warata: break most_warata = count if post_id in posts: post = posts[post_id] else: post = kuzuha.get_log_by_id(post_id)['_source'] most_warata_posts += '<hr>\n%s\n' % parse_log(post) most_warata_posts += add_tag(add_child(post['quoted_by'], posts), 'pre') if use_usamin_link: most_warata_posts = re_res.sub(USAMIN_LINK + '&id=', most_warata_posts) most_warata_posts = re_thread.sub(USAMIN_LINK + '&s=', most_warata_posts) most_warata_posts = Markup(most_warata_posts) # 最多レス獲得賞 if mentions: most_mentioned = max(mentions) most_mentioned_posts = join_posts(mentions[most_mentioned], posts, True, use_usamin_link) else: most_mentioned = None most_mentioned_posts = None # 最長ストッパー賞 if stop_counter: longest_stopped = max(stop_counter) longest_stopped_posts = join_posts(stop_counter[longest_stopped], posts, False, use_usamin_link) longest_stopped = parse_time(longest_stopped) else: longest_stopped = None longest_stopped_posts = None # 最速レス賞 if response_timedeltas: fastest_res = min(response_timedeltas) fastest_res_posts = join_posts(response_timedeltas[fastest_res], posts, False, use_usamin_link) fastest_res = parse_time(fastest_res) else: fastest_res = None fastest_res_posts = None # 最遅レス賞 if response_timedeltas: slowest_res = max(response_timedeltas) slowest_res_posts = join_posts(response_timedeltas[slowest_res], posts, False, use_usamin_link) slowest_res = parse_time(slowest_res) else: slowest_res = None slowest_res_posts = None return render_template('sov.html', dt=target_day, num_most_warata=most_warata, most_warata_posts=most_warata_posts, num_most_mentioned=most_mentioned, most_mentioned=most_mentioned_posts, longest_stopped=longest_stopped, longest_stopper=longest_stopped_posts, fastest_res=fastest_res, fastest_res_post=fastest_res_posts, slowest_res=slowest_res, slowest_res_post=slowest_res_posts)