[PYTHON] A story about getting the Atom field (XML telegram) of the Japan Meteorological Agency with Raspberry Pi and tweeting it

What do you want to do

I wanted to create a Twitter account like NE〇V, a special agency with complete complacency. For the time being, I would like to get weather warnings, warnings, and earthquake information and tweet them, leaving the automatic map creation aside.

Where to get information

"Meteorological Agency disaster prevention information XML format telegram" Toyara was published on the Japan Meteorological Agency website, so I am grateful to use it. The Atom feeds you can get ・ Regular: Information on the weather that is announced on a regular basis, such as general weather conditions. ・ As needed: Information on weather that is announced at any time, such as warnings and warnings. ・ Earthquake volcano: Information about earthquakes and volcanoes ・ Others: Those that do not belong to any of the above three types You can choose one of them, so choose the one you like and replace the variable "Atom_URL" in the code below. In addition, this article is advanced in "Anytime: Information on weather that is announced at any time such as warnings and warnings". It is also possible to obtain multiple information by duplicating the file and creating a file with a different variable "Atom_URL".

What you can do in the end

You can do this image.png image.png

My environment

Raspberry Pi 3 Model B+、Python 2.7.16[GCC 8.3.0] on linux2。

Get Twitter API

First from here. Please refer to this area to get the Twitter API. https://qiita.com/kazupen2018/items/ff9828cc853ab9c3357e

Install feedparser

$ pip install feedparser

Get information

test.py


# coding: utf-8
from twython import Twython, TwythonError
import feedparser

Atom_URL = "http://www.data.jma.go.jp/developer/xml/feed/extra.xml"
news_dic = feedparser.parse(Atom_URL)
latest_entry = news_dic['entries'][0]
rss1 = latest_entry.title + latest_entry.author
rss2 =  latest_entry.content

print (rss1)
print rss2[0]["value"]

Try to run

$ python test.py

Execution result

Meteorological Warning / Warning Hakodate Local Meteorological Observatory
[Watashijima / Hiyama Local Weather Warning / Warning] Cancels the warning.

After confirming that something like this is output, proceed to the next stage.

Tweet the acquired information

Install Twython

$ pip install twython

Try to write the code

JMA_XML.py


# coding: utf-8
from twython import Twython, TwythonError
#Japan Meteorological Agency Atom Field
import feedparser

APP_KEY = "Enter the obtained APP KEY here"
APP_SECRET = "Enter the APP SECRET obtained here"
OAUTH_TOKEN = "Enter the obtained OAUTH TOKEN here"
OAUTH_TOKEN_SECRET = "Enter the obtained OAUTH TOKEN SECRET here"

#Information on weather that is announced at any time, such as warnings and warnings
Atom_URL = "http://www.data.jma.go.jp/developer/xml/feed/extra.xml"
news_dic = feedparser.parse(Atom_URL)
latest_entry = news_dic['entries'][0]
rss1 =  latest_entry.content
rss2 = rss1[0]["value"]

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

try:
    twitter.update_status(status=rss2)
except TwythonError as e:
    print e

(Please replace APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET by yourself) (If you want to change the Atom field you want to get, replace the variable "Atom_URL")

Try to run

$ python JMA_XML.py

Check Twitter

image.png If you tweet like this, you are successful.

Recommended Posts

A story about getting the Atom field (XML telegram) of the Japan Meteorological Agency with Raspberry Pi and tweeting it
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Scraping the rainfall data of the Japan Meteorological Agency and displaying it on M5Stack
Read the GRIB2 file of the Japan Meteorological Agency with pygrib
A story about trying to use cron on a Raspberry Pi and getting stuck in space
A program that receives the servo command of the radio control, interrupts the Raspberry Pi and logs it
A story about predicting prefectures from the names of cities, wards, towns and villages with Jubatus
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
The story of making a sound camera with Touch Designer and ReSpeaker
A story about calculating the speed of a small ball falling while receiving air resistance with Python and Sympy
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
I made a system with Raspberry Pi that regularly measures the discomfort index of the room and sends a LINE notification if it is a dangerous value
Download the wind data of the Japan Meteorological Agency
A story that I wanted to realize the identification of parking lot fullness information using images obtained with a Web camera and Raspberry Pi and deep learning.
The process of installing Atom and getting Python running
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
A story about changing the master name of BlueZ
Getting Started with Yocto Project with Raspberry Pi 4 and WSL2
Make a note of what you want to do in the future with Raspberry Pi
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
(First post) A story about numerical calculation of influenza and new pneumonia coronavirus with Tensorflow
A story about porting the code of "Try and understand how Linux works" to Rust
The story of a Django model field disappearing from a class
Production of temperature control system with Raspberry Pi and ESP32 (1)
Create a partition and then install the Raspberry Pi OS
A story about how to deal with the CORS problem
The story of making a question box bot with discord.py
Building a distributed environment with the Raspberry PI series (Part 2: PiServer analysis and alternative system design)
Get information from the Japan Meteorological Agency and notify Slack of weather warnings in the 23 wards of Tokyo