[PYTHON] Create a BOT that displays the number of infected people in the new corona

What to make this time

Ultimate talented people

Map of the number of people infected with the new coronavirus by prefecture https://gis.jag-japan.com/covid19jp/

I have created a site called, but I am not so enthusiastic, so I decided to create a BOT that displays the breakdown of the number of infected people.

The data of the number of infected people was put in the json file on the same site, so I will use that.

https://services6.arcgis.com/5jNaHNYe2AnnqRnS/arcgis/rest/services/COVID19_Japan/FeatureServer/0/query?where=%E9%80%9A%E3%81%97%3E0&returnIdsOnly=false&returnCountOnly=false&&f=pgeojson&outFields=*&orderByFields=%E9%80%9A%E3%81%97

environment

Advance preparation

The following sites are easy to understand for creating and basics of Discord BOT using discord.py.

How to make a simple Discord Bot in Python https://qiita.com/PinappleHunter/items/af4ccdbb04727437477f Discord Bot Fastest Tutorial [Python & Heroku & GitHub] https://qiita.com/1ntegrale9/items/aa4b373e8895273875a8

Create

First, create a program to download the above json file. You can easily download it using ** urllib **.

download.py


import urllib.request

def download():
    url = 'https://services6.arcgis.com/5jNaHNYe2AnnqRnS/arcgis/rest/services/COVID19_Japan/FeatureServer/0/query?where=%E9%80%9A%E3%81%97%3E0&returnIdsOnly=false&returnCountOnly=false&&f=pgeojson&outFields=*&orderByFields=%E9%80%9A%E3%81%97'
    title = 'COVID-19_data.json'
    urllib.request.urlretrieve(url, "{0}".format(title))

Next, make the BOT body.

main.py


import download
import json
from collections import defaultdict
import discord

TOKEN = 'Any token'
CHANNEK_ID = 'Any channel ID'
client = discord.Client()

#Displayed at startup
@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

#When you receive the message
@client.event
async def on_message(message):
    #Ignore messages from bots
    if message.author.bot:
        return
    if message.content.startswith("!count"):
        #Load json file
        download.download()
        json_open = open('COVID-19_data.json', 'r', encoding="utf-8_sig")
        json_load = json.load(json_open)
        jsn = json_load
        
        #Keep the name and number of prefectures of residence in defaultdict
        properties = defaultdict(int)
        for f in jsn['features']:
            property = f['properties']['Prefecture of residence']
            if property == 'People's Republic of China' or property == 'investigating' or property == 'unknown':
                continue
            if property not in properties:
                properties[property] = 0
            properties[property] += 1
        #It takes time to output line by line, so the output contents are saved in advance.
        say = ''
        for p in properties:
            say += p + ' ' + str(properties[p]) + '\n'
        await message.channel.send(say)

client.run(TOKEN)

The above is the BOT program that displays the number of people infected with the new coronavirus in each prefecture.

result

Data !!!! covid bot.PNG

Actually, it was output a little more, but it could not fit due to the capture size. Please note.

in conclusion

It's a rough idea, so there may be places where you can make mistakes and be smart. In that case, I would appreciate it if you could point out. The violence of the new coronavirus is still unstoppable. Please be careful.

Twitter https://twitter.com/hasegawa2718

Recommended Posts

Create a BOT that displays the number of infected people in the new corona
Create a bot that posts the number of people positive for the new coronavirus in Tokyo to Slack
I created a Discord bot on Docker that reports the number of corona infected people in Tokyo at a specified time.
I tried to predict the number of domestically infected people of the new corona with a mathematical model
A server that returns the number of people in front of the camera with bottle.py and OpenCV
The story of creating a bot that displays active members in a specific channel of slack with python
Find the number of days in a month
[Python] A program that counts the number of valleys
Make a BOT that shortens the URL of Discord
Predict the number of people infected with COVID-19 with Prophet
[Python] A program that finds the shortest number of steps in a game that crosses clouds
Posted the number of new corona positives in Tokyo to Slack (deployed on Heroku)
Analyzing data on the number of corona patients in Japan
Let's visualize the number of people infected with coronavirus with matplotlib
Create a new list by combining duplicate elements in the list
Create a bot that only returns the result of morphological analysis with MeCab on Discord
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
Create a LINE Bot in Django
A function that measures the processing time of a method in python
I made a slack bot that notifies me of the temperature
Create a function to get the contents of the database in Go
Get the number of readers of a treatise on Mendeley in Python
A programming language that young people will need in the future
A story that verified whether the number of coronas is really increasing rapidly among young people
[Python] A program that calculates the number of chocolate segments that meet the conditions
[Python] A program that calculates the number of socks to be paired
Generate a list packed with the number of days in the current month.
Check the in-memory bytes of a floating point number float in Python
Considering the situation in Japan by statistician Nate Silver, "The number of people infected with coronavirus is meaningless"
Test & Debug Tips: Create a file of the specified size in Python
Create a new dict that combines dicts
How to create a wrapper that preserves the signature of the function to wrap
Create a bot that boosts Twitter trends
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
I tried to predict the number of people infected with coronavirus in Japan by the method of the latest paper in China
I tried to predict the number of people infected with coronavirus in consideration of the effect of refraining from going out
Create a simple app that incorporates the Fetch API of Ajax requests in Flask and explain it quickly
A collection of Numpy, Pandas Tips that are often used in the field
Set the number of elements in a NumPy one-dimensional array to a power of 2 (0 padded)
Test the number of times you have thrown a query (sql) in django
[Django] Create a form that automatically fills in the address from the zip code
[Python] A program that calculates the number of updates of the highest and lowest records
[Discode Bot] I created a bot that tells me the race value of Pokemon
The story of making a Line Bot that tells us the schedule of competitive programming
Get the caller of a function in Python
Make a copy of the list in Python
Create a new page in confluence with Python
Output in the form of a python array
The one that displays the progress bar in Python
Until you create a new app in Django
Factfulness of the new coronavirus seen in Splunk
A shell program that displays the Fibonacci sequence
A python script that gets the number of jobs for a specified condition from indeed.com
How to identify the element with the smallest number of characters in a Python list?
A memo that implements the job of loading a GCS file into BigQuery in Python
A script that can perform stress tests according to the number of CPU cores
I tried to summarize the new coronavirus infected people in Ichikawa City, Chiba Prefecture
Find out the maximum number of characters in multi-line text stored in a data frame
Let's put out a ranking of the number of effective reproductions of the new coronavirus by prefecture
[Python] Representing the number of complaints from life insurance companies in a bar graph
It seems that a new lottery with a total score will start in the sports lottery,