Advertisement
Kimarite

Monitor changes on a webpage (script)

Feb 10th, 2019
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #--------------------------------------------------------------------------------
  4. # Monitors the changes in a webpage
  5. #
  6. # To run it:
  7. # ./monitorwebpage.sh http://www.thewebpage.io mywebpage
  8. # where `http://www.thewebpage.io` is the address of the page you want to follow,
  9. # and mywebpage a suffix (default: page), needed if you monitor multiple pages at once.
  10. # -> The program checks the website every 15 minutes (timing can be changed),
  11. # and if there is a change, opens the new webpage.
  12. #
  13. #
  14. #
  15. # NOTES:
  16. #
  17. # This script works with Ubuntu;
  18. # On a Mac, replace "xdg-open" by "open"
  19. #
  20. # Source of some of the code:
  21. # http://bhfsteve.blogspot.fr/2013/03/monitoring-web-page-for-changes-using.html
  22. # And this script source: https://trucastuces.wordpress.com/2017/04/21/monitor-changes-on-a-webpage/
  23. #--------------------------------------------------------------------------------
  24.  
  25. # URL to monitor is entered as input
  26. URL="$1"
  27. # The second argument is optional, it is a suffix for the webpage
  28. # if we want to monitor multiple webpages at once
  29. # SUFF=${2:-page}
  30.  
  31. # Error message if not argument is supplied
  32. if [[ -z "$*" ]] ; then
  33. echo "Error: you need to enter a webpage as argument!
  34. (don't forget http://)"
  35. exit 0
  36. fi
  37.  
  38. echo "Monitoring "${URL}
  39.  
  40. chmod -R 774 /home/$USER/bin/monitorURL/work/
  41.  
  42. chmod -R 664 /home/$USER/bin/monitorURL/work/*
  43.  
  44. cd /home/$USER/bin/monitorURL/work/
  45.  
  46. # Initialization: download the webpage once to create old.html
  47. # curl ${URL} -L --compressed -s > old${SUFF}.html
  48. curl ${URL} -L --compressed -s > old.html
  49.  
  50. for (( ; ; )); do # (Infinite loop)
  51. # Change the name of new.html into old.html,
  52. # and erase potential error messages (2> /dev/null)
  53. # mv new${SUFF}.html old${SUFF}.html 2> /dev/null
  54. mv new.html old.html 2> /dev/null
  55.  
  56. # Print time at which the page is checked
  57. THEDATE="$(date)"
  58. echo "Last checked: ${THEDATE}"
  59.  
  60. # Download the webpage
  61. # curl ${URL} -L --compressed -s > new${SUFF}.html
  62. curl ${URL} -L --compressed -s > new.html
  63. # -L means that we follow potential redirections
  64. # --compressed compresses,
  65. # -s for quiet mode,
  66. # and save the output in new.html.
  67.  
  68. # Compare the two versions
  69. # DIFF_OUTPUT="$(diff new${SUFF}.html old${SUFF}.html)"
  70. DIFF_OUTPUT="$(diff new.html old.html)"
  71. # If there are differences:
  72. if [ "${DIFF_OUTPUT}" != "" ]; then
  73. # open the webpage
  74. URL="$1"
  75. xdg-open ${URL}
  76. break
  77. fi
  78. # Then wait for 15 minutes before checking the website again
  79. # Then wait for 20 seconds before checking the website again
  80. sleep 20s
  81. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement