Categories
How To Raspberry Pi

Coronavirus — beware of malware — better make own update ticker #MonthOfMaking

Not a day goes by that I don’t get a newsletter with warnings about malware related to the coronavirus SARS-CoV-2, which causes the lung disease COVID-19, but everyone should be aware of that by now. Cybercriminals are taking advantage, and that too is well known. Especially coronavirus tickers with malware are on the rise. The apps sometimes even show important infection numbers, but they collect data in the background. There are also various blackmailer apps in circulation that block your device and then demand a ransom. None of the scams are really new, except that the topic has just changed to Coronavirus.

Of course many people want to stay up to date and get information. First and foremost, I want to know whether the numbers are rising or falling. I have already made a Coronavirus ticker for my Sense Hat and showed you how to get the numbers on your Raspberry Pi. If you want to follow this guide all other Linux distributions or systems running Python will work, too.

Here is a special article about how you can easily create your own ticker with Raspberry Pi, Python, Apprise, Pushover, E-Mail or Slack. The script queries the worldwide data and then a few countries. You can easily add more or less countries.

Stay at home and build something

If you have a Raspberry Pi or another Linux instance, you can easily create your coronavirus ticker. Maybe it will help to overcome some boredom at home. A busy brain is better than a bored one.

Note: This article was written over a few days. Since the numbers are increasing so rapidly, the screenshots seem to be outdated. But even this shows how serious the situation is. Protect yourself and others! Keep your distance — stay at home (and go jogging every 2nd day, if that is still allowed)!

Preparations

I assume a Raspberry Pi with Raspbian Buster installed and up to date. How you send the message to your smartphone is up to you. I use Apprise and Pushover. The former is free, the latter costs about $5 per platform and you can send 7500 messages per month. If you don’t want to do that, you can also send an email or use many other services with Apprise. Here I show you the methods with Pushover and email, and at the end Slack without Apprise.

If you install Pushover via Google Play Store, the first 7 days are free. Then you can test whether you like it or not. After registration, you open the website and find your User Key.

Pushover User Key
Pushover User Key

Afterwards, you create an application (Create an Application / API Token) at the bottom of the page and receive a token. You will need this later in the script.

Create an application – for example Coronavirus Update
Create an application – for example Coronavirus Update

If you create an application, you may also upload an image. You will see this later in my screenshots. If you use Pushover also for other projects, like a Bitcoin ticker, a visual reference is certainly nice.

Cfscrape prevents the Cloudflare bot from interfering and wanting a confirmation that I’m human. I don’t know if I’d need that with the URLs here, but I always use it now. The behavior of websites and APIs can always change, especially when there are many requests. This method seems safer to me.

Apprise and cfscrape are installed via pip. The latter we have to install under Raspbian first. These three commands do the necessary software installation:

sudo apt install python3-pip
pip3 install apprise
pip3 install cfscrape

Now the system is prepared and we can start working on the script.

Python script for the Coronavirus updates

The script looks like this, I will explain what it does afterwards. I have named my script coronavirus-ticker.py:

#!/usr/bin/python3

import json
import cfscrape
import apprise

scraper = cfscrape.create_scraper()

apobj = apprise.Apprise()
# Here the key and token – the @ is important!
apobj.add('pover://Apprise-Key@ApplicationToken')

# Another instance for email notifications, here GMAIL
apobj.add('mailto://ADRESS:PASSWORD@gmail.com')

# Now the message is also sent via GMAIL

# Worldwide data
urltotal = 'https://covid19.mathdro.id/api'

# Data for the countries / regions
urlcountry = 'https://covid19.mathdro.id/api/confirmed'

def check_data(url, country):
    cfurl = scraper.get(url).content
    data = json.loads(cfurl)
    global message
    if country == 'world':
        infected = data['confirmed']['value']
        recovered = data['recovered']['value']
        deaths = data['recovered']['value']
        message = 'World || Infected: ' + str(infected) + ' | Cured: ' + str(recovered) + ' | Deaths: ' + str(deaths);
    else:
        for val in data:
            if val['countryRegion'] == country:

                infected = val['confirmed']
                recovered = val['recovered']
                deaths = val['deaths']
                countrycode = val['provinceState']
                # Messages append
                message = message + '\n' + country + ' (' + str(countrycode) + ') || Infected: ' + str(infected) + ' | Cured: ' + str(recovered) + ' | Deaths: ' + str(deaths);

check_data(urltotal, country='world')
check_data(urlcountry, country='United Kingdom')
check_data(urlcountry, country='Spain')
check_data(urlcountry, country='Italy')


apobj.notify(
   body=message,
   title='Coronavirus-Update!',
)

