import json, datetime, sys, re # usage: python step3.py [YYYY-MM-DD] (default = today) if len(sys.argv) > 1: rd = datetime.datetime.strptime(sys.argv[1], '%Y-%m-%d') else: n = datetime.datetime.now() rd = datetime.datetime(n.year, n.month, n.day) start = int(rd.timestamp()) end = start + 86400 print('reportDay=%s window: %d ~ %d' % (rd.strftime('%Y-%m-%d'), start, end)) def ok(t): t = (t or '').strip() if len(t) < 8: return False, 'too_short' has_cn = bool(re.search(r'[\u4e00-\u9fff]', t)) if not has_cn and len(t) < 20: return False, 'short_en' if not has_cn and t[-1] not in '.!?': return False, 'en_incomplete' if t[-1] in ',,::((、': return False, 'truncated' return True, 'ok' d = json.load(open('.weread/tmp/books_detail.json', encoding='utf-8')) kept = [] for bid, v in d.items(): marks = (v.get('marks') or {}).get('updated') or [] for m in marks: ct = m.get('createTime') if isinstance(ct, str): try: ct = int(ct) except Exception: ct = 0 ct = ct or 0 ts = datetime.datetime.fromtimestamp(ct).strftime('%Y-%m-%d %H:%M') if ct else 'NA' inday = start <= ct < end text = (m.get('markText') or '').strip() passed, why = ok(text) print('--- %s | %s | %s | quality=%s(%s)' % ( v['title'][:24], ts, 'IN_WINDOW' if inday else 'out', passed, why)) print(' chapter:', m.get('chapterTitle')) print(' text:', repr(text[:160])) if inday and passed: kept.append({'bookId': bid, 'title': v['title'], 'author': v.get('author'), 'chapter': m.get('chapterTitle'), 'text': text, 'createTime': ct}) json.dump(kept, open('.weread/tmp/today_marks.json', 'w', encoding='utf-8'), ensure_ascii=False, indent=1) print('\nKEPT_TODAY_MARKS =', len(kept))