Blog Post Icon

Automatically tweet your Toronto Real Estate (TREB) listings


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 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’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 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 =

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

    bitly_url = b.shorten(
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'])
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? šŸ™‚

At Shift8, we cater to all sorts of businesses in and around Toronto from small, medium, large and enterprise projects. We are comfortable adapting to your existing processes and try our best to compliment communication and collaboration to the point where every step of the way is as efficient as possible.

Our projects are typically broken into 5 or 6 key “milestones” which focus heavily on the design collaboration in the early stages. We mock-up any interactive or unique page within your new website so that you get a clear picture of exactly how your design vision will be translated into a functional website.

Using tools like Basecamp and Redpen, we try to make the process simple yet fun and effective. We will revise your vision as many times as necessary until you are 100% happy, before moving to the functional, content integration and development phases of the project.

For the projects that are more development heavy, we make sure a considerable amount of effort is spent in the preliminary stages of project planning. We strongly believe that full transparency with a project development plan ensures that expectations are met on both sides between us and the client. We want to ensure that the project is broken into intelligent phases with accurate budgetary and timeline breakdowns.

Approved design mock-ups get translated into a browse-ready project site where we revise again and again until you are satisfied. Client satisfaction is our lifeblood and main motivation. We aren’t happy until you are.

Need Web Design?

Fill out the form to get a free consultation.

shift8 web toronto – 416-479-0685
203A-116 geary ave. toronto, on M6H 4H1, Canada
Ā© 2023. All Rights Reserved by Star Dot Hosting Inc.

contact us
phone: 416-479-0685
toll free: 1-866-932-9083 (press 1)

Shift8 Logo