LoginSignup
6
3

More than 3 years have passed since last update.

新型コロナの感染者数を表示するBOTを作る

Last updated at Posted at 2020-03-19

今回つくるもの

アルティメット優秀な人たちが

都道府県別新型コロナウイルス感染者数マップ
https://gis.jag-japan.com/covid19jp/

というサイトを作っておられますが、私はそこまでつよつよではないので、感染者数の内訳を表示するBOTを作ってみようと思いました。

同じサイトに感染者数のデータがjsonファイルで置いてあったので、そちらを使わせて頂こうと思います。

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

環境

  • windows10
  • discord.py 1.2.3
  • python 3.7.3

事前準備

discord.pyを用いたDiscord BOTの作成や基本は以下のサイトがわかりやすいです。

Pythonで簡単なDiscord Botの作り方
https://qiita.com/PinappleHunter/items/af4ccdbb04727437477f
Discord Bot 最速チュートリアル【Python&Heroku&GitHub】
https://qiita.com/1ntegrale9/items/aa4b373e8895273875a8

作成

まず上記のjsonファイルをダウンロードするプログラムを作ります。
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))

続いてBOT本体を作ります。

main.py
import download
import json
from collections import defaultdict
import discord

TOKEN = '任意のトークン'
CHANNEK_ID = '任意のチャンネルID'
client = discord.Client()

# 起動時に表示
@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

# メッセージを受け取った時
@client.event
async def on_message(message):
    # botからのメッセージは無視
    if message.author.bot:
        return
    if message.content.startswith("!count"):
        #jsonファイルをロード
        download.download()
        json_open = open('COVID-19_data.json', 'r', encoding="utf-8_sig")
        json_load = json.load(json_open)
        jsn = json_load

        #居住都道府県名と数をdefaultdictで保持
        properties = defaultdict(int)
        for f in jsn['features']:
            property = f['properties']['居住都道府県']
            if property == '中華人民共和国' or property == '調査中' or property == '不明':
                continue
            if property not in properties:
                properties[property] = 0
            properties[property] += 1
        #一行ずつ出力すると時間がかかるので出力内容をあらかじめ保持
        say = ''
        for p in properties:
            say += p + ' ' + str(properties[p]) + '\n'
        await message.channel.send(say)

client.run(TOKEN)

以上が新型コロナウイルスの各都道府県の感染者数を表示するBOTのプログラムです。

結果

デター!!!!
covid bot.PNG

実はもう少し出力されているのですが、キャプチャサイズの関係で入りきりませんでした。ご了承ください。

おわりに

思いつきでざっと作ったものなので、ミスやスマートにできる場所があるかもしれません。その際はご指摘いただけると幸いです。
まだまだ新型コロナウイルスの猛威は止まりません。皆様お気をつけてお過ごしください。

Twitter
https://twitter.com/hasegawa2718

6
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3