12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TwitterのStreaming APIを使い任意のキーワードでツイートを取得

Last updated at Posted at 2015-05-15

TwitterのStreaming APIを使って、任意のキーワードを取得。
結局home_timelineだとフォローしている人だけだからStreaming使わないとデータ収集にならない。
ということで散々語り尽くされていますが今更Streaming APIを触ってみました。

stream.py

#!/user/bin/env python
# -*- coding: utf-8 -*-
from requests_oauthlib import OAuth1Session
import json
import requests
import time, calendar
import sys, codecs

word = raw_input(u"input: ")

C_KEY = "*************************************"
C_SECRET = "*************************************"
A_TOKEN = "*************************************"
T_SECRET = "*************************************"


URL = "https://stream.twitter.com/1.1/statuses/filter.json"
 
def Client_key():
    return OAuth1Session(C_KEY,
        client_secret = C_SECRET,
        resource_owner_key = A_TOKEN,
        resource_owner_secret = T_SECRET
    )
 
def Response(client, **filter_data):
    return client.post(
        URL,
        data = filter_data,
        stream = True
    )


def YmdHMS(created_at):
    time_utc = time.strptime(created_at, '%a %b %d %H:%M:%S +0000 %Y')
    unix_time = calendar.timegm(time_utc)
    time_local = time.localtime(unix_time)
    return int(time.strftime("%Y%m%d%H%M%S", time_local))

def Print_l(r):	
	for line in r.iter_lines():
		tweet = json.loads(line)
		
		Created_at = YmdHMS(tweet["created_at"])
		User = (tweet["user"]["screen_name"].encode("utf-8"))
		Name = (tweet["user"]["name"].encode("utf-8"))
		Text = (tweet["text"].encode("utf-8"))
		
		try:
			if tweet["user"]["lang"] == "ja":
				print "ID: ", User
				print "ユーザー名: ", Name
				print "本文: ", Text
				print Created_at
				print "==" * 40
		except:
			pass
 
if __name__ == "__main__":
    client = Client_key()
    r = Response(client, track=word)
    Print_l(r)

リアルタイムなモノを取得するわけですから、変なキーワードの場合は見つからなくてjsonオブジェクトが無いよ!って怒られます。#ハッシュタグとかを一方的に取得しに行くほうが良さげ。

created_atの日本時間への変更は
http://blog.unfindable.net/archives/4302
上記URLを使用させていただきました。

スクリプトの終了はctr+cで。

12
13
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
12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?