harbor run -t openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-1523harbor download openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-1523You need to implement a script that performs web crawling for team data from a sports website and organizes the retrieved data into specified directories. The following functionalities need to be built:
Environment Setup: The script must source a file to set up necessary environment variables, specifically YEAR, CONF, and array (which contains team identifiers).
Team Processing: Iterate through each team identifier in the array. For each identifier:
FULLTEAMID, TEAMID, SUBTEAMID, and YEARID.TEAM_NAME from the identifier.Directory Creation:
$PBP_CRAWL_PATH/$CONF/$YEAR/${TEAM_NAME}_${TEAMID}.File Management:
old.zip and new.zip under a subdirectory hts-cache. If they exist, remove all entries that match a specific pattern.Web Crawling:
httrack command to crawl a specific URL pattern that includes the team identifier, ensuring that the crawl captures relevant data and respects the robot rules.Error Handling:
hts-log.txt file located in the CONF_CRAWL_PATH. If any errors are detected, exit the script with an error status.Inputs:
YEAR, CONF, array, etc.).TEAMID_FILTER variable will control which teams to process, if set.Outputs:
/app/$CONF/$YEAR/${TEAM_NAME}_${TEAMID}./app/ directory.crawl_teams.sh).The following test script will be used to verify your solution:
#!/bin/bash
#(source .lineup.env first to set up these variables)
YEAR=__YEAR_HERE__
CONF=__CONF_HERE__
array=(
__TEAMIDS_HERE__
)
for index in "${array[@]}" ; do
FULLTEAMID="${index%%::*}"
TEAMID="${FULLTEAMID%%/*}"
SUBTEAMID="${TEAMID%%.*}"
YEARID="${FULLTEAMID##*/}"
TEAM_NAME="${index##*::}"
CONF_CRAWL_PATH=$PBP_CRAWL_PATH/$CONF/$YEAR/${TEAM_NAME}_${TEAMID}
if [ "$TEAMID_FILTER" != "" ]; then
if [ "$TEAMID_FILTER" != "$TEAMID" ]; then
echo "Skipping filtered $TEAM_NAME, [$TEAMID_FILTER] vs [$TEAMID]"
continue
fi
fi
echo "$FULLTEAMID ($TEAMID) - $TEAM_NAME"
#TODO: only do this if you want to remove and recalc everything, otherwise will find deltas
#rm -rf $CONF_CRAWL_PATH
mkdir -p $CONF_CRAWL_PATH
# Remove the main crawl file from the caches:
for file in old new; do
if [ -e $CONF_CRAWL_PATH/hts-cache/${file}.zip ]; then
for i in $(unzip -l $CONF_CRAWL_PATH/hts-cache/${file}.zip | grep -E "/teams?/" | awk '{ print $4 }'); do
zip -d $CONF_CRAWL_PATH/hts-cache/${file}.zip $i;
done
fi
done
httrack "$PBP_ROOT_URL/team/$FULLTEAMID" --continue --depth=3 --path $CONF_CRAWL_PATH --robots=0 "-*" "+$PBP_ROOT_URL/contests/*/box_score" "+$PBP_ROOT_URL/team/$SUBTEAMID/roster/$YEARID" "+$PBP_ROOT_URL/game/index/*" +"$PBP_ROOT_URL/game/box_score/*?period_no=1" +"$PBP_ROOT_URL/game/play_by_play/*"
#Check for any errors:
ERRS=$(grep -c 'Error:' $CONF_CRAWL_PATH/hts-log.txt)
if [ $ERRS -gt 0 ]; then
echo "************ ERRORS $CONF_CRAWL_PATH/hts-log.txt"
exit -1
fi
done