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.
Install the required libraries with pip.
$ pip install feedparser requests
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
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 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')
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)
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 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)
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.
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.