Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# TLに流れてる画像をすべて回収
import os
import time
import datetime
import urllib
import json
import tweepy as tp
def get_oauth():
"""設定ファイルから各種キーを取得し、OAUTH認証を行う"""
setting = json.load(open("./setting.json"))
auth = tp.OAuthHandler(setting['CK'], setting['CS'])
auth.set_access_token(setting['Admin_Key'], setting['Admin_Secret'])
return auth
class StreamListener(tp.StreamListener):
def __init__(self, api):
"""コンストラクタ"""
self.api = api
# 保存先
self.old_date = datetime.date.today()
self.mkdir()
def on_error(self, status_code):
"""接続エラー時対策"""
return True
def on_timeout(self):
"""接続タイムアウト時対策"""
return True
def on_status(self, status):
"""UserStreamから飛んできたStatusを処理する"""
# Tweetに画像がついているか
is_media = False
# 日付の確認
now = datetime.date.today()
if now != self.old_date:
self.old_date = now
self.mkdir()
# TweetがRTかどうか
if hasattr(status, "retweeted_status"):
status = status.retweeted_status
# Tweetが引用ツイートかどうか
if hasattr(status, "quoted_status"):
status = status.quoted_status
# 複数枚の画像ツイートのとき
if hasattr(status, "extended_entities"):
if 'media' in status.extended_entities:
status_media = status.extended_entities
is_media = True
# 一枚の画像ツイートのとき
elif hasattr(status, "entities"):
if 'media' in status.entities:
status_media = status.entities
is_media = True
# 画像がついていたとき
if is_media:
# 自分のツイートは飛ばす(RT対策)
if status.user.screen_name != self.api.me().screen_name:
for image in status_media['media']:
if image['type'] != 'photo':
break
# URL, ファイル名
media_url = image['media_url']
root, ext = os.path.splitext(media_url)
filename = str(self.fileno).zfill(5)
# ダウンロード
try:
temp_file = urllib.request.urlopen(media_url+":orig").read()
except:
print("Download Error")
continue
# 保存
out = open(self.base_path + filename + ext, "wb")
out.write(temp_file)
out.close()
print("Save : {}-{}".format(status.user.screen_name, filename))
self.fileno += 1
temp_file = None
def mkdir(self):
"""保存用のフォルダーを生成し、必要な変数を初期化する"""
self.base_path = "./" + self.old_date.isoformat() + "/"
if os.path.exists(self.base_path) == False:
os.mkdir(self.base_path)
self.fileno = 0
def main():
"""メイン関数"""
auth = get_oauth()
stream = tp.Stream(auth, StreamListener(tp.API(auth)), secure=True)
print('Start Streaming!')
while True:
try:
stream.userstream()
except KeyboardInterrupt:
exit()
except:
print('UserStream Error')
time.sleep(60)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.