Fabian42

Corona sheet filling script

Jan 22nd, 2021 (edited)
920
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # NOTE: I'm publishing this here on Pastebin, but that doesn't mean that I would recommend running it. I have made a lot of improvements to its code quality and stability over time, but it still relies on my personal Bash aliases and I definitely know that it will break horribly whenever anything in the sources changes.
  3. # abort if not executed with aliases etc. working
  4. if [[ $- != *i* ]]; then return; fi
  5.  
  6. insert_full(){
  7.  echo "$1" | xclip -selection clipboard
  8.  # down from header, paste, next sheet
  9.  xdotool key Down sleep 1 key Shift+KP_Insert sleep 1 key Alt+Down sleep $wait
  10. }
  11.  
  12. insert_one(){
  13.  echo "$1" | xclip -selection clipboard
  14.  # down to last entry, down to first empty cell, paste, next sheet
  15.  xdotool key Ctrl+Down sleep 1 key Down sleep 1 key Shift+KP_Insert sleep 1 key Alt+Down sleep $wait
  16. }
  17.  
  18. altkreis_assumption(){
  19.  # if no article found: down to last entry, copy, down to first empty cell, paste, next sheet
  20.  xdotool key Ctrl+Down sleep 1 key Ctrl+c sleep 1 key Down sleep 1 key Shift+KP_Insert sleep 1 key Alt+Down sleep $wait
  21. }
  22.  
  23. worldometers(){
  24.  # $1 is the number of leading rows to remove for alignment of all tables on 2020-05-25
  25.  # $2 is the series name to be searched
  26.  # find series name on website, get number list ("data"), remove prefix, remove suffix, "null"→"0", negatives→0, split on commas, align to 2020-05-25
  27.  ech "$page" | grep -i -m 1 --after-context=5 "name: '$2'" | grep "data" | sed -E "s/ *data\\: \\[//" | sed -E "s/\\] *\\} *\\]? *\\,? *\\{? *//" | sed -E "s/null/0/g" | sed "s/\\-[0-9]+/0/g" | sed -E "s/\\,/\\n/g" | tail -n +$1
  28. }
  29.  
  30. ### START
  31. # open sheet
  32. (firefox "docs.google.com/spreadsheets/d/1uDTghO_ZYBs5nfs2kDc0Ms6e9bbx7clx_QgkWii7OMY" &> /dev/null & disown)
  33. export wait=6
  34.  
  35. # print old state in case something goes wrong
  36. cat "/home/fabian/misc/corona_storage"
  37.  
  38. # Hannover cases and deaths, Lehrte cases and new (first because of highest probability of failure)
  39. # This breaks if 20 or more articles were published today after the Corona update. I could fix that, but it's extremely unlikely. Changes to the website are much more likely.
  40. # In case of a missed day: Replace »$(date "+%Y\\/%m\\/%d")« with e.g. »2021\\/03\\/29«
  41. links=$(wget --no-check-certificate -q -O - "www.altkreisblitz.de/region-hannover" | grep -E "<div class\\=\\\"news\\-list\\-item\\-title\\\"><h2><a href\\=\\\"https\\:\\/\\/www\\.altkreisblitz\\.de\\/aktuelles\\/datum\\/$(date "+%Y\\/%m\\/%d")" | grep -o -E "https[^\\\"]+")
  42. found=0
  43. for link in $links; do # $links not quoted, because that makes it only one long string
  44.  page=$(wget -q -O - "$link")
  45.  # find current cases table, filter out descriptor, only take top value (yesterday), remove date, remove period
  46.  cases_hannover=$(ech "$page" | grep --after-context=1 -e "Aktuell Infizierte" -e "Aktuell registrierte Infizierte" | grep -o -E "[0-9]+\\.[0-9]+\\.\\: [0-9\\.]+" | head -n 1 | sed "s/[0-9]+\\.[0-9]+\\.\\: //" | sed "s/\\.//")
  47.  # find new cases sentence, extract number, remove period
  48.  new_cases_hannover=$(ech "$page" | grep -o -E "Das sind [0-9\\.]+ Fälle mehr als" | head -n 1 | grep -o -E "[0-9\\.]+" | sed "s/\\.//")
  49.  # find (total) deaths sentence, extract number, remove period
  50.  dead_hannover_now=$(ech "$page" | grep -o -E "[0-9]+ Menschen sind infolge einer nachgewiesenen oder mutmaßlichen Corona\\-Infektion in der Region verstorben" | grep -o -E "[0-9\\.]+" | sed "s/\\.//")
  51.  # find current cases Lehrte based on some super specific HTML formatting (may break any day), extract number, remove period
  52.  cases_lehrte=$(ech "$page" | grep -o -E "Lehrte<\\/p> *<\\/td> *<td> *<p class\\=\\\"bodytext\\\"> *[0-9\\.]+" | grep -o -E "[0-9\\.]+" | sed "s/\\.//")
  53.  # even more specific HTML formatting, wider range behind it, extract last/second number
  54.  total_lehrte=$(ech "$page" | grep -o -E "Lehrte<\\/p> *<\\/td> *<td> *<p class\\=\\\"bodytext\\\"> *[0-9]+ *<\\/p> *<\\/td> *<td> *<p class\\=\\\"bodytext\\\"> *[0-9]+" | grep -o -E "[0-9]+" | tail -n 1)
  55.  if [[ "$cases_hannover" != "" ]] && [[ "$new_cases_hannover" != "" ]] && [[ "$dead_hannover_now" != "" ]] && [[ "$cases_lehrte" != "" ]] && [[ "$total_lehrte" != "" ]]; then
  56.   found=1
  57.   # calculate differences from previous saved values
  58.   deaths_hannover=$(qalc -t "$dead_hannover_now-$(cat /home/fabian/misc/corona_storage | head -n 1)" | sed "s/−[0-9]+/0/" | sed "s/\"//g")
  59.   new_cases_lehrte=$(qalc -t "$total_lehrte-$(cat /home/fabian/misc/corona_storage | tail -n 1)" | sed "s/−[0-9]+/0/" | sed "s/\\\"//g")
  60.   break
  61.  fi
  62. done
  63. if [[ $found == 0 ]]; then
  64.  echo "Couldn't find/parse article, skipping. Links checked:\n$links"
  65.  dead_hannover_now=$(cat /home/fabian/misc/corona_storage | head -n 1)
  66.  total_lehrte=$(cat /home/fabian/misc/corona_storage | tail -n 1)
  67. fi
  68. ech "$dead_hannover_now\n$total_lehrte" > /home/fabian/misc/corona_storage
  69.  
  70. # vaccination
  71. page=$(wget -q -O - "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/vaccinations.csv")
  72. # get area-specific rows, start at 2021-01-15, extract 6th column
  73. vaccination_de=$(echo "$page" | grep "Germany" | tail -n +22 | sed "s/([^\,]+\,){5}([^\,]+)\,.+/\2/")
  74. vaccination_world=$(echo "$page" | grep "World" | tail -n +35 | sed "s/([^\,]+\,){5}([^\,]+)\,.+/\2/")
  75. vaccination_at=$(echo "$page" | grep "Austria" | tail -n +22 | sed "s/([^\,]+\,){5}([^\,]+)\,.+/\2/")
  76.  
  77. page=$(wget -q -O - "coronavirus.sachsen.de/ueberblick-coronaschutzimpfungen-in-sachsen-9874.html")
  78. vaccination_saxony=$(echo "$page" | grep "Zweitimpfungen" | tail -n 1 | grep -o -E "[0-9\\.]+" | sed "s/\\.//g")
  79. vaccination_north_saxony=$(echo "$page" | grep --after-context=2 "Nordsachsen" | grep -o -E "[0-9\\.]+" | tail -n 1 | sed "s/\\.//g")
  80.  
  81. # Germany
  82. page=$(wget -q -O - "worldometers.info/coronavirus/country/germany")
  83. cases_de=$(worldometers 101 "Currently infected")
  84. new_cases_de=$(worldometers 101 "New cases")
  85. deaths_de=$(worldometers 101 "Daily deaths")
  86.  
  87. # world
  88. page=$(wget -q -O - "worldometers.info/coronavirus")
  89. cases_world=$(worldometers 125 "Currently infected")
  90. new_cases_world=$(worldometers 125 "Daily cases") # yay for consistency
  91. deaths_world=$(worldometers 125 "Daily deaths")
  92.  
  93. # Austria
  94. page=$(wget -q -O - "worldometers.info/coronavirus/country/austria")
  95. cases_at=$(worldometers 101 "Currently infected")
  96. new_cases_at=$(worldometers 101 "New cases")
  97. deaths_at=$(worldometers 101 "Daily deaths")
  98.  
  99. echo "\nEnter to continue, Ctrl+C to abort"
  100. # open pages for manual checks
  101. weekday=$(qalc -t "weekday(today)" | sed "s/\\\"//g")
  102. if [[ $found == 0 ]] && [[ $weekday -lt 6 ]]; then
  103.  (firefox "www.altkreisblitz.de/region-hannover" &> /dev/null & disown)
  104. else
  105.  # focus latest console window, hopefully that's the one with this script
  106.  wmctrl -i -a $(wmctrl -l | grep "Konsole" | tail -n 1 | sed "s/ .+//")
  107. fi
  108. read
  109. wmctrl -a "corona - Google Sheets — Mozilla Firefox"
  110. sleep 1
  111.  
  112. insert_full "$vaccination_de"
  113. insert_one "$vaccination_saxony"
  114. insert_one "$vaccination_north_saxony"
  115. insert_full "$vaccination_world"
  116. insert_full "$vaccination_at"
  117. insert_full "$cases_de"
  118. insert_full "$new_cases_de"
  119. insert_full "$deaths_de"
  120. insert_full "$cases_world"
  121. insert_full "$new_cases_world"
  122. insert_full "$deaths_world"
  123. if [[ $found == 1 ]]; then
  124.  insert_one "$cases_hannover"
  125.  insert_one "$new_cases_hannover"
  126.  insert_one "$deaths_hannover"
  127.  insert_one "$cases_lehrte"
  128.  insert_one "$new_cases_lehrte"
  129. else
  130.  altkreis_assumption
  131.  insert_one "0"
  132.  insert_one "0"
  133.  altkreis_assumption
  134.  insert_one "0"
  135. fi
  136. insert_full "$cases_at"
  137. insert_full "$new_cases_at"
  138. insert_full "$deaths_at"
  139.  
  140. notify-send -t 10000 "done"
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×