2014년 11월 12일 수요일

Tweepy

Tweepy

You can write your own twitter's bot with tweepy which is twitter API library for python.


An easy-to-user Python library for accessing the Twitter API.
Tweepy github


Let's make the own twitter'bot with tweepy
* Step 1 : Create a Twitter Application
* Step 2 : Install the Tweepy
* Step 3 : Make Twitter' bot with Tweepy

Create a Twitter Application



Go https://apps.twitter.com/ and sign with your twitter account to create twitter application.

Select the option to create a new application and fill in the required information as like Name, Description and Website.
TwitterTweepy

When then new application is created, check your cunsumer Key (API Key) and comsum Secret (API Secret) and modify app permission to Read, Write, and direct messages.
Key and Access Tokens

Install the Tweepy




  • Download Tweepy




sudo apt-get install python-pip
pip install tweepy



  • Install Tweepy




sudo python setup.py install



  • Run test




./run_test.sh
sudo apt-get install curl


Make Twitter' Bot with Tweepy



We will tweet a post after OAuth.


  • OAuth

    [code lang=python]
    import webbrowser
    import tweepy

    C_KEY = 'enter your cunsumer key'
    C_SECRET = 'enter your cunsumer secret'

    auth = tweepy.OAuthHandler(C_KEY, C_SECRET)
    auth_url = auth.get_authorization_url()
    print ('Open the URL:'+auth_url)
    webbrowser.open(auth_url)

    input_pin = raw_input ('PIN:').strip()
    token = auth.get_access_token(verifier=input_pin)
    print 'ACCESS KEY = %s' % token[0]
    print 'ACCESS SECRET = %s' % token[1]
    [/code]

    When "webbrowser.open()" is executed, Web-browser is open and sign with your twitter account.
    Then, click button of "Authoize app" to be able to your application, as like below figure.
    Authrioze

    Next, you can get PIN number for the authorization.
    AuthorizePin


  • Tweet
    You can post on your twitter with update_status() API.

    [code lang=python]
    import tweepy
    C_KEY = 'enter your cunsumer key'
    C_SECRET = 'enter your cunsumer secret'
    A_KEY = 'enter your access key'
    A_SECRET = 'enter your access secret'

    auth = tweepy.OAuthHandler(C_KEY, C_SECRET)
    auth.set_access_token(A_KEY, A_SECRET)

    api = tweepy.API(auth_handler=auth, api_root='/1.1')
    api.update_status('This message has been sent through Twitter API')
    [/code]


  • Confirm your post on your twitter

    posting


댓글 없음:

댓글 쓰기