The script uses the two URLs https://covid19.mathdro.id/api and https://covid19.mathdro.id/api/confirmed. The first address gives me the worldwide data. The second URL contains data for the individual countries and/or regions. If you want to query many countries, it is certainly more efficient to submit a list and not to call the function for each country separately. But with my three countries this is too much effort for now.

For the messages, I just let the results append to each other, but insert a line break. The \n is responsible for that.

I get the names for the countries from the second URL.

Coronavirus data from the UK
Coronavirus data from the UK

I could also take the values iso2 or iso3. Maybe the country code would be nicer, but you can decide that yourself and adjust it accordingly. Because United Kingdom gives several results I inculde the provinceState in my code. You have to be careful that iso2 and iso3 are not set for all countries. Just check first which data you want to have.

With Apprise you can create as many instances as you want to send a notification to. In my script there is also a Gmail address as an example. Apprise supports many services. You can find the long list here. Now you can decide how you want to be notified. If you use a multi-factor authentication process at Google, you first have to create an app password.

Coronavirus update via email
Coronavirus update via email

I do have a cosmetic problem, though as you see in the email screenshot. If I use \n the e-mail does not show me any new lines. If I use <br> instead, new lines in the email would work, but not in the Android app. I’m satisfied with the coronavirus update on my smartphone and therefore I simply omit email. Less is more and in this case a good solution. 🙂

Make script executable and create cronjob

I could call the script with python3. However, I put in the first line of the file and tell the script what to do. So, I can simply make the file executable and execute it from the terminal.

chmod +x ./coronavirus-ticker.py

A so-called cronjob is responsible for automation. The command

crontab -e

opens the crontab and to be honest, two updates a day is enough for me. The following line executes the script at 8 am and 6 pm.

0 8,18 * * * /home/pi/coronavirus-ticker.py >> /dev/null 2>&1

If you prefer an update every 3 hours:

0 */3 * * * /home/pi/coronavirus-ticker.py >> /dev/null 2>&1

During development, I ran the script more often to check if it works.

This is how the coronavirus update looks like in Android

If the script is executed, I get the update to the coronavirus numbers delivered directly to the locked screen. This looks like the following:

Update for the Coronavirus numbers
Update for the Coronavirus numbers

In the Pushover App itself many messages are stored. You can set yourself when the messages should be deleted.

When I only followed the news, I didn’t even realize how fast the numbers are increasing. When I have the numbers in front of me, I actually get a bit queasy. These are now also only the known cases. I don’t even want to think about the hidden numbers.

The virus is spreading fast
The virus is spreading fast

Anyone who wonders whether the data is reliable. It’s from John Hopkins University, so yes. Of course, it always depends on the countries themselves how accurate the data from their health ministries are.

It also works with Slack and without Apprise and Pushover

If you are not keen on using Pushover you can also use Slack. It’s free of charge. So far, I have had no problems with Pushover and the notifications come straight away.

With Slack I have made previously experience that sometimes I did not get any message at all. But I did not have to work with it for very long. That is why I have looked again into Slack and using that neither Apprise nor Pushover is necessary.

There are several methods how you can send messages to Slack. I used a webhook for this but you have to create it first. You may also choose a channel to post to. Important is the webhook URL to which you can send JSON payloads. I created the webhook and also a channel called #coronavirus-ticker.

You can now use the corresponding parameters in your Python script. The changes look like this:

import requests

…

# At the end of the script
headers = {'Content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
dataslack = '{"channel": "#coronavirus-ticker", "text": "' + message + '"}'
response = requests.post('https://hooks.slack.com/services/YOUR-WEBHOOK', headers=headers, data=dataslack)

Afterwards, you adjust the script accordingly. You must import requests at the beginning of the script.

You can omit the lines starting with apobj. This also applies to the last four lines that send a message via Apprise. To use Slack, add the three lines with headers, dataslack and response at the end of the script, as shown in the code block above.

My script would send the following message to the channel #coronavirus-ticker.

The notification via Smartphone app looks like this:

Slack App on the smartphone also works as a coronavirus ticker
Slack App on the smartphone also works as a coronavirus ticker

I want to stay informed in any case, but for that I don’t have to install a special app for the current figures regarding coronavirus. This will also help me avoid malware on my smartphone and other devices.

Further improvements to your coronavirus ticker

Maybe you can further customize and improve the script. It is possible that the numbers from the last request will be saved and you will see a difference.

Maybe you want to send a list to the function because you want to see more countries. If you want to have all numbers from the USA, you would have to add them up because the USA is listed as states.

Stay healthy and let’s hope the scientists find a cure to COVID-19 rapidly.

Leave a Reply

Your email address will not be published. Required fields are marked *