laurens
laurens

Reputation: 497

Latest 100 mentions - Twitter api

I'm looking to achieve the following:

For a specific person, for example BarackObama, I'd like to get the last 100 times/tweets he was mentioned. Not his own tweets but the tweets of others containing @BarackObama. In the end I'd like to have: the person who mentioned, location, datetime. This content should be written to a flat file.

I've been experimenting with the Twitter API and Python, with success but haven't yet succeeded achieving the above problem.

I know there is a dev sections on the twitter website but they don't provide any example of code!! https://dev.twitter.com/docs/api/1/get/statuses/mentions count=100 ....

For me the scripting language or way of doing is not relevant it's the result. I just read on the internet that python and Twitter api are a good match.

Thanks a lot in advance!!

Upvotes: 0

Views: 2054

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121972

The tweepy module is not Python 3 compatible, but should give you a good start on how to talk to the Twitter API from python.

Essentially, the Twitter API is called over HTTP and returns JSON, so you'll need to read up on the json and either the urllib.request or the http.client modules (tweepy uses the python 2 equivalent of the http.client module, httplib).

Note that the API method you link to, mentions, does not give you arbitrary mentions, only those for the current twitter user. So if I were to log in, it would show mentions for @zopatista. To show mentions for @BarakObama you'd have to do a search for that string via the search API instead.

Upvotes: 1

Christian Witts
Christian Witts

Reputation: 11585

As far as I know you should be using the GET search instead of GET statuses/mentions as statuses/mentions is relative to the authenticating user.

Just use the username, in this case @BarackObama as the search term and it should yield what you're looking for.

As for sample code, you can look at projects like Python-Twitter which have already built libraries around the Twitter API so you don't have to roll your own unless you really need to.

Upvotes: 2

Related Questions