How to configure pywws to post messages to Twitter

Install dependencies

Posting to Twitter requires some extra software. See Dependencies - Twitter updates.

Create a Twitter account

You could post weather updates to your ‘normal’ Twitter account, but I think it’s better to have a separate account just for weather reports. This could be useful to someone who lives in your area, but doesn’t want to know what you had for breakfast.

Authorise pywws to post to your Twitter account

Before posting to Twitter you need to authorise pywws by running the module with the -r option.

If you run pywws on a low power device such as a Raspberry Pi, you may find it easier to run this authorisation step on another computer, as long as it has the required dependencies installed. You can use an empty ‘data’ directory – a weather.ini file will be created whose contents can be copied into your real weather.ini file using any text editor.

Make sure no other pywws software is running, then run the module with the -r option:

python -m pywws.totwitter -r data_dir

(Replace data_dir with your data directory.)

This will open a web browser window (or give you a URL to copy to your web browser) where you can log in to your Twitter account and authorise pywws to post. If the login is successful the browser will display a 7 digit number which you then copy to pywws:

jim@brains:~/Documents/projects/pywws/master$ python -m pywws.totwitter -r ../data/
12:20:08:pywws.logger:pywws version 18.4.2, build 1521 (487c307)
Please enter the PIN shown in your web browser: 9069882
Success! Authorisation data has been stored in ../data/weather.ini

The secret and key values stored in weather.ini give access to your Twitter account and should be kept confidential.

Add location data (optional)

Edit your weather.ini file and add latitude and longitude entries to the [twitter] section. For example:

secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
latitude = 51.501
longitude = -0.142

Create a template

Twitter messages are generated using a template, just like creating files to upload to a website. Copy the example template ‘tweet.txt’ to your template directory, then test it:

python -m pywws.template ~/weather/data ~/weather/templates/tweet.txt tweet.txt
cat tweet.txt

(Replace ~/weather/data and ~/weather/templates with your data and template directories.) If you need to change the template (e.g. to change the units or language used) you can edit it now or later.

Post your first weather Tweet

Now everything is prepared for pywws.totwitter to be run. Using high verbosity shows you what’s happening as it runs:

python -m pywws.totwitter -vv ~/weather/data tweet.txt

If this works, your new Twitter account will have posted its first weather report. (You can delete the tweet.txt file now.)

Add Twitter updates to your hourly tasks

Edit your weather.ini file and edit the [hourly] section. For example:

services = []
plot = ['7days.png.xml', '24hrs.png.xml', 'rose_12hrs.png.xml']
text = [('tweet.txt', 'T'), '24hrs.txt', '6hrs.txt', '7days.txt']

Note the use of the 'T' flag – this tells pywws to tweet the template result instead of uploading it to your web site.

You could use the [logged], [12 hourly] or [daily] sections instead, but I think [hourly] is most appropriate for Twitter updates.

Changed in version 13.06_r1015: added the 'T' flag. Previously Twitter templates were listed separately in twitter entries in the [hourly] and other sections.

Include an image in your tweet

New in version 14.05.dev1216.

You can add up to four images to your tweets by specifying the image file locations in the tweet template. Make the first line of the tweet media path where path is the absolute location of the file. Repeat for any additional image files. The “tweet_media.txt” example template shows how to do this.

The image could be from a web cam, or for a weather forecast it could be an icon representing the forecast. To add a weather graph you need to make sure the graph is drawn before the tweet is sent. The pywws.regulartasks module processes graph and text templates before doing Twitter uploads, so you can include the graph drawing in the same section. The 'L' flag ensures the plot is stored in your local files directory:

services = []
plot = [('tweet.png.xml', 'L'), '7days.png.xml', '24hrs.png.xml', 'rose_12hrs.png.xml']
text = [('tweet_media.txt', 'T'), '24hrs.txt', '6hrs.txt', '7days.txt']

Comments or questions? Please subscribe to the pywws mailing list and let us know.