Get tweets with arbitrary keywords using Twitter's Streaming API

Get arbitrary keywords using Twitter's Streaming API. After all, if it is home_timeline, only the people who follow it will not be able to collect data unless Streaming is used. It's been talked about a lot, but now I've touched the 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 "username: ", Name
				print "Text: ", Text
				print Created_at
				print "==" * 40
		except:
			pass
 
if __name__ == "__main__":
    client = Client_key()
    r = Response(client, track=word)
    Print_l(r)

Since you get real-time things, if you have a strange keyword, you can't find it and there is no json object! I get angry. It's better to go get the #hashtag unilaterally.

The change of created_at to Japan time http://blog.unfindable.net/archives/4302 I used the above URL.

End the script with ctr + c.

Recommended Posts

Get tweets with arbitrary keywords using Twitter's Streaming API
Get tweets containing keywords using Python Tweepy
Automatic follow-back using streaming api with Tweepy
Get Tweets with Tweepy
Continue to retrieve tweets containing specific keywords using the Streaming API in Python
Get information with zabbix api
Get ranking with Rakuten API
Get Salesforce data using REST API
Get reviews with python googlemap api
Get Amazon data using Keep API # 1 Get data
Create API using hug with mod_wsgi
Streaming speech recognition with Google Cloud Speech API
Get mail using Gmail API in Java
Try using Dropbox API v2 with Go
Get replies to specific tweets with tweepy
Get Youtube data in Python using Youtube Data API
Get holidays with the Google Calendar API
Extract sudden buzzwords with twitter streaming API
Play with puns using the COTOHA API
Get coincheck virtual currency information with API ♪
Get lots of your tweets with Tweepy
[Python] Get all comments using Youtube Data API
Get image URL using Flickr API in Python
Get stock price data with Quandl API [Python]
How to get article data using Qiita API
Recent ranking creation using Qiita API with Python
Get Gmail subject and body with Python and Gmail API