Blog

Automatically tweet your Toronto Real Estate (TREB) listings

Hello!

A while ago we released a Python based solution to connect to TREB, pull real estate listings and post it to WordPress. We also posted a brief guide on how to use the Python script.

With python, we are using the module wordpress_xmlrpc in order to connect to a wordpress sites via the XML RPC interface in order to search, add, edit and delete posts and content. There is tonnes of documentation out there with respect to how you can , via custom code in Python, Ruby or PHP (to name a few), interact with a WordPress site.

We are basically taking the CSV data set from TREB, parsing it for certain listings we are really interested in, grabbing the listing photos and then posting it to wordpress as a blog post. For someone with experience in development, this would be fairly easy to set up and integrate into a WordPress site.

Enough about WordPress! This post is about Twitter. What we wanted to do was during the daily execution of this Python script, not only post the new listings to WordPress, but tweet about them as well! In the tweets themselves, we would have definable hash tags (in our case #toronto and #realestate). We would also link to the actual wordpress post we just created by utilizing the bit.ly API to generate a shortened URL so it could fit in a single tweet.

Import the Python modules you need

import tweepy
import bitlyapi

We are using tweepy and bitlyapi to accomplish everything we’ve just described. In order to connect to twitter and bit.ly’s API , we would need to create API credentials on each service.

Load the authentication keys to interact with the APIs

    tw_consumer = ConfigSectionMap("twitter")['consumer']
    tw_secret = ConfigSectionMap("twitter")['secret']
    tw_token = ConfigSectionMap("twitter")['access_token']
    tw_token_secret = ConfigSectionMap("twitter")['access_token_secret']
    tw_hashtags = ConfigSectionMap("twitter")['hashtags']
    tw_bitlyuser = ConfigSectionMap("twitter")['bitly_user']
    tw_bitlykey = ConfigSectionMap("twitter")['bitly_key']

Obviously you will need to authenticate with Twitter and Bit.ly in order to tweet and generate shortened urls. We are using a Python module called ConfigParser. This allows us to store this information in a separate file (in our home folder). In the abbove snippet, we are parsing the defined config file and assigning the authentication data to variables that we will use later to authenticate.

Pull listing data from TREB

I wont go into this in too much detail, because you could just look at the script linked in the beginning of this post and see how we do it. We are basically connecting to TREB’s systems and downloading the listing data, parsing it and then compiling the listings we want to post into wordpress posts using a customizable post template. We take the variables from the TREB data like listing price, street address and so forth and using search/replace regex to generate wordpress posts via the template.

For the tweeting of the listing after posting to WordPress, we are taking the variables defined already for the WordPress post and then reformatting it into a tweet.

Authenticate with Twitter and Bitly

auth = tweepy.OAuthHandler(tw_consumer, tw_secret)
auth.set_access_token(tw_token, tw_token_secret)
api = tweepy.API(auth)
b = bitlyapi.BitLy(tw_bitlyuser, tw_bitlykey)

In the above code we take the keys defined with ConfigParser earlier and actually authenticate with the services in order to perform actions. Pretty straightforward and heavily documented already.

Start to build the tweet

tweet = 'New Listing : ' , address , ' , ' , listpricefix , ' , ' , bedrooms , ' beds ' , bathrooms , ' baths '
hashtag_list = tw_hashtags.split(',')
for hash in hashtag_list:
tweet += '#', hash , ' '
tweet_fixed = ''.join(str(e) for e in tweet)

You can see us taking variables already defined for the WordPress post (listing price, etc) and building the structure of how the tweet will look. We also have a definable option that the ConfigParser grabs : hashtags. We take the hashtags and because we are allowing people to define multiple hash tags, we parse through the comma separated list. You can look at the example config file on github (linked in the beginning) to see the formatting of the config variables.

Generate the shortened url of the previously posted WordPress post

post_link = wp.call(posts.GetPost(post.id))

First I should clarify how the link is pulled from wordpress. The above link, using the Python wordpress_xmlrpc module, pulls the previous post ID and gets the permalink and defines it to the variable post_link

try:
    bitly_url = b.shorten(longUrl=post_link.link)
except Exception, e:
    print 'Bitly error : ' + str(e)
    bitly_url = ''

If you look at the above code, you can see that I’m taking that post link and connecting to Bitly to generate a shortened url. The shortened url is stored in the variable bitly_url.

Add bitly URL to the end of the tweet and tweet it

tweet_fixed += ' ' + str(bitly_url['url'])
try:
    api.update_status(tweet_fixed[:140])
except Exception, e:
    print 'Twitter error : ' + str(e)

The first line in the above snippet simply adds the assigned Bitly url to the end of the tweet message. The next few lines are simply connecting to the Twitter api to post the message! If any errors happen it will be caught and posted here for you to debug. There is already a lot of examples in how you can use Python’s tweepy module so I dont need to get too much into it.

The bottom line is that this works! We love this python script not because of what it can technically do for you, but for the potential it represents. You can pull data from anywhere, post it to anything and then tweet about it.

Next week we might post a tutorial of how to automatically post to facebook in the same context! Wouldn’t that make life easier? 🙂