Using Twitter Search API To Find Happy People (Latest)

Using the new Twitter Search API (v1.1) requires a little bit more work than in the previous version. Today, I am going to show you how to set everything up and finally show you how to use this new Twitter Search API to search whatever you want on Twitter. I am doing this because many people have asked me to do it!

twitter search api

Using the new Twitter Search API – Requirements

Before you can use this new version of the Twitter Search API, you need a few things – just two things to be exact and they include:

  1. Create a Twitter Application on Twitter Developer Site
  2. Install one of the Twitter Libraries for the respective language like in our example, Python!

The first step is pretty straight-forward to do and I have actually saved you a ton of reading by creating a short video that you can quickly follow to create a Twitter Application on Twitter Developer Page. Please use it to setup and obtain the keys and tokens that will be generated when you create your first application.

The second part is the fun one although not very simple. In order to install the TwitterSearch Library – a Python API for Twitter, you can use either easy_install or pip. I have, fortunately, created straight-to-the point guides on how to install both of them on your computer. I even show you how to use them. To install pip, click here and if you prefer easy_install, click here. Once you have installed either one, simply go to your command line and do: easy_install TwitterSearch or pip install TwitterSearch and press enter. Depending on which one you installed, you should be able to use that TwitterSearch library in your Python code. You can install easy_install by watching this video I recently created 🙂

The Twitter Search API Code Example

When you created an application on Twitter Developer Site in step 1 above, you generated the following:

  • OAUTH_TOKEN (Access Token)
  • OAUTH_SECRET (Access Token)

Now is the time to use these bad boys! This is going to be fun! By the way, keep them private because well, they are your secrets.

What we will be searching for on Twitter

Let us search for something we rarely think about; what happy people say! You know, there is so much stress in the world, and so why not see what happy people talk about? You can try the opposite of happiness later.

The Results of Running the above Twitter Search Code

  • @dudewitch tweeted: happy flavour day to all my heroes
  • @RobertBrooks26 tweeted: I had 6 dogs at my house all weekend. That made me happy, but I’d still prefer 6 cats or something
  • @Ima_YellowGirl tweeted: Happy I never had to “act” like I had.
  • @ErrinBear tweeted: RT @cedesziegler: Rain. I’m happy.
  • @Tia_Milan95 tweeted: I guess I’ll tell skinny happy birthday too .!
  • @DevonSeacord tweeted: Why waste your time getting hurt by someone, when there’s someone else out there waiting to make you happy?
  • @avernalaw tweeted: @ozsultan @gagnier @saurnou Happy Labor Day. Are we on for Wednesday?
  • @Meredith_anne1 tweeted: RT @anna_smitty: happy hell week may the eggs be ever in your favor
  • @wilaisingkhon tweeted: RT @ItsLifeFact: Nobody can make you happy until you’re happy with yourself first.
  • @AwkwardComedy tweeted: RT @FactsGuide: Depressed people are likely to get colds more often while happy and energetic individuals get sick less often.
  • @FlyestPenguin tweeted: RT @shannamalcolm: It’s Labor Day so I can sleep late right? Good morning & happy Labor Day!:)

You see, people are having fun out there! You might have also noticed in your own results that the smiley face is associated with happiness – but when I tried searching the emoticon itself, I got errors because of encoding issues.

Where to go from here

The best place to stop by from here is the Twitter Search API documentation and read more about other methods you can use to analyze Twitter data. Once again, try searching for sad tweets or what you want or think about on a daily basis. This is just the tip of the twitter-berg (not an iceberg)!

So the real difference between the new twitter search api and the older one is that you have to have authentication to access the data! That is the bottom line.

If you liked this post, please share and if you have any questions, ask them through the comments. Subscribe for more updates as well!Improve this idea by creating a real application of your own! The sky is the limit.

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

