Website Design Chester by the Informatics Centre

Updating the Twitter Feed for Python 3

For this guide I am going to assume that you have Python 3 installed and set up and that you are familiar with running Python commands on the command line.

Grab a copy of the Python 2 code

I am going to be carrying out the required changes to make the Python 2 code for our Twitter app work on Python 3 so grab a copy from the previous blog post.

Install Pillow

The Python Imaging Library (PIL) does not have support for Python 3, however a fork of the project has been created which does. The fork happens to be called pillow and it's easy to install by running the following command.

pip install pillow

Install python3-oauth2

The oauth2 library also doesn't work in Python 3 so a new library is needed however as with PIL a fork is available which has Python 3 support. You will need to download and install this yourself though by doing the following steps.

  1. Go to https://github.com/i-kiwamu/python3-oauth2 and download the zip file of the repository by clicking the button in right menu bar.
  2. Extract the zip file to a memorable location.
  3. Install the library using the following command when in the extracted folder.
    python setup.py install
    

Fixing the code

Now we need to make some changes to the code to take into account the differences between Python 2 and Python 3.

Fixing renderer.py

Open renderer.py and on line 1 change "Tkinter" to "tkinter".

Fixing twitterrepository.py

Open twitterrepository.py and make the following changes.

Find "response" on line 38 and change it to "response.decode()" and do the same on line 55.

Find "urllib" on line 48 and change it to "urllib.parse".

Find "unicode" on line 42 to "str" and do the same on line 58.

Handling extra characters

Twitter allows you to use a variety of Unicode characters which are outside of the range of characters that TCL will render. We can get around this by adding the following line to encode the characters which it won't render.

Open feedrenderer.py and find "post.text" on line 50 and change it to "post.text.encode('unicode_escape')".

Download the source code GitHub

*You must register your own app to your twitter developer account, and then edit the fields in "start.py".
*This Twitter feed will also check your blocked users, and any Tweets from them are ignored.

If you have any questions feel free to post a comment below!

View all Informatics Centre blog posts.