Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bash script to generate .json files based on Wikipedia status codes #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions scripts/create-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Script retrieves all http status codes from official Wikipedia page
# and creates .json files for these, but only if they also exist within public/images

HTTP_STATUS_CODES_URL=https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
HTML_REGEX='(?<=><\/span>).*?(?=<\/dt>)'

# Retrieve HTML from URL
HTTP_STATUS_CODES_HTML=$(curl -L $HTTP_STATUS_CODES_URL)

# Run Regex to get closest to http status code information
STATUS_CODE_HTML_LIST=$(echo "$HTTP_STATUS_CODES_HTML" | grep -o -P "$HTML_REGEX")

# JSON template for .json files
JSON_TEMPLATE='{"code":"%s","text":"%s"}'
mkdir -p public/json

# Loop through every http status html and retrieve code and text.
# Create a .json file only if the http code already exist as a .jpg image.
while IFS= read -r line; do
http_status_string=$(echo "$line" | sed 's/([^()]*)//g' | sed 's/.*<\/span>//' | sed 's/<\/a>//' | sed 's/.*>//')
http_code=$(cut -d' ' -f1 <<< "$http_status_string")
http_text=$(cut -d' ' -f 2- <<< "$http_status_string" | xargs)
json_string=$(printf "$JSON_TEMPLATE" "$http_code" "$http_text")
if [[ -f "public/images/$http_code.jpg" ]]; then
echo "$json_string" > "public/json/$http_code.json"
fi
done <<< "$STATUS_CODE_HTML_LIST"