Skip to content

Instantly share code, notes, and snippets.

@hidao80
Last active August 1, 2020 09:14
Show Gist options
  • Save hidao80/3abfed097f8203eec97e6e327618ac76 to your computer and use it in GitHub Desktop.
Save hidao80/3abfed097f8203eec97e6e327618ac76 to your computer and use it in GitHub Desktop.
esa.io にローカルの Markdown ファイルを連続アップロードするシェルスクリプト
#!/bin/sh
#
# esa.io の 記事投稿 API を使って、ローカルの Markdown ファイルを投稿する
# 投稿に成功したファイルは $sent_file_dir に退避させ、重複登録を防ぐ
# コール制限は 75 回/15分 なので、target_dirに 75ファイル以上入れても 76ファイル目以降で
# 自動で止まる。続きは手動で再実行すること
#
target_dir=default
sent_file_dir=default/sent
api_token=<your-writable-api-token>
your_team=<your-team-name>
count=0
temp_file="tmp$(date +%y%m%d).json"
# 登録済みファイルの退避先がなければあらかじめ作っておく
if [ ! -d "$sent_file_dir" ]; then
mkdir -p "$sent_file_dir"
fi
for file in "$target_dir"/*.md; do
# ファイルの内容を JSON 向けに加工
content=$(sed -z "s/\n/\\\\n/g" "$file" | sed -e 's/\"/\\"/g' -e 's/`/\\\\`/g' -e 's/\t/\\\\t/g')
# ファイルの1行目から「#」文字を取り除き、記事のタイトルとする
title=$(head -n 1 "$file" | sed -e "s/#//g")
# posts API の仕様に従い、記事タイトル、本文、WIP の状態を JSON にする
# WIP の状態はデフォルトで true なので、false をセット
echo "{\"post\":{\"name\":\"$title\",\"body_md\":\"$content\",\"wip\":false}}" > $temp_file
# curl で API を実行する
# 実行結果の http ステータスコードを取得しておく
status=$(curl -X POST -H 'Content-Type: application/json' -H "Authorization: Bearer $api_token" -d @$temp_file https://api.esa.io/v1/teams/$your_team/posts -o /dev/null -w '%{http_code}\n' -s)
# 処理件数をカウントアップ
count=$(($count + 1))
# curl の実行結果が 200未満または 300以上だったら、API の実行が失敗とし、ループから抜ける(300番台はセーフでよいかも)
if [ $status -ge 200 -a $status -lt 300 ]; then
echo "$count [$status] $title"
# 記事 POST API に成功したら、ターゲットディレクトリから Markdown ファイルを移動させる
# 重複登録防止のため
mv "$file" "$sent_file_dir"
else
# 失敗したときだけ時刻を表示する。次実行開始するときの基準となるように
echo "$count [$status] miss! [$(date +%H:%M:%S)]: $title"
break
fi
done
# json の送信用一時ファイルを削除する
rm -f $temp_file
LICENSE='
MIT License
Copyright (c) 2018 hidao80
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment