Skip to content

Instantly share code, notes, and snippets.

@TakamiChie
Created January 31, 2022 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakamiChie/213ef1c33288571aa75c28b57e7f54ac to your computer and use it in GitHub Desktop.
Save TakamiChie/213ef1c33288571aa75c28b57e7f54ac to your computer and use it in GitHub Desktop.
Markdown形式の日記ファイルから日付を指定してダイジェストを作るやつ
# python .\digest.py -s 2022/01 -e 2022/01 -o 202201digest.md -> 2022年1月のダイジェストを作る。
from datetime import datetime
from datetime import timedelta
from argparse import ArgumentParser
import calendar
import sys
p = ArgumentParser()
p.add_argument("-s", "--start_date", type=lambda s: datetime.strptime(s, "%Y/%m"), required=True, help="開始月。yyyy/mm形式の年月")
p.add_argument("-e", "--end_date", type=lambda s: datetime.strptime(s, "%Y/%m"), required=True, help="終了月。yyyy/mm形式の年月")
p.add_argument("-o", "--output", type=str, help="出力先ファイル名。")
args = p.parse_args()
args.start_date -= timedelta(days=1)
args.end_date = args.end_date.replace(day=calendar.monthrange(args.end_date.year, args.end_date.month)[-1])
ignorewords = ["今日やった", "明日やる", "就寝前", "文章", "朝の計画", "作業内容"]
day = args.start_date
if args.output:
out = open(args.output, mode="w", encoding="utf-8")
else:
out = sys.stdout
try:
while day < args.end_date:
day += timedelta(days=1)
if day.day == 1:
print (f"# ----{datetime.strftime(day, '%Y/%m')}----", file=out)
fn = f"{datetime.strftime(day, '%Y/%Y-%m-%d')}.md"
with open(fn, mode="r", encoding="utf-8") as f:
while line := f.readline():
if line.startswith("#") and list(filter(lambda s: s in line, ignorewords)) == []:
print("* [{}]({})".format(line.strip(' #\n'), fn), file=out)
finally:
if args.output:
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment