quinta-feira, 25 de setembro de 2014

TWEEPY

Um jeito fácil de usar biblioteca python para acessar twitter API.

Documentação:
https://media.readthedocs.org/pdf/tweepy/latest/tweepy.pdf

Código para escrever no meu twitter via tweepy:

import tweepy

# == OAuth Authentication ==
#
# This mode of authentication is the new preferred way
# of authenticating with Twitter.

# The consumer keys can be found on your application's Details
# page located at https://dev.twitter.com/apps (under "OAuth settings")
consumer_key="ITAeiVaqrS21zmYTmmHXaQ3h1"
consumer_secret="Hay3hh3uCOcJ1nk96zPKffgwIMZnHRE18afdlzUzCklH5arXNa"

# The access tokens can be found on your applications's Details
# page located at https://dev.twitter.com/apps (located 
# under "Your access token")
access_token="136375442-t59V1rbJz3hUK1t7hjJqmSYMexWh1PoiCf9dpS7b"
access_token_secret="1rVRTEhyrMnz33qhFWwVzRhzYklSLL8WB2yT1KbWgz1Ai"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.secure = True
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

# If the authentication was successful, you should
# see the name of the account print out
print api.me().name

# If the application settings are set for "Read and Write" then
# this line should tweet out the message to your account's 
# timeline. The "Read and Write" setting is on https://dev.twitter.com/apps
api.update_status('Updating using OAuth authentication via Tweepy!')







Código para buscar tweets com 'eleicoes2014' e exibir no cmd:

import tweepy
import json

# Authentication details. To  obtain these visit dev.twitter.com
consumer_key = 'ITAeiVaqrS21zmYTmmHXaQ3h1'
consumer_secret = 'Hay3hh3uCOcJ1nk96zPKffgwIMZnHRE18afdlzUzCklH5arXNa'
access_token = '136375442-t59V1rbJz3hUK1t7hjJqmSYMexWh1PoiCf9dpS7b'
access_token_secret = '1rVRTEhyrMnz33qhFWwVzRhzYklSLL8WB2yT1KbWgz1Ai'

# This is the listener, resposible for receiving data
class StdOutListener(tweepy.StreamListener):
    def on_data(self, data):
        # Twitter returns data in JSON format - we need to decode it first
        decoded = json.loads(data)

        # Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users
        print '@%s: %s' % (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))
        print ''
        return True

    def on_error(self, status):
        print status

if __name__ == '__main__':
    l = StdOutListener()
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    print "Showing all new tweets for #eleicoes2014:"

    # There are different kinds of streams: public stream, user stream, multi-user streams
    # In this example follow #programming tag
    # For more details refer to https://dev.twitter.com/docs/streaming-apis
    stream = tweepy.Stream(auth, l)
    stream.filter(track=['eleicoes2014'])



Nenhum comentário:

Postar um comentário