|
| 1 | +#!/bin/sh |
| 2 | +set -eu |
| 3 | + |
| 4 | +URLS_FILE="${1:-tests/corpus/urls.txt}" |
| 5 | +OUT_DIR="${2:-tests/fixtures/corpus}" |
| 6 | +INDEX_FILE="${OUT_DIR}/index.tsv" |
| 7 | +PRECHECK_HOST="${CORPUS_PRECHECK_HOST:-github.com}" |
| 8 | + |
| 9 | +if [ ! -f "${URLS_FILE}" ]; then |
| 10 | + echo "URLs file not found: ${URLS_FILE}" >&2 |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +if ! command -v curl >/dev/null 2>&1; then |
| 15 | + echo "curl is required but not found in PATH" >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +# Fail fast when command-line DNS/network is unavailable in this runtime. |
| 20 | +if ! node -e "require('node:dns').lookup('${PRECHECK_HOST}', (e) => process.exit(e ? 1 : 0))" >/dev/null 2>&1; then |
| 21 | + echo "DNS precheck failed for ${PRECHECK_HOST}. This runtime cannot resolve external hosts." >&2 |
| 22 | + echo "Run corpus fetch in a network-enabled shell (outside restricted sandbox) and retry." >&2 |
| 23 | + exit 2 |
| 24 | +fi |
| 25 | + |
| 26 | +mkdir -p "${OUT_DIR}" |
| 27 | +rm -f "${OUT_DIR}"/*.html |
| 28 | +printf "id\turl\tfile\tstatus\tdetail\n" > "${INDEX_FILE}" |
| 29 | + |
| 30 | +i=0 |
| 31 | +while IFS= read -r line || [ -n "${line}" ]; do |
| 32 | + url="$(printf "%s" "${line}" \ |
| 33 | + | tr -d '\r' \ |
| 34 | + | sed '1s/^\xEF\xBB\xBF//' \ |
| 35 | + | sed 's/^[[:space:]]*//; s/[[:space:]]*$//' \ |
| 36 | + | sed 's/^[-*][[:space:]]*//' \ |
| 37 | + | sed 's/[[:space:]]\+#.*$//' \ |
| 38 | + | sed 's/[;,][[:space:]]*$//')" |
| 39 | + case "${url}" in |
| 40 | + ""|\#*) continue ;; |
| 41 | + esac |
| 42 | + |
| 43 | + case "${url}" in |
| 44 | + http://*|https://*) ;; |
| 45 | + *) url="https://${url}" ;; |
| 46 | + esac |
| 47 | + |
| 48 | + i=$((i + 1)) |
| 49 | + id="$(printf "%03d" "${i}")" |
| 50 | + host="$(printf "%s" "${url}" | sed -E 's#^[a-zA-Z]+://##; s#/.*$##; s#[:?&=]#_#g')" |
| 51 | + [ -n "${host}" ] || host="site" |
| 52 | + file="${id}-${host}.html" |
| 53 | + out_path="${OUT_DIR}/${file}" |
| 54 | + |
| 55 | + err_file="${OUT_DIR}/.${id}.curl.err" |
| 56 | + if curl -fsSL \ |
| 57 | + --retry 2 \ |
| 58 | + --retry-all-errors \ |
| 59 | + --connect-timeout 10 \ |
| 60 | + --max-time 30 \ |
| 61 | + -H 'Accept: text/html,application/xhtml+xml' \ |
| 62 | + -H 'Accept-Language: en-US,en;q=0.9' \ |
| 63 | + -A 'ImageFetchCorpus/1.0 (+https://github.com/jtbrough/ImageFetch)' \ |
| 64 | + "${url}" \ |
| 65 | + -o "${out_path}" \ |
| 66 | + 2>"${err_file}"; then |
| 67 | + printf "%s\t%s\t%s\tok\t-\n" "${id}" "${url}" "${file}" >> "${INDEX_FILE}" |
| 68 | + rm -f "${err_file}" |
| 69 | + echo "ok ${url} -> ${file}" |
| 70 | + else |
| 71 | + rc=$? |
| 72 | + detail="$(tr '\n' ' ' < "${err_file}" | sed 's/[[:space:]]\+/ /g; s/^[[:space:]]*//; s/[[:space:]]*$//')" |
| 73 | + rm -f "${err_file}" |
| 74 | + [ -n "${detail}" ] || detail="curl_exit_${rc}" |
| 75 | + rm -f "${out_path}" |
| 76 | + printf "%s\t%s\t%s\tfetch_failed\t%s\n" "${id}" "${url}" "${file}" "${detail}" >> "${INDEX_FILE}" |
| 77 | + echo "fail ${url} (${detail})" >&2 |
| 78 | + fi |
| 79 | +done < "${URLS_FILE}" |
| 80 | + |
| 81 | +echo "Wrote corpus index: ${INDEX_FILE}" |
0 commit comments