def collect(self): #with tweetstream.FilterStream(self.twitterUser, self.twitterPass, track=self.words) as stream: with tweetstream.SampleStream(self.twitterUser, self.twitterPass) as stream: for tweet in stream: #print tweet.get('text') if self.count and self.count % 100 == 0: print "done with %d tweets in %s" % (self.count, datetime.now()) if (self.count >= self.limit): raise Exception('done') if tweet.get('text'): if 'rt' in tweet.get('text').lower(): continue self.queue.put(tweet) self.count += 1
#GetSearch(self, term=None, geocode=None, since_id=None, max_id=None, until=None, count=15, lang=None, locale=None, result_type='mixed', include_entities=None) #Return twitter search results for a given term. #36.35,-86.92 #polygon 8mi #Nashville 30mi #<onset>2013-09-10T15:31:00-05:00</onset> <expires>2013-09-10T16:00:00-05:00</expires> #statuses = api.GetSearch(term = "FEMA", count = 100, geocode=("36.350000","-86.920000","3000mi")) #statuses = api.GetSearch(geocode="36.35,-86.92,3000mi", until="2013-09-11", count = 10) #statuses = api.GetSearch(until="2013-09-11", count = 10) #statuses = api.GetSearch(term = "snow", count = 100000, geocode=("45.023513", "-109.915807", "100mi"), result_type="recent") #print statuses #dumpfile = open("statuses_100000_recent_geocode_snow.pkl", "wb") #pickle.dump(statuses, dumpfile) #dumpfile.close() #for status in statuses: # print status.text # print "\n\n" stream = tweetstream.SampleStream("*****@*****.**", "karidola") for tweet in stream: print tweet
#!/usr/bin/env python import tweetstream, twitter stream = tweetstream.SampleStream("*****@*****.**", "825123") api = twitter.Api( consumer_key='NINW6eFEgEAOUAFAX5bVpQ', consumer_secret='SjJDB2Bpk0IjiEWlptCUkJsluq17tockkAQqJCSs', access_token_key='SjJDB2Bpk0IjiEWlptCUkJsluq17tockkAQqJCSs', access_token_secret='QxwE8socZx7gxcNJlrKMveDYWeyhIir3cvh2KgO5wHI') print api.VerifyCredentials() # statuses = api.GetUserTimeline('gsm1011') # print [s.user.name for s in statuses] users = api.GetFriends() print[u.name for u in users]
def create_stream(): return tweetstream.SampleStream(tpconf.USERNAME, tpconf.PASSWORD)
def send_tweets(server, user, password): stream = tweetstream.SampleStream(user, password) for tweet in stream: broadcast_msg(server, '/tweets', 'tweet', tweet)
parser.add_argument('-s', '--s', dest='s', action='store', type=float, required=True, help='Minimum frequency') parser.add_argument('-c', '--credentials', dest='credentials', action='store', default="./.tpass", help='File with Twitter credentials (username and password, separated by a space)') args = parser.parse_args() return args args = parse_command_line_arguments() username,password = open(args.credentials).read().strip().split() stream = tweetstream.SampleStream(username,password) lc = LossyCounter(args.s) N=0 while True: try: t = stream.next() except Exception, e: print e break if t.has_key("text"): tags = re.findall("#[A-Za-z0-9]+", t["text"]) if len(tags) > 0: for tag in tags: print tag N+=1
import tweetstream import codecs stream = tweetstream.SampleStream("ID", "password"); f = codecs.open("dumpfile", "w", encoding="UTF-8"); counter=0; for tweet in stream: counter=counter+1; try: f.write(counter+" TEXT: "+tweet["text"]); except KeyError: print "No text field"; try: g=tweet["geo"]; #try to print the geographical location of the twitter try: if(not g is None): print counter," LOCATION: ",g; except AttributeError: g=None; #print "AttributeError, g=|",g,"|"; except KeyError: g=None;
# Connect to Mongo try: c = Connection(host="localhost", port=27017) print "Connected successfully" except ConnectionFailure, e: sys.stderr.write("Could not connect to MongoDB: %s" % e) sys.exit(1) db = c['haiku'] # Open connection to Twitter's public timeline, build haikus failed = True while failed: failed = False try: with tweetstream.SampleStream(USER, PASS) as stream: for tweet in stream: if 'text' in tweet and len(tweet['text']) > 0: screen_name = tweet['user']['screen_name'] hashes = [ j for j in set([ i for i in tweet['text'].split() if i.startswith('#') ]) ] # Strip out urls, punctuation, RTs, and @'s tweet_stripped = urlre.sub('', tweet['text']) tweet_stripped = punctre.sub('', tweet_stripped) tweet_stemmed = [ porter_stemmer.stem_word(i.lower()) for i in tweet_stripped.split()
def tweetFilter(uname, psswd): stream = tweetstream.SampleStream(uname, psswd) for tweet in stream: if 'user' in tweet.keys() and tweet['user']['lang'] == 'en': yield tweet
from pymongo.errors import ConnectionFailure import tweetstream import string def normalize(s): ret = s for p in string.punctuation: ret = ret.replace(p, '') return ret """ Connect to MongoDB """ try: c = Connection(host="localhost", port=27017) except ConnectionFailure, e: sys.stderr.write("Could not connect to MongoDB: %s" % e) sys.exit(1) db = c['nba_tweets'] with open('nba_queries.txt') as f: queries = [i.strip() for i in f.readlines()] with tweetstream.SampleStream('username', 'pass') as stream: for tweet in stream: if 'text' in tweet.keys() and len(tweet['text']) > 0: if True in [i.lower() in queries for i in tweet['text'].split()]: print tweet['text'] db.tweets.insert(tweet)
ret = ret.replace(p, '') return ret def is_english_word(word): return word.lower() in english_words afinn = dict( map(lambda (k, v): (k, int(v)), [line.split('\t') for line in open("AFINN/AFINN-111.txt")])) if __name__ == '__main__': romney_count = 0 obama_count = 0 with tweetstream.SampleStream(sys.argv[1], sys.argv[2]) as stream: for tweet in stream: if 'text' in tweet.keys() and len(tweet['text']) > 0: try: no_punct = normalize(tweet['text']) if no_punct != None: val = sum([ afinn[i] if i in afinn else 0 for i in no_punct.split() ]) if val < -2 or val > 2: if any([ i in ['romney', 'mitt'] for i in no_punct.split() ]): romney_count += 1