fereria
7/24/2019 - 3:39 PM

create updatepage

# -*- coding: utf-8 -*-
import requests
import json
import os.path
from datetime import datetime

from create_mkdocs_pages import get_summary_word

ROOT_PATH = "S:/docs/markdown_note/reincarnation_tech"
UPDATE_MD = "S:/docs/markdown_note/reincarnation_tech/docs/update_log.md"

val = requests.get('https://api.github.com/repos/fereria/reincarnation_tech/commits/master')
commitValue = json.loads(val.text)

files = []
for f in commitValue['files']:
    ext = os.path.splitext(f['filename'])[1]
    if ext == ".md":
        md_path = ROOT_PATH + "/" + f['filename']
        if os.path.exists(md_path):
            if os.path.basename(md_path) != "update_log.md":
                files.append([f['filename'], get_summary_word(md_path)])


dt = datetime.strptime(commitValue['commit']['author']['date'], '%Y-%m-%dT%H:%M:%SZ')

# LogをいったんJsonとして出力(コメント重複をさけるため)
json_log = []
with open('update_log.json', 'r') as f:
    json_log = json.load(f)

# まだログに書き出してない場合のみ追加処理する
if len([x for x in json_log if x['SHA'] == commitValue['sha']]) == 0:
    buff = {}
    buff['SHA'] = commitValue['sha']
    buff['DATE'] = dt.strftime('%Y/%m/%d %H:%M:%S')
    buff['COMMENT'] = commitValue['commit']['message']
    buff['FILES'] = files
    json_log.append(buff)

with open('update_log.json', 'w') as f:
    f.write(json.dumps(json_log, indent=2, ensure_ascii=False))

# Jsonを元にして更新履歴ページを生成
# 最新が一番上になるようにする
json_log.reverse()

# Markdown作成
updatePage = ["# 更新履歴"]
for i in json_log:
    if len(i['FILES']) == 0:
        continue
    updatePage.append("## " + i['DATE'])
    updatePage.append("<!--{}-->".format(i['SHA']))
    updatePage.append("")
    updatePage.append(i['COMMENT'])
    updatePage.append("")
    updatePage.append("### 更新ページ")
    for f in i['FILES']:
        updatePage.append("* [{}]({})".format(f[1], f[0].replace("docs/", "")))
with open(UPDATE_MD, 'w', encoding='utf-8') as f:
    f.write("\n".join([x + "  " for x in updatePage]))