Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of results for a particular word on Twitter (API v1.1)

The question has already been asked a couple of times. For example here or here. Yet, no accepted answer was found. Also, both the questions refer to Twitter API v1.0, which is no longer in use. Thus, I thought it could be beneficial to share a simple piece of code I wrote in order to have the number of tweets containing a given keyword (or phrase).

If you have any feedbacks, do not hesitate to reply.

like image 901
Edgar Derby Avatar asked Feb 05 '14 14:02

Edgar Derby


People also ask

Can I still use Twitter API v1 1?

The v1. 1 search/tweets and the Twitter API v2 recent search endpoint support both OAuth 1.0a User Context and OAuth 2.0 App-Only. Therefore, if you were previously using the standard v1. 1 search endpoint you can continue using the same authentication method if you migrate to the Twitter API v2 version.

What is the Twitter word count?

In most cases, the text content of a Tweet can contain up to 280 characters or Unicode glyphs. Some glyphs will count as more than one character.


1 Answers

Here it is:

#Import the required modules
from twython import Twython
import json
import csv

#Set parameters
keyword = 'kittens'; #The desired keyword(s)
tweetsXiteration = 100; #Where 100 is the max
dateFrom = '2014-02-01'; #Inclusive (YYYY-MM-DD)
dateTo = '2014-02-02'; #Exclusive (YYYY-MM-DD)
done = False; #Must be false

#Setting the OAuth
Consumer_Key = 'XXX';
Consumer_Secret = 'XXX';
Access_Token = 'XXX';
Access_Token_Secret = 'XXX';

#Connection established with Twitter API v1.1
twitter = Twython(Consumer_Key, Consumer_Secret, Access_Token, Access_Token_Secret);

#Twitter is queried
response = twitter.search(q = keyword, count = tweetsXiteration, since = dateFrom, until = dateTo, result_type = 'mixed');

#Results (partial)
countTweets = len(response['statuses']);

#If all the tweets have been fetched, then we are done
if not ('next_results' in response['search_metadata']): 
    done = True;

#If not all the tweets have been fetched, then...
while (done == False):

    #Parsing information for maxID
    parse1 = response['search_metadata']['next_results'].split("&");
    parse2 = parse1[0].split("?max_id=");
    parse3 = parse2[1];
    maxID = parse3;

    #Twitter is queried (again, this time with the addition of 'max_id')
    response = twitter.search(q = keyword, count = tweetsXiteration, since = dateFrom, until = dateTo, max_id = maxID, include_entities = 1, result_type = 'mixed');

    #Updating the total amount of tweets fetched
    countTweets = countTweets + len(response['statuses']);       

    #If all the tweets have been fetched, then we are done
    if not ('next_results' in response['search_metadata']): 
        done = True;

print(countTweets);

Keep in mind that:

  1. You need to authenticate trough OAuth;
  2. You can only fetch results no older than a week;
  3. If you want to search for more than one word, you need to use "" if you are only interested in the results containing the two words in that particular order (e.g., '"Stack Overflow"').

Additional information can be found here or on Twitter official documentation.

like image 141
Edgar Derby Avatar answered Oct 07 '22 03:10

Edgar Derby