Advertisement
mosaid

prayerTime.sh

Feb 11th, 2020
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.28 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. msgId="991148"
  4. scriptname=$( basename "$0" )
  5. is_running=$( pgrep -cf "$scriptname" )
  6. if (( $is_running > 1 )) && [[ -z "$1" ]] ; then
  7.     >&2 echo $scriptname is running.
  8.     exit 0
  9. fi
  10.  
  11. dir="${HOME}/.i3/conky/salat"
  12. declare -A v
  13. v["Fajr"]=1
  14. v["Dhuhr"]=2
  15. v["Asr"]=3
  16. v["Maghrib"]=4
  17. v["Isha"]=5
  18.  
  19. function CURL (){
  20.     userAgent="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:48.0) "
  21.     userAgent+="Gecko/20100101 Firefox/48.0"
  22.     curl -s -A "$userAgent" "$@"
  23. }
  24.  
  25. CURL 'https://www.islamicfinder.org/' \
  26. |sed -n -e '/Upcoming Prayer/{N;N;N;N;N;N;N;s/<[^>]*>//g;s/\s\s*/ /g;p}' \
  27. >| "/tmp/nextPrayerTime"
  28.  
  29. if [[ -n "$1" ]] ; then exit ; fi
  30.  
  31. while true ; do
  32.     sleep 1
  33.     nextPrayerName="$(cat "/tmp/nextPrayerTime" | cut -d' ' -f4 )"
  34.     nextPrayerTime="$(cat "/tmp/nextPrayerTime" | cut -d' ' -f5 )"
  35.     h=$(echo $nextPrayerTime|cut -d: -f1 )
  36.     m=$(echo $nextPrayerTime|cut -d: -f2 )
  37.     s=$(echo $nextPrayerTime|cut -d: -f3 )
  38.     if [[ -z "$h" ]] || [[ -z "$m" ]] || [[ -z "$h" ]] ; then
  39.         code=$(ping -c 1 8.8.8.8 2>&1 |grep unreachable >/dev/null; echo $? )
  40.         (( code == 0 )) && {
  41.             >&2 echo "PrayerTime: waiting for network..."
  42.             sleep 30
  43.             continue
  44.         }
  45.         >&2 echo "PrayerTime: getting data..."
  46.         CURL 'https://www.islamicfinder.org/' \
  47.         |sed -n -e '/Upcoming Prayer/{N;N;N;N;N;N;N;s/<[^>]*>//g;s/\s\s*/ /g;p}' \
  48.         >| "/tmp/nextPrayerTime"
  49.         continue
  50.     fi
  51.     if (( $s > 0 )) ;  then
  52.             s=$((s-1))
  53.         elif (( $m > 0)) ; then
  54.             m=$((m-1))
  55.             s=59
  56.         elif (( $h > 0)) ; then
  57.             h=$((h-1))
  58.             m=59
  59.             s=59
  60.         else
  61.             pn=${v[$nextPrayerName]}
  62.             dunstify -u critical -r "$msgId"  "$(cat "$dir/t") $(cat "$dir/$pn")"
  63.             CURL 'https://www.islamicfinder.org/' \
  64.             |sed -n -e '/Upcoming Prayer/{N;N;N;N;N;N;N;s/<[^>]*>//g;s/\s\s*/ /g;p}' \
  65.             >| "/tmp/nextPrayerTime"
  66.             continue
  67.     fi
  68.     echo " Upcoming Prayer $nextPrayerName $h:$m:$s" >| "/tmp/nextPrayerTime"
  69.     pn=${v[$nextPrayerName]}
  70.     Atext="$(cat "$dir/s") $(cat "$dir/$pn") $(cat "$dir/a")"
  71.     Atext+="  $(echo "$h:$m:$s"|rev)"
  72.     echo "$Atext " >| "/tmp/nextPrayerTimeA"
  73. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement