Mastodon bot that posts NZ weather alerts from the metservice.com Commong Alerting Protocol (CAP) feed.
To run this bot yourself you would probably need:
- A mastodon account, e.g. on botsin.space (if on another server you will need to update
config.json) - An app registered for that account (you can do this from the development settings) and the access token associated with that app saved in
nzweather_usercred.secretor a similar file - A shapefile of New Zealand (I am using LINZ's 1:500 coastlines and islands file, exported in EPSG:2193, as it is only 1.2MB, but if you can find a smaller one that's only for the better). The file name needs to be updated in the
config.jsonfile - The packages listed in
requirements.txtinstalled, ideally in a python3 virtual environment - You will probably want to change the visibility option in the config from 'direct' to 'public', 'unlisted', or 'private', and adjust the wait time (in seconds)
The bot can be run with python3 mastodon-metservice.py with no future arguments, but running in cron (dealing with working directories, virtual environments, logging etc) requires a mess like:
*/5 * * * * /home/petra/mastodon-metservice/venv/bin/python /home/petra/mastodon-metservice/mastodon-metservice.py --config=/home/petra/mastodon-metservice/config.json --dir="/home/petra/mastodon-metservice" >> /home/petra/weatherbot-log.txt 2>&1If run with --debug the bot will not connect to Mastodon or post but instead prints to stdout, and therefore does not need valid user credentials. However it still needs the mastodon.py package to be loaded.
The config file is at config.json although another file can be specified with --config=<path to file>. The options are, in no particular order:
shape_file: Path to a shape file to use as the outline in the mapstz: Name of the timezone used to give times and dateswait: Number of seconds to wait between posts, to avoid completely clogging timelines when large numbers of alerts are being updatedfull_map: File name to use to temporarily save the summary map (not currently used)alert_map: File name to use to temporarily save the individual alert maps between generation and posting (not currently used)rss_url: URL of the rss CAP feedwebsite_url: URL to direct users for more information (currently unused, and instead taken from the feed for each individual alert)archive_file: JSON file used to save the most recently updated information - currently only used for the list of guids for comparison with most recent rss loadmastodon_server: URL of the mastodon server the account is located onmastodon_cred: file path to the user secret file for the bot; can also just contain the secret directly (not recommended)visibility: Post visibility on mastodon, one of 'public', 'unlisted', 'private', or 'direct', for the main (summary) postsecondary_visibility: Post visibility of replies to the main post, i.e. details of individual warnings and watches. Recommended to be unlisted.tileservice: XYZServices TileProvider-style dict for the base map of the secondary posts; if omitted no maps will be created for the secondary postsmain_map: Info used to create the background map for the main posts; if omitted no maps will be created for the main posts
Example, for LINZ Gridless Topo basemap:
"tileservice": {
"name": "LINZ Topo Gridless",
"url": "https://basemaps.linz.govt.nz/v1/tiles/{tileset_name}/{crs}/{z}/{x}/{y}.{format}?api={api_key}",
"attribution": "Basemap (C) LINZ",
"api_key": "<API Key>",
"tileset_name": "topo-raster",
"crs": "WebMercatorQuad",
"format": "webp"
}LINZ API keys are available from the LINZ Basemaps site, though note that by default they expire after a few months (and only work for NZ maps obviously).
Alternate, for OSM you might have:
"tileservice": {
"name": "OSM Mapnik",
"url": "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
"attribution": "(C) OpenStreetMap contributors"
}...but this is blocked nowofdays so don't do that; find a service with api keys etc.
For more information, see xyzservices: A full look into providers objects.
Should contain:
- The CRS specifier for these maps (2193 is NZGD2000 / New Zealand Transverse Mercator) - this doesn't need to be what the shapefiles are in currently, but what they will be converted to via geopandas as
crs - The background colour as
bg_colour shapefiles, which in turn require apathto a shapefile and acolour(nameis optional)
My current:
"main_map": {
"crs": "EPSG:2193",
"bg_colour": "#80CCFF",
"shapefiles": [
{
"name": "coastlines",
"path": "lds-nz-coastlines-and-islands-polygons-topo-1500k-SHP.zip",
"colour": "#FFFFCC"
},
{
"name": "urban",
"path": "nz-nochathams-large-urban.zip",
"colour": "#C0C0C0"
},
{
"name": "lakes",
"path": "nz-nochathams-lakes.zip",
"colour": "#80CCFF"
}
]
}This bot can probably be adapted to another CAP feed relatively easily, assuming they use a similar format to metservice, but I don't think it would work out of the box - I may try this myself at a later date. In particular the RSS url is specified in the config file.