diff --git a/.github/workflows/send-message.yml b/.github/workflows/send-message.yml index d2f49a1..9375484 100644 --- a/.github/workflows/send-message.yml +++ b/.github/workflows/send-message.yml @@ -1,4 +1,4 @@ -name: 'GitHub Action for COVID-19 Updates' +name: 'GitHub Action for COVID-19 Update' on: push: @@ -19,3 +19,6 @@ jobs: env: TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }} + URL: 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/' + CSV: '.csv' + COUNTRY: 'Malaysia' diff --git a/README.md b/README.md index 1cbd0f2..58e16a7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,199 @@ -# Covid-19 Updates Bot -Get Malaysia Covid-19 Updates. Data from 2019 Novel Coronavirus COVID-19 (2019-nCoV) Data Repository by Johns Hopkins CSSE, https://github.com/CSSEGISandData/COVID-19. Updates will be sent via Telegram. +# Covid-19 Update Bot +## Description +Get Covid-19 Update by country. Data from 2019 Novel Coronavirus COVID-19 (2019-nCoV) Data Repository by Johns Hopkins CSSE, https://github.com/CSSEGISandData/COVID-19. Update will be sent via Telegram. + +## Example Usage +Update `send-message.yml` as below; +```YAML +... + - cron: '0 12 * * *' + +... + env: + TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} + TELEGRAM_TOKEN: ${{ secrets.TELEGRAM_TOKEN }} +... + COUNTRY: 'Malaysia' +``` + +## List of Countries +Country | +:---| +Afghanistan +Albania +Algeria +Andorra +Angola +Antigua and Barbuda +Argentina +Armenia +Australia +Austria +Azerbaijan +Bahamas +Bahrain +Bangladesh +Barbados +Belarus +Belgium +Belize +Benin +Bhutan +Bolivia +Bosnia and Herzegovina +Botswana +Brazil +Brunei +Bulgaria +Burkina Faso +Burma +Cabo Verde +Cambodia +Cameroon +Canada +Central African Republic +Chad +Chile +China +Colombia +Congo (Brazzaville) +Congo (Kinshasa) +Costa Rica +Cote d'Ivoire +Croatia +Cuba +Cyprus +Czechia +Denmark +Diamond Princess +Djibouti +Dominica +Dominican Republic +Ecuador +Egypt +El Salvador +Equatorial Guinea +Eritrea +Estonia +Eswatini +Ethiopia +Fiji +Finland +France +Gabon +Gambia +Georgia +Germany +Ghana +Greece +Grenada +Guatemala +Guinea +Guinea-Bissau +Guyana +Haiti +Holy See +Honduras +Hungary +Iceland +India +Indonesia +Iran +Iraq +Ireland +Israel +Italy +Jamaica +Japan +Jordan +Kazakhstan +Kenya +Korea, South +Kosovo +Kuwait +Kyrgyzstan +Laos +Latvia +Lebanon +Liberia +Libya +Liechtenstein +Lithuania +Luxembourg +Madagascar +Malaysia +Maldives +Mali +Malta +Mauritania +Mauritius +Mexico +Moldova +Monaco +Mongolia +Montenegro +Morocco +Mozambique +MS Zaandam +Namibia +Nepal +Netherlands +New Zealand +Nicaragua +Niger +Nigeria +North Macedonia +Norway +Oman +Pakistan +Panama +Papua New Guinea +Paraguay +Peru +Philippines +Poland +Portugal +Qatar +Romania +Russia +Rwanda +Saint Kitts and Nevis +Saint Lucia +Saint Vincent and the Grenadines +San Marino +Saudi Arabia +Senegal +Serbia +Seychelles +Singapore +Slovakia +Slovenia +Somalia +South Africa +Spain +Sri Lanka +Sudan +Suriname +Sweden +Switzerland +Syria +Taiwan* +Tanzania +Thailand +Timor-Leste +Togo +Trinidad and Tobago +Tunisia +Turkey +Uganda +Ukraine +United Arab Emirates +United Kingdom +Uruguay +US +Uzbekistan +Venezuela +Vietnam +West Bank and Gaza +Zambia +Zimbabwe \ No newline at end of file diff --git a/action.yml b/action.yml index ad879bd..b887851 100644 --- a/action.yml +++ b/action.yml @@ -1,5 +1,5 @@ name: 'GitHub Action for COVID-19 Updates' -description: 'Get Malaysian Covid-19 Updates. Data from 2019 Novel Coronavirus COVID-19 (2019-nCoV) Data Repository by Johns Hopkins CSSE.' +description: 'Get Covid-19 Update by country. Data from 2019 Novel Coronavirus COVID-19 (2019-nCoV) Data Repository by Johns Hopkins CSSE.' inputs: who-to-greet: # id of input description: 'Yesterday date' @@ -7,7 +7,7 @@ inputs: default: 'Yesterday date' outputs: time: # id of output - description: 'COVID-19 Updates on confirmed, deaths, recovered and active.' + description: 'COVID-19 Update on confirmed, deaths, recovered and active.' runs: using: 'node12' main: 'index.js' \ No newline at end of file diff --git a/index.js b/index.js index ddbbbc4..ed10746 100644 --- a/index.js +++ b/index.js @@ -1,13 +1,13 @@ require('dotenv').config(); const core = require('@actions/core'); -const github = require('@actions/github'); const { StringStream } = require('scramjet'); const request = require('request'); const Telegram = require('node-telegram-bot-api'); const bot = new Telegram(process.env.TELEGRAM_TOKEN); -const URL = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/'; -const CSV = '.csv'; +const URL = process.env.URL.toString(); +const CSV = process.env.CSV.toString(); +const COUNTRY = process.env.COUNTRY.toString(); try { const today = new Date(); @@ -21,22 +21,42 @@ try { formattedYesterdayDate = getMonth + '-' + yesterday.getDate() + '-' + yesterday.getFullYear(); } - const generateMessage = object => - `Malaysia COVID-19 Updates as ${yesterday.toDateString()}; - Confirmed: ${object.Confirmed}. - Deaths: ${object.Deaths}. - Recovered: ${object.Recovered}. - Active: ${object.Active}.`; + const generateMessage = (country, totalConfirmed, totalDeaths, totalRecovered, totalActive) => + `${country} COVID-19 Update as ${yesterday.toDateString()}; + Total Confirmed: ${totalConfirmed}. + Total Deaths: ${totalDeaths}. + Total Recovered: ${totalRecovered}. + Total Active: ${totalActive}.`; + + async function getResult(country) { + let arr = []; + await request + .get(URL + formattedYesterdayDate + CSV) + .pipe(new StringStream()) + .CSVParse({ skipEmptyLines: true, header: true }) + .filter(data => (data.Country_Region === country)) + .consume(data => arr.push(data)); + return arr; + } - // TODO: Get country region from configuration - request.get(URL + formattedYesterdayDate + CSV) - .pipe(new StringStream()) - .CSVParse({ skipEmptyLines: true, header: true }) - .filter(object => (object.Country_Region === 'Malaysia')) - .map(async(object) => { - const message = generateMessage(object); + getResult(COUNTRY) + .then(result => { + let totalConfirmed = 0; + let totalDeaths = 0; + let totalRecovered = 0; + let totalActive = 0; + + result.forEach(element => { + totalConfirmed = totalConfirmed + parseInt(element.Confirmed); + totalDeaths = totalDeaths + parseInt(element.Deaths); + totalRecovered = totalRecovered + parseInt(element.Recovered); + totalActive = totalActive + parseInt(element.Active); + }); + + const message = generateMessage(COUNTRY, totalConfirmed, totalDeaths, totalRecovered, totalActive); bot.sendMessage(process.env.TELEGRAM_CHAT_ID, message); }); + } catch (error) { core.setFailed(error.message); } \ No newline at end of file diff --git a/package.json b/package.json index 7e6778d..ea0a6c9 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,6 @@ "homepage": "https://github.com/emafazillah/covid19-updates-bot#readme", "dependencies": { "@actions/core": "^1.2.3", - "@actions/github": "^2.1.1", "dotenv": "^8.2.0", "node-telegram-bot-api": "^0.40.0", "request": "^2.88.2",