def create_stream(phrase, queue): """ Celery task that connects to the twitter stream and runs a loop, periodically emitting tweet information to all connected clients. """ local = SocketIO(message_queue=queue) stream = Twitter().stream(phrase, timeout=30) for i in range(60): stream.update() for tweet in reversed(stream): sentiment = classify_tweet(tweet) x, y = vectorize_tweet(tweet) local.emit( 'tweet', { 'id': str(i), 'text': str(tweet.text.encode('ascii', 'ignore')), 'sentiment': sentiment, 'x': x, 'y': y }) stream.clear() time.sleep(1) return queue
def TwitterStream(): # Another way to mine Twitter is to set up a stream. # A Twitter stream maintains an open connection to Twitter, # and waits for data to pour in. # Twitter.search() allows us to look at older tweets, # Twitter.stream() gives us the most recent tweets. for trend in Twitter().trends(cached=False): print trend # It might take a few seconds to set up the stream. stream = Twitter().stream("i love", timeout=30) pos_count=0 neg_count=0 #while True: for i in range(50): if(neg_count): ratio = pos_count / neg_count else: ratio = 0 print str(pos_count) + " " + str(neg_count) + " " + str(ratio)+"%" #print i #print "+ " + str(pos_count) #print "- " + str(neg_count) #print "- - -" # Poll Twitter to see if there are new tweets. stream.update() # The stream is a list of buffered tweets so far, # with the latest tweet at the end of the list. for tweet in reversed(stream): print tweet.text print tweet.language sent = pol(tweet.text) if(sent>0): pos_count+=1 else: neg_count+=1 # Clear the buffer every so often. stream.clear() # Wait awhile between polls. time.sleep(1) print "Final Twitter" print pos_count print neg_count
def create_stream(phrase, queue): local = SocketIO(message_queue=queue) stream = Twitter().stream(phrase, timeout=120) for i in range(120): stream.update() for tweet in reversed(stream): sentiment = classify_tweet(tweet) x, y = vectorize_tweet(tweet) local.emit( 'tweet', { 'id': str(i), 'text': str(tweet.text.encode('ascii', 'ignore')), 'sentiment': sentiment, 'x': x, 'y': y }) stream.clear() time.sleep(1) return queue
def create_stream(phrase, queue): """ Celery task that connects to the twitter stream and runs a loop, periodically emitting tweet information to all connected clients. """ local = SocketIO(message_queue=queue) stream = Twitter().stream(phrase, timeout=30) for i in range(60): stream.update() for tweet in reversed(stream): sentiment = classify_tweet(tweet) x, y = vectorize_tweet(tweet) local.emit('tweet', {'id': str(i), 'text': str(tweet.text.encode('ascii', 'ignore')), 'sentiment': sentiment, 'x': x, 'y': y}) stream.clear() time.sleep(1) return queue
import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..")) import time from pattern.web import Twitter # Another way to mine Twitter is to set up a stream. # A Twitter stream maintains an open connection to Twitter, # and waits for data to pour in. # Twitter.search() allows us to look at older tweets, # Twitter.stream() gives us the most recent tweets. # It might take a few seconds to set up the stream. stream = Twitter().stream("I hate", timeout=30) #while True: for i in range(10): print(i) # Poll Twitter to see if there are new tweets. stream.update() # The stream is a list of buffered tweets so far, # with the latest tweet at the end of the list. for tweet in reversed(stream): print(tweet.text) print(tweet.language) # Clear the buffer every so often. stream.clear() # Wait awhile between polls. time.sleep(1)
import os, sys; sys.path.insert(0, os.path.join("..", "..")) import time from pattern.web import Twitter # Another way to mine Twitter is to set up a stream. # A Twitter stream maintains an open connection to Twitter, # and waits for data to pour in. # Twitter.search() allows us to look at older tweets, # Twitter.stream() gives us the most recent tweets. # It might take a few seconds to set up the stream. stream = Twitter().stream("I hate", timeout=30) #while True: for i in range(100): print i # Poll Twitter to see if there are new tweets. stream.update() # The stream is a list of buffered tweets so far, # with the latest tweet at the end of the list. for tweet in reversed(stream): print tweet.text print tweet.language # Clear the buffer every so often. stream.clear() # Wait awhile between polls. time.sleep(1)
from pattern.web import Twitter import time s = Twitter().stream('#snowday') for i in range(25): time.sleep(1) s.update(bytes=1024) print s[-1].text if s else '' s.clear()
from pattern.web import Twitter import time s = Twitter().stream('#college') for i in range(25): time.sleep(1) s.update(bytes=1024) print s[-1].text if s else '' s.clear()