~michalr/telegram-covid-bot

84041aa4b708190674d2128783103ad9b842516c — MichaƂ Rudowicz 4 years ago 5ea27da
Switched from covid19api.com to JHU CSSE Git Repo
3 files changed, 33 insertions(+), 6 deletions(-)

M README.md
M bot.rb
M config.example.yml
M README.md => README.md +1 -1
@@ 1,6 1,6 @@
# Coronavirus Telegram bot for covid19api.com

Simple Ruby bot for fetching the data from covid19api.com and pushing to a specified Telegram
Simple Ruby bot for fetching the data from [JHU CSSE COVID-19 Repo](https://github.com/CSSEGISandData/COVID-19/) and pushing to a specified Telegram
group or channel.

Stores previous data in `previous.json` for comparison and to ignore the data that has not changed.

M bot.rb => bot.rb +31 -4
@@ 1,6 1,8 @@
require 'yaml'
require 'json'
require 'net/http'
require 'date'
require 'csv'

unless ARGV.length == 2 then
    puts "Usage: bot.rb [path-to-config.yml] [path-to-previous-data.json]"


@@ 40,11 42,36 @@ def s(value)
    "#{value_sign}#{value}"
end

raw_json = Net::HTTP.get(URI("https://api.covid19api.com/summary"))
# int -> string with Two Digits
def td(value)
    return "#{value}" if value > 9
    return "0#{value}"
end

def get_current_data(for_when)
    data_uri = URI("https://raw.githubusercontent.com/CSSEGISandData/"\
        "COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/"\
        "#{td(for_when.month)}-#{td(for_when.day)}-#{for_when.year}.csv")

    raw_csv = Net::HTTP.get(data_uri)

    current_from_csv = CSV.parse(raw_csv, :headers => true)
        .select { |e| e['Country_Region'] == Config['CountryCode']}
        .first

    {
        "Country" => current_from_csv['Country_Region'],
        "CountryCode" => current_from_csv['Country_Region'],
        "Slug" => current_from_csv['Country_Region'],
        "TotalConfirmed" => current_from_csv['Confirmed'].to_i,
        "TotalDeaths" => current_from_csv['Deaths'].to_i,
        "TotalRecovered" => current_from_csv['Recovered'].to_i,
        "Date" => current_from_csv['Last_Update']
    }
end

current_data = JSON.parse(raw_json)['Countries']
    .select { |e| e['CountryCode'] == Config['CountryCode'] }
    .first
yesterday = Date.today - 1
current_data = get_current_data(yesterday)

unless current_data.reject { |k,_| k == 'Date' } == PreviousData.reject { |k,_| k == 'Date' } then
    previous_data = PreviousData unless PreviousData.empty?

M config.example.yml => config.example.yml +1 -1
@@ 1,4 1,4 @@
CountryCode: 'PL'
CountryCode: 'Poland'
TelegramToken: 'Your telegram token obtained from @BotFather'
TelegramChatId: '@your-channel-id'
Translation: