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 Pattern_Module_Twitter_Stream(): # Start Stop Watch t1 = time.time() # Create a list to Store the Data List = Twitter().stream('#Fail') # For 10 Instances for second in range(10): # Get Stream Data value = List.update(bytes=1024) # Add Value to List if not Empty if len(value) == 0: # Pass continue else: # Storing Results List.append() # Print Tweet print('Tweet: %s') % (value.text) # Get Sentiment print('Sentiment Analysis of Tweet: %s') % (TextBlob( str(value.text), analyzer=NaiveBayesAnalyzer()).sentiment[0].upper()) # Wait 3 Seconds between queries - Do not want to get blocked time.sleep(3) return time.time() - t1
def Pattern_Module_Twitter_Stream(): # Start Stop Watch t1 = time.time() # Create a list to Store the Data List = Twitter().stream('#Fail') # For 10 Instances for second in range(10): # Get Stream Data value = List.update(bytes=1024) # Add Value to List if not Empty if len(value) == 0: # Pass continue else: # Storing Results List.append() # Print Tweet print('Tweet: %s') % (value.text) # Get Sentiment print('Sentiment Analysis of Tweet: %s') % (TextBlob(str(value.text), analyzer=NaiveBayesAnalyzer()).sentiment[0].upper()) # Wait 3 Seconds between queries - Do not want to get blocked time.sleep(3) return time.time() - t1
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, time from pattern.web import Twitter s = Twitter().stream( '#joy, #happiness, #hopeful, #pleasure, #harmony, #kindness, #affection, #love' ) for i in range(250): time.sleep(1) s.update(bytes=1024) print s[-1].text if s else ''
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()