Created
June 12, 2013 22:19
-
-
Save toshihikoyanase/5769634 to your computer and use it in GitHub Desktop.
Twitter Streaming APIを叩いて、Geoタグ付きのツイートを収集するPythonスクリプト
This file contains hidden or 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
#!/usr/bin/env python | |
import tweepy | |
import datetime | |
import time | |
# See http://blog.unfindable.net/archives/4257 | |
locationsL=[-180,-90,180,90] | |
class StreamListener(tweepy.StreamListener): | |
def __init__(self, api=None): | |
tweepy.StreamListener.__init__(self, api) | |
d = datetime.date.today() | |
self.out_prefix = "./filter" | |
self.fout = open("%s-%s.jsonl" % (self.out_prefix, d.isoformat()), 'a' ) | |
self.day = d.day | |
def on_data(self, data): | |
if self.day != datetime.date.today().day: | |
self.fout.close() | |
self.fout = open("%s-%s.jsonl" % (self.out_prefix, d.isoformat()), 'a' ) | |
if data.startswith("{"): | |
self.fout.write(data + "\n") | |
def on_timeout(self): | |
self.fout.close() | |
raise Exception | |
consumer_key = "" | |
consumer_secret = "" | |
access_token = "" | |
access_secret = "" | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_secret) | |
stream = tweepy.Stream(auth, StreamListener()) | |
while True: | |
try: | |
stream.filter(locations=locationsL) | |
except Exception: | |
time.sleep(60) | |
stream = tweepy.Stream(auth, StreamListener()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
元にしたプログラムのURLは http://blog.unfindable.net/archives/4257
変更したところは
インストール方法は
使い方は、
出力形式は、
気をつけるところは