最近因为办事,需要在某个网站上提前预约,最早能约到的时间是半个多月以后。
但这个网站有个好处:可用日期和时间段通过API获取,像这样:
)]}',
{
"status": "OK",
"data":[
{
"key": "...",
"date": "2019-04-30",
"startTime": "09:55",
"endTime": "10:00",
},
{
"key": "...",
"date": "2019-04-30",
"startTime": "10:35",
"endTime": "10:40",
},
...
(前面的")]}'"是为了预防覆盖JS数组构造函数跨站攻击https://stackoverflow.com/questions/6572295/what-kind-of-json-answer-starts-with)
只要有人取消了自己的预约,可用的时间段就会立刻释放出来。所以用curl加jq写个几行的脚本:
fetch() {
curl https://website/api/endpoint 2>/dev/null
| sed 1d
| jq '.data|map(select(.date != "2019-04-23" and .date < "2019-04-30"))'
}
RES=$(fetch)
while [ "${RES}" = "[]" ]; do
sleep 5 && RES=$(fetch) && echo -n ".";
done
while true; do
terminal-notifier -message "New date found!" && sleep 2;
done
sed是为了去掉开头第一行的")]}",否则jq无法parse。因为4月23号有事,所以用jq寻找所有早于29号但不是23号的开放日期。最后用terminal-notifier在mac上弹个通知。反复提示是因为刚跑了没多久就发现一个18号可用的时间段,但因为忙工作没立刻注意到,等看到的时候已经被别人预约掉了 XD