Matt Bettinson
Matt Bettinson

Reputation: 531

Where and how can I install twitter's Python API?

I went to twitter's API, it redirected me to google code, and the website wasn't there. Any alternative twitter APIs, plus tutorials? thanks!

Upvotes: 4

Views: 19739

Answers (4)

Blairg23
Blairg23

Reputation: 12055

Twitter maintains an ongoing list of libraries a developer can use in many different languages, including Python. Honestly, the easiest method is just using the Python requests library and performing a simple HTTP request to one of the many REST endpoints.

Here is an example I wrote to search the Twitter tweets REST endpoint:

import requests
from requests_oauthlib import OAuth1 # For authentication

_consumer_key = <api_key>
_consumer_secret = <api_secret>
_key = <token>
_secret = <token_secret>

_auth = OAuth1(_consumer_key, _consumer_secret, _key, _secret)

def search(search_terms):
    # Twitter search URL:
    url = 'https://api.twitter.com/1.1/search/tweets.json'
    payload = {
        'q': search_terms,  # May be @user_search or #hash_search also
        'lang': 'en',  # Based on ISO 639-1 two-letter code
        'result_type': 'mixed',
        'count': '100',  # Number of tweets to return per page, up to a max of 100
        'until': Get_Time()['today']
    }
    search_results = requests.get(url, auth=_auth, params=payload)
    print search_results.json() # Gets the raw results in json format 

To create api and token keys and secrets, you need to create an app here at apps.twitter.com. Twitter has a very user friendly REST api and documentation, so I believe it will be easy to understand once you do a few HTTP requests and receive appropriate responses.

Upvotes: 1

zvisofer
zvisofer

Reputation: 1368

this is a very user friendly twitter API.

Upvotes: 0

Joe F.
Joe F.

Reputation: 867

I've been using Python-Twitter for the past couple of months. It makes it extremely easy to pull data from the Twitter API as well as post Tweets.

You can install via pip:

pip install python-twitter

or by cloning from git => https://github.com/bear/python-twitter.git then installing the dependencies (which can be done via pip) follow the instructions in README.rt

python setup.py build

then

python setup.py install

After you've installed the library, I'd recommend setting up a simple authentication file (eg. twitterAuth.py) like this one:

# twitterAuth.py

import twitter

"""This script is meant to connect to the Twitter API via the tokens below"""

api = twitter.Api(consumer_key='yourConsumerKeyGoesHere',
    consumer_secret='yourConsumerSecretGoesHere',
    access_token_key='your-AccessTokenGoesHere',
    access_token_secret='yourTokenSecretGoesHere')

Then you can simply import this from any scripts that need to acccess the Twitter API. Here's a simple example that posts a Tweet:

from twitter import * 
import twitterAuth

api = twitterAuth.api

status = api.PostUpdate('testing twitter-python')
print status.text

Upvotes: 4

146
146

Reputation: 821

Try Tweepy: http://code.google.com/p/tweepy/

You can get to a tutorial wiki page for it at the same Google Code link.

To install it with easy_install, just run easy_install tweepy

To install it with git:

git clone git://github.com/joshthecoder/tweepy.git
cd tweepy
python setup.py install

To install it from source, download the source from http://pypi.python.org/pypi/tweepy then run something like:

tar xzvf tweepy-1.7.1.tar.gz
cd tweepy-1.7.1
python setup.py install

Upvotes: 6

Related Questions