jsonをいじって日報にMM.DDの情報を埋め込んだりしたい
適切なプログラミング言語ってある?
とりあえずPythonでやるか
参考
https://qiita.com/Intel0tw5727/items/5e3e2f229d4bddcde3b7
https://qiita.com/neeeeeko/items/ebbff41d4b0f5125823a
https://qiita.com/johnjohn622/items/a3113a89686a04b1b53d
CosenseのJSONの構造
どう書くか
<- 2025.01.01 / 2024.12.30 ->
# 2024.12 --- # 12.31
こうかな~
関係度合いの順番的にも
明日→昨日→今月→MM月DD日の順番のリンク
で下にも置く
下にないとスクロールして次の日みるか~がやりづらいからいれてる
<- 2025.01.01 / 2024.12.30 ->
YYYY.MM.DDのフォーマットのページに対して
2行目と最終行を削除する
YYYY.MM.DDから、明日昨日今月MM月DD日のリンクを以下に書く
2行目
3行目
最終行
2025.01.01 <2024.12 / 12.31> 2024.12.30
これでもスマホで1行で表示されてスッキリするのだけど、リンクの順番が好ましくない
relatedでソート時の2 hop linkを共通するリンクの数でソートして欲しい
ChatGPTに聞きながらやってるけど今のところ全然できてないが近い処理はしてくれているのでもう少し手をいれるだけで良さそうに思える
が、整形しないでjsonを吐くというのはむずいのか?
code:diary-link-reformat.py
import json
import os
from datetime import datetime, timedelta
input_file = 'yozba.json'
output_file = 'diary_updated.json'
# ファイルの存在確認
if not os.path.exists(input_file):
print(f"Error: {input_file} does not exist.")
exit()
try:
# JSONファイルを読み込む
with open(input_file, 'r', encoding='utf-8') as file:
data = json.load(file)
print(f"Successfully loaded {input_file}")
# JSONのページをチェック
if 'pages' not in data:
print(f"Invalid format: 'pages' key not found in {input_file}")
# 各ページを処理
for page in data'pages':
if 'title' not in page or 'lines' not in page or len(page'lines') < 2:
continue
# ページタイトルから日付を取得(YYYY.MM.DD形式を想定)
try:
page_date = datetime.strptime(page'title', "%Y.%m.%d")
except ValueError:
continue
# 日付の計算
next_day = (page_date + timedelta(days=1)).strftime("%Y.%m.%d")
prev_day = (page_date - timedelta(days=1)).strftime("%Y.%m.%d")
current_month = page_date.strftime("%Y.%m")
display_month = page_date.strftime("%m")
display_day = page_date.strftime("%d")
# 最初の行と最後の行を変更
new_first_line = f"<- {next_day} / {prev_day} ->\n# {current_month} --- # {display_month}.{display_day}"
new_last_line = f"<- {next_day} / {prev_day} ->\n# {current_month} --- # {display_month}.{display_day}"
# linesの1行目を分割して処理
if isinstance(page'lines'1, str):
lines = page'lines'1.split(',')
else:
lines = [json.dumps(line, ensure_ascii=False) if isinstance(line, dict) else str(line) for line in page'lines'1:]
lines.insert(0, new_first_line)
lines.append(new_last_line)
page'lines'1: = ','.join(lines) # カンマ区切りで戻す
# JSONファイルを別名で保存(整形しない)
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, separators=(',', ':'))
print(f"Successfully saved updated data to {output_file}")
except Exception as e:
print(f"Error processing {input_file}: {e}")