Extending this answer solution, I've wrote the following bash script
to get the html of my StackExchange accounts page and parse it to a YAML file. The objective was list for each community I participate its url and my user id.
get-stackexchange-info.sh
#!/bash/bin
NETWORK_PROFILE_URL=$1 # https://stackexchange.com/users/3581081/artu-hnrq
read -r ID USER <<< \
$( cut -d'/' -f5,6 <<< $NETWORK_PROFILE_URL \
| sed 's/\// /g' \
)
OUTPUT_FILE=stackexchange.yml
echo \
"user: $USER
stackexchange:
url: http://stackexchange.com
id: $ID" \
> $OUTPUT_FILE
parse(){
local HTTPS="https:\/\/"
local COMMUNITY="[^\.]*"
local DOMAIN="[^\/]*"
local PROFILE_ID="\d*"
local MODEL="\1:\n url: $HTTPS\1\2\n id: \3"
sed -E "s/($COMMUNITY)($DOMAIN)\/($PROFILE_ID)/$MODEL/"
}
curl "https://stackexchange.com/users/$ID/$USER?tab=accounts" \
| grep "account-container" -A1 \
| grep "http" \
| cut -d"/" -f3,5 \ # stackoverflow.com/3581081
| parse \
>> $OUTPUT_FILE
I would like to receive some opinions about the parse process (usage of grep
, cut
and sed
commands) and about control flow management could be improved to the script
output example:
user: artu-hnrq
stackexchange:
url: https://stackexchange.com
id: 3581081
askubuntu:
url: https://askubuntu.com
id: 689894
stackoverflow:
url: https://stackoverflow.com
id: 2989289
codereview:
url: https://codereview.stackexchange.com
id: 213097
Thanks a lot!