Skip to content

Instantly share code, notes, and snippets.

@inaz2
Last active January 4, 2016 05:29
Show Gist options
  • Save inaz2/9c330a7405daae292d3e to your computer and use it in GitHub Desktop.
Save inaz2/9c330a7405daae292d3e to your computer and use it in GitHub Desktop.
Twitter trends streaming by using Tweepy
$ ENV/bin/python twtrend.py
#あなたの薄い本できました / 仕事始め / #あなたが今年度好かれる人 / アクシーズ / ヒルナンデス / #観察ちゃんと呟くとフォロ爆が来る / 頭の中の言葉 / #tama954 / #スタパ / 日経平均 / 国会開会式 / シルバニアファミリー / 混物語 / インド北東部 / スマホ解約不自由 / 仕事はじめ / イラン / 搭乗車両 / Hey!Say!JUMPの出番 / Lenovo / 水沼宏太選手 / テガミバチ / ヴァニスタカフェ / ビビット / メンテ前 / 食戟 のソーマ / 信号ナシ・速度無制限 / 鳥栖新監督 / 葦木場くん / 法的措置 / 美人市長 / マッシモ / サンカフェ / 左門くん / ゆるかわ隕石本 / マーガレット / 片野坂 / シクフェス / うすた展 / #あなたが才能を持っているブキ / #あなたに似合うあだ名ベスト3 / #嫌なマンションポエム / #東京女子プロレス / #kokkai / #武装神姫ファンの集まる居酒屋を考える会 / #happy_inter / #thebridge_jp / #スプラトゥーン武器縛 りプレイ
nyagonyago (@nyagonyago1): RT @higakubo: 反対だけでは政治は前に進まない。民主党や共産党などの野党は現実的で具体的な対案を出さず、反対ばかり唱えて、本会議場でヤジを飛ばすが、政治を前に進めているのは今の自民・公明政権。政治は結果。反対だけなら政治家でなくても誰でもできる。#kokkai #N…
変態仮面ひできは帰省中 (@azusatdn): 今年変態仮面ひできを好きになる人は… ーーーーーーーーーー 「岡本【男】」 「大松【女】」 #あなたが今年度好かれる人 https://t.co/Tx8VIIpPtq 大松「いないぞ」
Neel Chauhan (@NeelChauhan30): Lenovo Vibe P1 Review: The Lenovo Vibe P1 is crammed with plenty of useful features and aims to be a truly who... https://t.co/wSIt2fXRvN
よっし (@yosshi_onew): ~よっしの薄い本、新作出来!~ 『ジューシーな桃尻』 ・よっし総受け ・友情系 ・1,000円 ・よっし本人を一晩プレゼント *ご予約はRTで #あなたの薄い本できました https://t.co/Cd6Ac6UJLv ジューシーな桃尻やめろw
とよ (@toyo_46): ~路地裏の少年の薄い本、新作出来!~ 『ヤバイ扉の開き方』 ・路地裏の少年総受け ・サイコ系 ・1,100円 ・路地裏の少年の奇声の音声ファイルつき *ご予約はRTで #あなたの薄い本できました https://t.co/JNftgBPkxy 奇声
ゆみこ@ふぃるまめんと (@iumico): RT @ttakahasi: この地に住まう。我が選択に杭無し。 #嫌なマンションポエム
意対秘人 (@itaihito2015): ~意対秘人の薄い本、新作出来!~ 『日参児童舎』 ・意対秘人総受け ・サイコ系 ・800円 ・意対秘人のお手製藁人形つき *ご予約はRTで #あなたの薄い本できました https://t.co/aCFU7REz8x
(snip)
from __future__ import absolute_import, print_function
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import API
import json
# Go to http://apps.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key=""
consumer_secret=""
# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token=""
access_token_secret=""
class StdOutListener(StreamListener):
""" A listener handles tweets that are received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""
def on_data(self, data):
o = json.loads(data)
line = "%(name)s (@%(screen_name)s): " % o['user']
line += o['text'].replace('\n', ' ')
print(line)
return True
def on_error(self, status):
print(status)
if __name__ == '__main__':
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = API(auth)
o = api.trends_place(23424856) # Japan
queries = [x['name'] for x in o[0]['trends']]
print(' / '.join(queries) + '\n')
stream = Stream(auth, l)
stream.filter(track=queries)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment