Created
December 2, 2014 18:29
-
-
Save lan496/7d311a5a770ad6eb848a to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 -*- | |
from requests_oauthlib import OAuth1 | |
import requests | |
import json | |
from datetime import datetime,timedelta | |
import secret | |
def streaming(): | |
consumerKey=secret.twDict['consumer_key'] | |
consumerSecret=secret.twDict['consumer_secret'] | |
accessToken=secret.twDict['access_token_key'] | |
accessSecret=secret.twDict['access_token_secret'] | |
url='https://stream.twitter.com/1.1/statuses/sample.json' | |
auth=OAuth1(consumerKey,consumerSecret,accessToken,accessSecret) | |
res=requests.get(url,auth=auth,stream=True) | |
start=datetime.now() | |
range_time=timedelta(minutes=20) | |
meshitero=0 | |
N=0 | |
for line in res.iter_lines(): | |
if line: | |
data=json.loads(line) | |
try: | |
if data[u'lang']=='ja': | |
if data[u'text'].find('http')==-1: | |
N+=1 | |
if data[u'text'].find(u'飯テロ')!=-1: | |
meshitero+=1 | |
except: | |
pass | |
end=datetime.now() | |
if end>start+range_time: | |
break | |
return [start.strftime("%Y/%m/%d %H:%M:%S"),(start+range_time).strftime("%Y/%m/%d %H:%M:%S"),meshitero,N] | |
def main(): | |
f=open('meshitero.txt','a') | |
for e in streaming(): | |
f.write(str(e)) | |
f.write(' ') | |
f.write('\n') | |
f.close() | |
if __name__=='__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment