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.
"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".
You can do this
Raspberry Pi 3 Model B+、Python 2.7.16[GCC 8.3.0] on linux2。
First from here. Please refer to this area to get the Twitter API. https://qiita.com/kazupen2018/items/ff9828cc853ab9c3357e
$ pip install feedparser
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"]
$ python test.py
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.
$ pip install twython
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")
$ python JMA_XML.py
If you tweet like this, you are successful.
Recommended Posts