Created
December 6, 2017 16:48
-
-
Save nomunomu0504/6242121f397c54cd127f178edaabd922 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
class AutoReply(StreamListener): | |
def on_connect(self): | |
print("on_connect") | |
def on_status(self, status): | |
# Ubuntuの時は気づかなかったんだけど、Windowsで動作確認してたら | |
# created_atはUTC(世界標準時)で返ってくるので日本時間にするために9時間プラスする。 | |
status.created_at += timedelta(hours=9) | |
print('--------------------') | |
# @tweetが存在しているかどうか | |
if len(status.entities['user_mentions']) != 0: | |
# print status.entities['user_mentions'][0] | |
# reply or RTかどうかの判定 | |
if status.entities['user_mentions'][0]['indices'][0] == 0: # Reply | |
# だれに対してreplyしているのか | |
# status.entities['user_mentions'][0]['screen_name']; 誰に | |
# status.author.screen_name; 誰から | |
if status.entities['user_mentions'][0]['screen_name'] == "nomunomu0504" \ | |
and status.author.screen_name == "@aaaaaaaa": # 特定のScreen_name | |
# アップロードする画像の用意 | |
files = {"media": open('image_file.png', 'rb')} | |
# 画像ファイルのアップロード | |
req_media = twitter.post(mediaUploadAdress, files=files) | |
# 画像アップロードが成功したかどうか | |
if req_media.status_code != 200: | |
print("画像アップデート失敗: %s", req_media.text) | |
exit() | |
# アップロードしたファイルのmedia番号の取得 | |
media_id = json.loads(req_media.text)['media_id'] | |
# mentionに対してreplyを返す | |
tweetParams = { | |
'status': u'@{name} image_test'.format(name=status.author.screen_name), | |
'in_reply_to_status_id': status.id, | |
"media_ids": [media_id] | |
} | |
twitter.post(tweetAdress, params=tweetParams) | |
else: | |
pass | |
else: | |
print("entities is NULL") | |
print(u"{id}: {screen_name}({name})\n {tweet}".format( | |
id=status.id, | |
screen_name=status.author.screen_name, | |
name=status.author.name, | |
tweet=status.text | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment