以前使ってて、最近また使おうとしている、AutomatticのSimplenoteなのですが、前使ってた時に何回かノートがおかしくなったことがありました。原因はSublime Textの拡張機能のせいだったのかもしれませんが……
自動でバックアップとかできんのかなぁと調べたところ、下のを見つけました。hiroshiさん、ありがとうございます。
GitHub – hiroshi/simplenote-backup: Backup simplenote as text files
でもPython……、いやこのスクリプトを動かすのは問題ないのですが、PHPの方がお手軽かなぁと。
Simperiumのライブラリが使われてて、その元が下のだそうでリファレンス見ながらPHPにしてみました。
Simperium HTTP – Getting Started
下のプログラムは色々と手抜きです。適当に修正して、cronとかで動かしてください。
実行すると、全てのノートをひとつのテキストファイルとして出力します。出力内容はノートのID、タグ、本文、区切り線となっていて、ゴミ箱に入ったノートはタグにTrashを追加してます。
トークンの調べ方はGitHub – hiroshi/simplenote-backupのInstallation 3. Get your tokenを御覧ください。
<?php
echo date('Y-m-d H:i:s') . ' start<br />';
if (!function_exists('curl_reset')) {
function curl_reset(&$ch){
$ch = curl_init();
}
}
$your_token = 'abcdefghijklmnopqrstuvwxyz123456'; // 32桁の英数字
$baseurl = 'https://api.simperium.com/1/chalk-bump-f49/note/index?data=1';
$header = array('X-Simperium-Token: ' . $your_token);
$mark = '';
$index = array();
$ch = curl_init();
do {
$url = empty($mark) ? $baseurl : $baseurl . '&mark=' . $mark;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$dump = json_decode(curl_exec($ch), true); // 連想配列で
$mark = isset($dump['mark']) ? $dump['mark'] : '';
$index = array_merge($index, $dump['index']);
curl_reset($ch);
} while ($mark != '');
curl_close($ch);
$text = '';
foreach ($index as $note) {
// if ($note['d']['deleted']) continue; // ゴミ箱のを省略するなら
$text .= $note['id'] . "\n";
if ($note['d']['deleted']) $note['d']['tags'] = array_merge(array('Trash'), $note['d']['tags']);
$text .= implode(', ', $note['d']['tags']) . "\n";
$text .= $note['d']['content'] . "\n- - - - - - - - - - - - - - - - - - - - \n";
}
file_put_contents('simplenote.txt', $text);
echo date('Y-m-d H:i:s') . ' end';