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!

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:

Create a Twitter Application on Twitter Developer Site

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)

CONSUMER_KEY

CONSUMER_SECRET

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

#using twitter search api

fromTwitterSearch import*

try:

searchOrder=TwitterSearchOrder()# create a TwitterSearchOrder object

searchOrder.setKeywords(['happy'])

searchOrder.setCount(10)# only return 10 pages

searchOrder.setIncludeEntities(False)# and don't give us all those entity information

# Now let's create a Twitter Search API Object here

# complete these by copying from your Twitter Application

# from Twitter Developer Site

ts=TwitterSearch(

consumer_key='xxxxxxxxx',

consumer_secret='yyyyyyyy',

access_token='zzzzzzzz',

access_token_secret='aaaaaaaa'

)

forhappy_tweet ints.searchTweetsIterable(searchOrder):

print('@%s tweeted: %s'%(happy_tweet['user']['screen_name'],

happy_tweet['text']))

exceptTwitterSearchException ase:# deal with exceptions as usual here

print(e)

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?

@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.

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

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 http://simpledeveloper.com/how-to-install-pip/ 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.

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!

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.

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!

Hi,
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.

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.

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.

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!

Sorry to bother again, I just run the program, however, it has a error said:
File “D:\Eclipse Workspace\demo1\src\test.py”, line 10
consumer_secret=’ZYlnYaYQDi3QU*********************GyxmbAFDYnRthYtF’
^
SyntaxError: invalid syntax

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\test.py”, line 10
consumer_secret=”ZYln*******Q*******HYa2*************yxmbA********tF”
^
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