20 Comments on “Using Twitter Search API To Find Happy People (Latest)

  1. Nishikar Reply

    September 10, 2013 at 4:38

    Thanks for the post.
    In line 21, searchObject = searchOrder

    • Elisha Chirchir Reply

      September 10, 2013 at 2:23

      First, you are welcome and thank you for reading. Thank you also for locating that error there on line 21. I have fixed it now and it should work fine going forward!

  2. manoj Reply

    October 7, 2013 at 8:21

    I am getting this error when I am trying to run the above script

    ImportError: No module named TwitterSearch

    I think it is because i don’t have any twitter libraries installed on my machine, can you please help me with this… I am a new bie in programming.

    • Elisha Chirchir Reply

      October 7, 2013 at 8:53

      You most likely don’t have the TwitterSearch Python library installed. It is easy to install it and I have created a post that will help you install it with ease.

      First, go to and follow those instructions. This will help you install pip – a python package manager that will be helpful for all other python libraries. Once you have that installed, simply do this:

      pip install TwitterSearch

      on your command line! You will be done and ready to import and use that library.

      I am also available to chat on Gmail if you need more help!

  3. L Reply

    October 17, 2013 at 4:48

    Thanks so much for this info. It is very helpful! I set up TwitterSearch according to your instructions and it runs great! However, now I am trying to use setGeocode [ex: setGeocode(39.8282,-98.5795,1000,’mi’)] and I keep getting Error 1005: Invalid unit.

    What I am trying to do is get Tweets with location information and I thought that this is a good work around, but if you have any suggestions to only pull Tweets with coordinate info, I would love to know! Thanks much!

    • Elisha Chirchir Reply

      October 18, 2013 at 3:52

      I am glad you found this helpful. The best way to solve your problem here is to study the TwitterSearch library on Github and see if it has a method for geo-coding (location). If it has, then use it, else, add your own or find a better library with such functionality.

      Let me know how it goes.

  4. Jacob Reply

    November 21, 2013 at 6:28

    Hi, thank you for sharing this and for a wonderful website!

    Im trying to run your script above but run into this:

    Traceback (most recent call last):
    File “”, line 3, in
    from TwitterSearch import *
    File “/home/pi/”, line 25, in
    except TwitterSearchException as e: # deal with…
    NameError: name ‘TwitterSearchException’ is not defined

    Do you have any idea of what I´ve been missing?



    • Elisha Chirchir Reply

      November 21, 2013 at 6:47

      I would suggest you type each piece line by line. I also hope you installed the TwitterSearch python library before trying to run this code. You can always test if the library was installed correctly by going to your python interactive shell and importing the library (from TwitterSearch import *) and if you get no errors, then you are good to go. Otherwise, use pip or easy_install to install it first.

      If you still need help, I am available for gmail chats where I can show you all you need free of charge!

  5. Vikalp Handa Reply

    October 21, 2014 at 10:05

    Thanks a lot for this wonderful post.
    While I’m trying to the above code on my machine I’m getting the following error:
    Error 401: Unauthorized: Authentication credentials were missing or incorrect
    PS: I’ve installed TwitterSearch using pip and also updated my requests to include to_native_string.

    • Elisha Chirchir Reply

      October 22, 2014 at 2:27

      Make sure you have created an application on Twitter Website before trying to make requests.

  6. Raviraj Kuber Reply

    October 16, 2015 at 4:29

    Hi i am trying to develop a twitter app using JAVA. i Would Like to know how to download & save the images that are sent as Direct Message on Twitter

    • Elisha Chirchir Reply

      October 17, 2015 at 10:05

      First, create an app on Twitter developer page. Complete the settings fields as expected then download Twitter4J library that you can use to interact with Twitter APIs. I have not tried to get Direct Messages before but if you have researched and found out that it is possible to do, please try it with that; let me know how it goes.

  7. Kaloma Reply

    February 1, 2016 at 12:29

    Thank you for this nice tutorial. Could you explain how to retieve all replies to specific user’s specific tweets. Thans in advance.

  8. Newb Reply

    March 22, 2016 at 4:12

    Hi, thanks for your post! However, when I try to run it, it gives me this:

    The Belgian date format is 22-03-2016 , while the US format is 03/22/2016
    Traceback (most recent call last):
    File “”, line 1, in
    from TwitterSearch import *
    File “C:\Python27\lib\site-packages\TwitterSearch\”, line 7, in
    from .TwitterSearch import TwitterSearch
    File “C:\Python27\lib\site-packages\TwitterSearch\”, line 3, in
    import requests
    File “C:\Python27\lib\site-packages\requests\”, line 53, in
    from .packages.urllib3.contrib import pyopenssl
    File “C:\Python27\lib\site-packages\requests\packages\”, line 27, in
    from . import urllib3
    File “C:\Python27\lib\site-packages\requests\packages\urllib3\”, line 8, in
    from .connectionpool import (
    File “C:\Python27\lib\site-packages\requests\packages\urllib3\”, line 33, in
    from .packages.ssl_match_hostname import CertificateError
    File “C:\Python27\lib\site-packages\requests\packages\urllib3\packages\”, line 3, in
    from . import ssl_match_hostname
    File “C:\Python27\lib\site-packages\requests\packages\urllib3\packages\ssl_match_hostname\”, line 3, in
    from ssl import CertificateError, match_hostname
    File “C:\Python27\lib\”, line 90, in
    import textwrap
    File “C:\Python27\lib\”, line 40, in
    class TextWrapper:
    File “C:\Python27\lib\”, line 82, in TextWrapper
    whitespace_trans = string.maketrans(_whitespace, ‘ ‘ * len(_whitespace))
    AttributeError: ‘module’ object has no attribute ‘maketrans’

    • Elisha Chirchir Reply

      March 27, 2016 at 11:49

      The easiest way to narrow down this problem is to remove some of the potentially problematic code and then add back one line at a time. This will help you isolate the problem. Looks like you are calling maketrans() method in the String object and it complains that it doesn’t exist.

  9. yi Reply

    March 24, 2016 at 8:55

    Thank you for your kind help. It’s really useful for me and I have subscribe your account. One question I’m confusing is that, can I use twitter api to search past data? I mean for example, I want to collect all tweets about ‘Christmas’ in Paris during the last week of 2013,2014,2015. Looking forward to your reply, thank you again!

  10. yi Reply

    March 24, 2016 at 9:22


    Sorry to bother again, I just run the program, however, it has a error said:
    File “D:\Eclipse Workspace\demo1\src\”, line 10
    SyntaxError: invalid syntax

    I don’t know what’s wrong with it. How could I correct the code?

    Best regards

    • Elisha Chirchir Reply

      March 27, 2016 at 11:45

      In Java, Strings are enclosed in DOUble quotes and not single quotes; so you would do something like this : String consumer_secret = “YourSecrethere”;

      • yi Reply

        March 29, 2016 at 9:16

        Hi Elisha, thanks for your reply. However, the thing is, I just use Eclipse for PyDev, and write python code. Sorry that I still can’t find the out way. This is what the error shows:
        File “D:\Eclipse Workspace\demo1\src\”, line 10
        SyntaxError: invalid syntax.
        By the way, there is a yellow exclamation mark which I don’t know the meaning, says:
        PyDev breakpoint
        It seems that TwitterSearchOrder can’t been imported now
        Unused in wild import: TwitterOrder, TwitterSearch, TwitterSearchException, TwitterSearchOrder,
        TwitterUserOrder, py3k, utils

Leave a Reply

Your email address will not be published. Required fields are marked *