Tweet image of stats of covid19
This is a small personal project which tweets details about the current stats of covid19 in Mauritius by writing the stats received from https://github.com/CSSEGISandData/COVID-19/ on one of my image that I have uploaded on Google Photos.
The flow is programmed on NodeRed flows and triggered everyday a specific time.
The codes is available on gitlab https://gitlab.com/appadudevraj7/tweet-covidpictures-mru
The Scripts and Node red is on my Raspberry PI running the Raspbian Buster.
Pre-requisites
- NodeRed (with twitter module)
- Bash script
- Imagemagick
- A twitter API Token
Get Data from CSSEGISandData Github
Everyday at 23h55 GMT+0, the data in csv format is uploaded on https://github.com/CSSEGISandData/COVID-19/tree/master/csse_covid_19_data/csse_covid_19_daily_reports with name like “04-08-2020.csv“
My first flow is to retrieve the data with the updated csv file based on previous date and is triggered at 01 AM.
– Create the URL of the daily report with previous date
– Download the CSV file using the http get request
– Convert the Result into csv
– Save the file as it contains data for all countries
– I have used a loop to get values for Mauritius only and save the details in json format on a file
A snippet of the Javascript to get details about mauritius
var data = msg.payload;
var confirmed,recovered, deaths;
for(var i = 0; i < data.length; i++)
{
if(data[i]['Country_Region'] == 'Mauritius')
{
confirmed = data[i]['Confirmed'];
recovered = data[i]['Recovered'];
deaths = data[i]['Deaths'];
}
}
msg.payload = '{"latest":{"confirmed": ' + confirmed + ',"deaths": ' + deaths + ' ,"recovered": ' + recovered + '}}' ;
return msg;
Prepare a custom Get Request to get values for mauritius
I have expose a url https://url.com/getcovid that will retrieve data of mauritius from the json file for Mauritius only
Prepare Image with current Statistics
Imagemagick is of great help to allow image manipulation
Steps:
1 . Get the URL of image from my API (Images from Google Photos)
2. Download the image on a path
3. Get http request on /getcovid on my node red to get the values
4. Use Imagemagick to write on the Image
5. Save the image on another location
https://gitlab.com/appadudevraj7/tweet-covidpictures-mru/-/blob/master/imagegen.sh
Launch full flow to tweet image
- Executes the image script to get the picture with stats on it
- Get the image location and copy the file to Binary on Nodered
- To post image on twitter, it uses the msg.media, the next node is to switch msg.payload to msg.media
- Query the /covid on nodered to get stats that will be used as text post of twitter
- Put all the Post together along with the picture (msg.media for image and msg.payload for the text tweet)
- Use the twitter node to post