LoginSignup
14
12

More than 5 years have passed since last update.

Twitterでリプライが飛んで来たらすぐにいいねする

Last updated at Posted at 2017-08-03

動機

  • Twotter APIを触って見たかったので

本題

  1. https://apps.twitter.com/ にアクセスしてTwitterAppsを適当に作成してください、その後permissionタブで今回行うReadの許可をつけます
  2. key and access tokenからConsumer KeyConsumer secretを控えてください
  3. そしてpythonが入っている事が前提でいきます(私はver2.7で作りました)
  4. pip install tweepyでTwitterを扱うライブラリをインストール
  5. そしたら下のプログラムを適当なpythonファイルに貼り付けて実行してください、このときConsumer KeyConsumer secretをそれぞれ変数に格納してください
access.py
#!/usr/bin/python
#-*- coding: utf-8 -*-
import tweepy

consumer_key = ""#access https://apps.twitter.com/ and get consumer key
consumer_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
print("Access this URL:" + auth.get_authorization_url())
verifier = raw_input('Verifier:')
auth.get_access_token(verifier)
print "Access Token:", auth.access_token
print "Access Token Secret:", auth.access_token_secret

6.するとURLが表示されるのでそこへアクセスし表示されているコードを実行画面に打ち込んでください
7. 認証されると、Access TokenAccess Token Secretが表示されるのでそれぞれまた控えてください
8. 以下のプログラムを適当なpythonファイルに貼り付けて、これまでに控えたキーをそれぞれ変数に格納してください.あと自分のTwitterIDも@抜きで格納してください

replyBot.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import tweepy
import datetime

class Listener(tweepy.StreamListener):
    def on_status(self, status):
        status.created_at += datetime.timedelta(hours=9)

        #favo when reply
        if str(status.in_reply_to_screen_name)==Twitter_ID and str(status.user.screen_name)!=Twitter_ID:
            print(str(datetime.datetime.today()))
            api.create_favorite(status.id)
        return True

    def on_error(self, status_code):
        print('Error code:' + str(status_code))
        return True

    def on_timeout(self):
        print('Timeout error')
        return True



consumer_key = ""#access s and get consumer key
consumer_secret = ""
access_token = ""
access_secret = ""
Twitter_ID = ""#Your TwitterID(Don't need @)

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)

listener = Listener()
stream = tweepy.Stream(auth, listener)
stream.userstream()

9.そしてあとは実行すれば完了です.ちなみに上のプログラムは自分が自分にリプライを飛ばしてもファボしないようにしてます

終わり

  • 以上ですが、APIの使用は簡単で誰でも簡単にBotを作れると思います.
14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12