Last active
May 8, 2020 06:08
-
-
Save hddananjaya/f85d5949fb31afb783fa48e5f5c560f6 to your computer and use it in GitHub Desktop.
Fidenz bash challenge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# WeatherReporter 1.0 | |
scriptDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
scriptFile="$scriptDir/$0" | |
tmpWeatherXMLFile="$scriptDir/sydneyData.xml" | |
logFile="$scriptDir/weatherReport.log" | |
tmpCronFile="$scriptDir/cronInfo" | |
function checkRequiredUtils() { | |
utilSet=("wget" "xmllint" "crontab") | |
for util in ${utilSet[*]}; do | |
type $util &> /dev/null | |
exitCode=$? | |
if [ $exitCode -ne 0 ] | |
then | |
echo "[!] Require '${util}' but can't find." | |
exit 1 | |
fi | |
done | |
} | |
function setupCronjob() { | |
crontab -l | grep $scriptFile &> /dev/null | |
exitCode=$? | |
if [ $exitCode -ne 0 ] | |
then | |
crontab -l > $tmpCronFile | |
echo "0 * * * * $scriptFile" >> $tmpCronFile | |
crontab $tmpCronFile | |
rm $tmpCronFile | |
echo '[+] Sheduled weather fetch for every hour' | |
fi | |
} | |
function fetchXML(){ | |
wget http://rss.weather.com.au/nsw/sydney -O $tmpWeatherXMLFile &>/dev/null | |
echo '[+] Fetched weather data' | |
sydneyData=$(cat $tmpWeatherXMLFile) | |
date=$(xmllint --xpath '//channel/pubDate/text()' $tmpWeatherXMLFile) | |
weatherData=$(xmllint --xpath '//channel/item[1]/description' $tmpWeatherXMLFile | sed -n 's:.*</b>\(.*\)<br />.*:\1:p') | |
rm $tmpWeatherXMLFile | |
} | |
function main() { | |
checkRequiredUtils | |
setupCronjob $@ | |
fetchXML | |
# weather dataset | |
dt=() | |
for data in $weatherData; do | |
dt+=($data) | |
done | |
echo '[+] Weather data of Sydney' | |
tee -a $logFile << EOT | |
+------------------------------------------------------------------------------------------------------------------------+ | |
| Date: $(printf %-113s "$date")| | |
+--------------+--------------+--------------+--------------+--------------+---------------+--------------+--------------+ | |
| $(printf %-12s "Temperature") | $(printf %-12s "DewPoint") | $(printf %-12s "Humidity") | $(printf %-12s "WindSpeed") | $(printf %-12s "WindGusts") | $(printf %-12s "WindDirection") | $(printf %-12s "Pressure") | $(printf %-12s "Rain") | | |
+--------------+--------------+--------------+--------------+--------------+---------------+--------------+--------------+ | |
| $(printf %-12s ${dt[0]//°/}) | $(printf %-12s ${dt[1]//°/}) | $(printf %-12s ${dt[2]}) | $(printf %-12s ${dt[3]}) | $(printf %-12s ${dt[4]}) | $(printf %-12s ${dt[5]}) | $(printf %-12s ${dt[6]}) | $(printf %-12s ${dt[7]}) | | |
+--------------+--------------+--------------+--------------+--------------+---------------+--------------+--------------+ | |
EOT | |
} | |
main $@ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment