[Python] Create a script that uses FeedParser and LINE Notify to notify LINE of the latest information on the new coronavirus of the Ministry of Health, Labor and Welfare.

Trigger

I was watching the news about the new coronavirus every day and wondered if there was anything I could do as an engineer using some technology. For coronavirus related information, I was looking at the Ministry of Health, Labor and Welfare website as the primary information. I noticed that there was an RSS feed](https://www.mhlw.go.jp/stf/news.rdf) and was wondering if I could use it.

Therefore, I decided to create a script that LINE notifies the URL of the latest information of the Ministry of Health, Labor and Welfare using Python, which I recently started learning. I'm studying Python, so I hope you'll take it for granted. The environment is Windows10, Python3.8, but I think that there is no problem if it is a Mac or Python3 system.

Advance preparation

Install library

Install the required libraries with pip.

$ pip install feedparser requests

Get LINE Notify tokens

From here, get a token to notify LINE.

You can get LINE Notify tokens quickly by referring to here. [Super easy] Try using LINE notify

Access RSS and get the latest URL

Write a script to access RSS and get the latest URL. The very first link that contains the string newpage seems to be the latest information, so create a method that returns this.

#Access RSS and get the latest URL
def getRssFeedData():
    #Describe the URL of rdf to access
    RSS_URL = 'https://www.mhlw.go.jp/stf/news.rdf'
    xml = feedparser.parse(RSS_URL)
    for entry in xml.entries:
        #Extract the first URL of the new page from the link
        if('newpage' in entry.link):
            print(entry.link)
            return entry.link

Determine if the retrieved URL is up to date

Determine if the URL you fetched is up to date. Here, it is assumed that the method of ↑ is always up to date. Record the URL in a local file (latest_url.txt), compare the URL of the local file with the URL obtained from RSS, and if they are different, LINE notification will be sent as the latest information.

#Check for new information
def checkLatestNews():
    rss_url = getRssFeedData()
    path = './latest_url.txt'
 
    # latest_url.If there is no txt, create a new one
    if not os.path.isfile(path):
        string = 'new file'
        with open(path, mode='w') as file:
            file.write(string)
 
    local_url = ''
    with open(path, mode='r') as file:
        local_url = file.read()
 
    #Check for new information
    if (local_url == rss_url):
        print('There was no new information')
    else:
        with open(path, mode='w') as file:
            string = rss_url
            file.write(string)
        print('There was new information')

Notify LINE when there is new information

Finally, create a process to notify LINE when there is new information. Receive the URL of what's new and notify LINE.

#Method to notify LINE
def lineNotify(url):
    #List the issued token
    LINE_NOTIFY_TOKEN = '1234567890abcdefghijklmnopqrstuvwxyz'
 
    #Enter the API URL of LINE Notify
    LINE_NOTIFY_API = 'https://notify-api.line.me/api/notify'
 
    message = '\n There is the latest information from the Ministry of Health, Labor and Welfare.\n' + url
    payload = {'message': message}
    headers = {'Authorization': 'Bearer ' + LINE_NOTIFY_TOKEN }
 
    #Make a LINE notification
    requests.post(LINE_NOTIFY_API, data=payload, headers=headers)

Have them check regularly

Let's check it every 5 minutes as if it were run locally.

while True:
    checkLatestNews()
    #Check every 5 minutes * Change as appropriate
    sleep(300)

Connect each process

Connect the processing so far.

line_notify.py


import requests
import feedparser
from time import sleep
import os
 
#Access RSS and get the latest URL
def getRssFeedData():
    #Describe the URL of rdf to access
    RSS_URL = 'https://www.mhlw.go.jp/stf/news.rdf'
    xml = feedparser.parse(RSS_URL)
    for entry in xml.entries:
        #Extract the first URL of the new page from the link
        if('newpage' in entry.link):
            print(entry.link)
            return entry.link
 
#Method to notify LINE
def lineNotify(url):
    #List the issued token
    LINE_NOTIFY_TOKEN = '1234567890abcdefghijklmnopqrstuvwxyz'
 
    #Enter the API URL of LINE Notify
    LINE_NOTIFY_API = 'https://notify-api.line.me/api/notify'
 
    message = '\n There is the latest information from the Ministry of Health, Labor and Welfare.\n' + url
    payload = {'message': message}
    headers = {'Authorization': 'Bearer ' + LINE_NOTIFY_TOKEN }
 
    #Make a LINE notification
    requests.post(LINE_NOTIFY_API, data=payload, headers=headers)
 
#Check if the URL has been updated
def checkLatestNews():
    rss_url = getRssFeedData()
    path = './latest_url.txt'
 
    # latest_url.If there is no txt, create a new one
    if not os.path.isfile(path):
        string = 'new file'
        with open(path, mode='w') as file:
            file.write(string)
 
    local_url = ''
    with open(path, mode='r') as file:
        local_url = file.read()
 
    #Check for new information
    if (local_url == rss_url):
        print('There was no new information')
    else:
        #LINE notification
        lineNotify(rss_url)
        with open(path, mode='w') as file:
            string = rss_url
            file.write(string)
        print('I notified you because there was new information')
    
while True:
    checkLatestNews()
    #Check every 5 minutes * Change as appropriate
    sleep(300)

Complete! ✨ (Kimagure Cook style)

Run locally.

$ python line_notify.py

https://www.mhlw.go.jp/stf/seisakunitsuite/newpage_00016.html
I notified you because there was new information
https://www.mhlw.go.jp/stf/seisakunitsuite/newpage_00016.html
There was no new information
https://www.mhlw.go.jp/stf/seisakunitsuite/newpage_00016.html
There was no new information
https://www.mhlw.go.jp/stf/seisakunitsuite/newpage_00016.html
There was no new information
・ ・ ・

I got a notification. LINE.PNG

Improvement

I think there is still room for improvement, such as raising it to Lambda and moving environment variables to the env file. I want to raise it to Lambda and have it run regularly.

Recommended Posts

[Python] Create a script that uses FeedParser and LINE Notify to notify LINE of the latest information on the new coronavirus of the Ministry of Health, Labor and Welfare.
[Python] Automatically read prefectural information on the new coronavirus from the PDF of the Ministry of Health, Labor and Welfare and write it in a spreadsheet or Excel.
Use AWS lambda to scrape the news and notify LINE of updates on a regular basis [python]
[Python] About creating a tool to create a new Outlook email based on the data of the JSON file and the part that got caught
Data Langling PDF on the outbreak of influenza by the Ministry of Health, Labor and Welfare
Data cleansing of open data of the occurrence situation of the Ministry of Health, Labor and Welfare
Create a bot that posts the number of people positive for the new coronavirus in Tokyo to Slack
I tried to automatically send the literature of the new coronavirus to LINE with Python
Convert PDF of the situation of people infected in Tokyo with the new coronavirus infection of the Tokyo Metropolitan Health and Welfare Bureau to CSV
A script that makes it easy to create rich menus with the LINE Messaging API
Scraping PDF of the national list of minimum wages by region of the Ministry of Health, Labor and Welfare
I tried to create a Python script to get the value of a cell in Microsoft Excel
A Python script that allows you to check the status of the server from your browser
[Python] A program to find the number of apples and oranges that can be harvested
A Python script that compares the contents of two directories
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
[Python] I tried to make a simple program that works on the command line using argparse.
[Python] Let LINE notify you of the ranking of search results on your site on a daily basis.
A story that struggled to handle the Python package of PocketSphinx
[Python] LINE notification of the latest information using Twitter automatic search
A script that returns 0, 1 attached to the first Python prime number
A python script that deletes ._DS_Store and ._ * files created on Mac
[python] A note that started to understand the behavior of matplotlib.pyplot
[Python] A program that rotates the contents of the list to the left
[Python / Jupyter] Translate the comment of the program copied to the clipboard and insert it in a new cell
I made a POST script to create an issue on Github and register it in the Project
[Python] A program that calculates the number of socks to be paired
[Python] Create a linebot to write a name and age on an image
How to create a wrapper that preserves the signature of the function to wrap
Folding @ Home on Linux Mint to contribute to the analysis of the new coronavirus
[Python] I made a script that automatically cuts and pastes files on a local PC to an external SSD.
GAE --With Python, rotate the image based on the rotation information of EXIF and upload it to Cloud Storage.
Various ways to read the last line of a csv file in Python
I made a Line Bot that uses Python to retrieve unread Gmail emails!
A Python script that reads a SQL file, executes BigQuery and saves the csv
Create a BOT that displays the number of infected people in the new corona
Build a python environment to learn the theory and implementation of deep learning
I want to create a web application that uses League of Legends data ①
[Python] A program that calculates the difference between the total numbers on the diagonal line.
[Python] A program that calculates the number of updates of the highest and lowest records
Python script to get a list of input examples for the AtCoder contest
I made a script to record the active window using win32gui of Python