Last active
October 29, 2016 10:18
-
-
Save hapo31/bddf2a624cc1ee98e2a47ae56c915dcd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
# -*- coding: utf-8 -*- | |
import re | |
import tweepy | |
consumer_key = "" | |
consumer_secret = "" | |
access_token = "" | |
access_token_secret = "" | |
#反応するツイートの正規表現 | |
regstr = "^きゃ$" | |
class MyStreamListener(tweepy.StreamListener): | |
def __init__(self, auth): | |
super(MyStreamListener, self).__init__(api=tweepy.API(auth)) | |
self._repatter = re.compile(regstr) | |
# target_screen_nameと相互フォローかどうかをチェック | |
def check_friendship(self, target_screen_name): | |
source = self.api.me().screen_name | |
# targetが自分なら常にTrue | |
if(source == target_screen_name): | |
return True | |
result = self.api.show_friendship(source_screen_name=source, target_screen_name = target_screen_name) | |
# resultは[0]に自分→相手のフォロー情報、[1]にその逆が入っている | |
return result[1].following | |
def on_status(self, status): | |
if(self._repatter.match(status.text)): | |
# ツイートのID | |
tweetid = status.id | |
# ツイートしたユーザーのScreenName | |
tweetuser_screenname = status.author.screen_name | |
# ツイート本文 | |
tweetstr = "@%s な" % (tweetuser_screenname) | |
try: | |
# その人と相互フォローであれば投稿する | |
if(self.check_friendship(status.author.screen_name) and tweetstr): | |
self._rec_tweet = self.api.update_status(status = tweetstr, in_reply_to_status_id = tweetid) | |
except tweepy.error.TweepError as e: | |
pass | |
return True | |
def on_error(self, status): | |
# WriteLogFile("on_error:%s" % status) | |
print(status) | |
def init(): | |
auth = tweepy.OAuthHandler( | |
consumer_key = consumer_key, | |
consumer_secret = consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
mystream = MyStreamListener(auth=auth) | |
stream = tweepy.Stream(auth=auth, listener=mystream) | |
stream.userstream() | |
if __name__ == '__main__': | |
while True: | |
try: | |
init() | |
except TypeError as e: | |
print(str(e)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment