Finding the closest Apache Software Foundation mirror programatically

为君一笑 提交于 2019-11-29 07:34:02

The mirror URLs in the page are marked up as <strong>, so you can scrape the page to get the top recommendation like this:

curl 'https://www.apache.org/dyn/closer.cgi' |
  grep -o '<strong>[^<]*</strong>' |
  sed 's/<[^>]*>//g' |
  head -1

Additionally, closer.cgi supports an ?as_json=1 query parameter to provide the same information as JSON. The result has a key of preferred for the closest mirror, as well as http for the alternatives.

soulmachine

There is a more elegant way by using jq:

curl -s 'https://www.apache.org/dyn/closer.cgi?as_json=1' | jq --raw-output '.preferred'

Here is an alternative using python:

curl -s 'https://www.apache.org/dyn/closer.cgi?as_json=1' \
| python -c "import sys, json; print json.load(sys.stdin)['preferred']"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!