Post messages to Twitter.

Twitter is a popular micro-blogging service. Users send short messages (up to 280 characters) called “tweets”. This module sends tweets, with optional image files, typically to report on weather conditions every hour.

Create an 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.

The pywws.service.twitter module requires you to install additional dependencies. There is a choice of Python Twitter library. Use either python-twitter (preferred) or tweepy:

sudo pip install python-twitter oauth2


sudo pip install tweepy oauth2

Authorise pywws

Before you can send tweets you need to authorise pywws to post to your account. 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. (Remember to replace data_dir with your data directory.)

python -m pywws.service.twitter -r data_dir

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:~$ python3 -m pywws.service.twitter -r weather_data
14:19:21:pywws.logger:pywws version 18.8.0, build 1566 (5054dc7)
Please enter the PIN shown in your web browser: 2594624

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

A tweet is a short text of up to 280 characters (fewer if you include images). It’s up to you what to put in your tweets but an example is included to get your started. Copy the example template tweet.txt to your template directory, then edit it to suit your preferences. You should also check it uses the same text encoding as your other templates.

Now generate a tweet file from your template, for example:

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 tweet

Now you are ready to run pywws.service.twitter. Using high verbosity shows you what’s happening as it runs:

python -m pywws.service.twitter -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 posts to your hourly tasks

Edit the [hourly] section in weather.ini. Add your tweet template to the text list. Then add twitter to the services list, with an option specifying the template processing result. For example:

text = ['tweet.txt']
services = [('twitter', 'tweet.txt')]

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

Include images in your tweet

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 file name, or full path for files that are not in your “output” directory (a subdirectory of your work directory called output). 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 service uploads, so you can include the graph drawing in the same section. For example:

plot = ['tweet.png.xml']
text = ['tweet_media.txt']
services = [('twitter', 'tweet_media.txt')]


PythonTwitterHandler(key, secret, latitude, …)
ToService(context[, check_params])
TweepyHandler(key, secret, latitude, longitude)
class pywws.service.twitter.TweepyHandler(key, secret, latitude, longitude)[source]

Bases: object

post(status, media)[source]
class pywws.service.twitter.PythonTwitterHandler(key, secret, latitude, longitude, timeout)[source]

Bases: object

post(status, media)[source]
class pywws.service.twitter.ToService(context, check_params=True)[source]

Bases: pywws.service.FileService

config = {'key': (None, True, None), 'latitude': (None, False, None), 'longitude': (None, False, None), 'secret': (None, True, None)}
logger = <Logger pywws.service.twitter (WARNING)>
service_name = 'twitter'

Context manager factory function for a batch of one or more uploads.

This makes it easy to ensure any resources such as an internet connection are properly closed after a batch of uploads. Use the contextlib.contextmanager() decorator when you implement this method.

For a typical example, see the source code of the pywws.service.openweathermap module. If your upload can’t benefit from a session object yield None, as in pywws.service.copy.

upload_file(session, filename)[source]

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