def test_follow_encoding(self): s = Stream(None, None) s._start = lambda is_async: None s.filter(follow=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.body['follow'])
class TweepyStreamTests(unittest.TestCase): def setUp(self): self.auth = create_auth() self.listener = MockStreamListener(self) self.stream = Stream(self.auth, self.listener, timeout=3.0) def tearDown(self): self.stream.disconnect() def test_userstream(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream() self.assertEqual(self.listener.status_count, 1) def test_sample(self): self.listener.status_stop_count = 10 self.stream.sample() self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_filter_track(self): self.listener.status_stop_count = 5 phrases = ['twitter'] self.stream.filter(track=phrases) self.assertEquals(self.listener.status_count, self.listener.status_stop_count)
class IRCPlugin(BasePlugin): name = "twitter_stream" enabled = True def _validate(self, event): return False def stop(self): if self._stream: self._stream.disconnect() def run(self): try: auth = tweepy.OAuthHandler(config.TWITTER_CONSUMER_KEY, config.TWITTER_CONSUMER_SECRET) auth.set_access_token(config.TWITTER_ACCESS_KEY, config.TWITTER_ACCESS_SECRET) self._stream = Stream(auth, StreamerToIrc(self)) LOG.info("Twitter stream successfully initializing") except Exception as e: LOG.error("Twitter stream authorization failed: %s" % e) return followers = [] api = API(auth) for f in config.TWITTER_FOLLOW_IDS: if isinstance(f, (str, unicode)): try: user_id = api.get_user(f).id followers.append(str(user_id)) except Exception: LOG.debug("Can't get ID for %s" % user_id) continue else: followers.append(str(f)) self._stream.filter(followers)
def startBot(): print("") print("Initializing....") getBanAccounts() print("getting banned accounts....check") getBanWords() print("getting banned words....check") getTrackWords() print("getting track words....check") getFollowAccounts() print("getting follow accounts....check") getWhiteListAccounts() print("getting whitelist accounts....check") print("") print("""\ _____ _ _ _ _____ _____ _____ |_ _|_ _ _|_| |_| |_ ___ ___| __ | |_ _| | | | | | | | _| _| -_| _| __ -| | | | | |_| |_____|_|_| |_| |___|_| |_____|_____| |_| created by vishwenga Running.... :) """) try: twt = Stream(auths, listener()) twt.filter(track=track_words) except Exception as e: print(str(e)) pass
def test_track_encoding(self): s = Stream(None, None) s._start = lambda is_async: None s.filter(track=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.body['track'])
class TweepyStreamTests(unittest.TestCase): def setUp(self): self.auth = create_auth() self.listener = MockStreamListener(self) self.stream = Stream(self.auth, self.listener, timeout=3.0) def tearDown(self): self.stream.disconnect() def test_userstream(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream() self.assertEqual(self.listener.status_count, 1) def test_sample(self): self.listener.status_stop_count = 10 self.stream.sample() self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_filter_track(self): self.listener.status_stop_count = 5 phrases = ['twitter'] self.stream.filter(track=phrases) self.assertEquals(self.listener.status_count, self.listener.status_stop_count)
def filter_track(follow): auth = OAuthHandler(consumer_key2, consumer_secret2) auth.set_access_token(access_token2, access_token_secret2) stream = Stream(auth, MyStreamListener()) api = API(auth) #print 'start filter track ', ','.join(track) stream.filter(track=follow)
def test_follow_encoding(self): s = Stream(None, None) s._start = lambda async: None s.filter(follow=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.session.params['follow'])
def main(): """ Main run function """ set_logging() logger = logging.getLogger(f"{__name__}.main") auth = tweepy.OAuthHandler(os.getenv("CONSUMER_KEY"), os.getenv("CONSUMER_KEY_SECRET")) auth.set_access_token(os.getenv("ACCESS_TOKEN"), os.getenv("ACCESS_TOKEN_SECRET")) api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) following = get_following_users(api) following.extend(get_default_users(api)) tracks = get_following_searches() logger.info(f"Following {len(following)} users and {len(tracks)} searches") while True: listener = Listener() stream = Stream(auth=api.auth, listener=listener) try: logger.info("Started streaming") stream.filter(follow=following, track=tracks) except KeyboardInterrupt: logger.info("Stopped") break except ReadTimeoutError as exc: logger.error("Handled exception:", str(exc), exc_info=True) finally: logger.info("Done") stream.disconnect()
class KeywordStreamer: ''' Streams in tweets matching a set of terms/from a set of user streams. ''' def __init__(self, kw_list): self.kws = kw_list self.api = get_api() self.stream = Stream(auth=self.api.auth, listener=MyStreamListener()) def streamTweets(self): goForIt = True while goForIt: try: goForIt = False self.stream.filter(track=self.kws) except IncompleteRead as ex: try: tb = traceback.format_exc() except: tb = '' print("%s: %s\n%s\n\n" % (getTimeString(), str(sys.exc_info()) + "\nStill ticking!", tb)) time.sleep(15 * 60) goForIt = True except: try: tb = traceback.format_exc() except: tb = '' print('%s: %s\n%s\n\n' % (getTimeString(), str(sys.exc_info()), tb)) time.sleep(15 * 60) goForIt = True
def test_track_encoding(self): s = Stream(None, None) s._start = lambda is_async: None s.filter(track=['Caf\xe9']) # Should be UTF-8 encoded self.assertEqual('Caf\xe9'.encode('utf8'), s.body['track'])
def test_track_encoding(self): s = Stream(None, None) s._start = lambda async: None s.filter(track=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.session.params['track'])
class TweepyStreamTests(unittest.TestCase): def setUp(self): self.auth = create_auth() self.listener = MockStreamListener(self) self.stream = Stream(self.auth, self.listener, timeout=3.0) def tearDown(self): self.stream.disconnect() def test_userstream(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream() self.assertEqual(self.listener.status_count, 1) def test_userstream_with_params(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream(_with='user', replies='all', stall_warnings=True) self.assertEqual(self.listener.status_count, 1) def test_sample(self): self.listener.status_stop_count = 10 self.stream.sample() self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_filter_track(self): self.listener.status_stop_count = 5 phrases = ['twitter'] self.stream.filter(track=phrases) self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_track_encoding(self): s = Stream(None, None) s._start = lambda async: None s.filter(track=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.session.params['track']) def test_follow_encoding(self): s = Stream(None, None) s._start = lambda async: None s.filter(follow=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.session.params['follow'])
def filter_track(): track = ["python"] stream_auth = BasicAuthHandler('<USERNAME>', '<PASSWORD>') api = API() stream = Stream(stream_auth, MyStreamListener(api)) print 'start filter track ', ','.join(track) stream.filter(track=track)
def get_tweets(keyword): init_tweepy() auth = tweepy.OAuthHandler('QxpECDyg9eM6inD0jo7Q','a0A8OE2ZN7imCg6WR5ygkrGvKG6tNtoZIChQXQ8NIf4') auth.set_access_token('18752311-FXmc9zaPGcszH1bdDNJQa0MY2XRYfYzT3nBRnMqgB','tzXURgYPAbsD1VgmchkoKH9QOJ80qGgSSL13K5A3rY') api = tweepy.API(auth) listener = Listener() stream = Stream(auth, listener) stream.filter(track=[keyword])
def my_main(): args = get_parser().parse_args() auth = OAuthHandler("HqQlPfDvsu6oOVKcbSaC3dwy6", "Nr801nZ9gJoq5D6x1cQewFJeHDBnyI2IqUsYVePv7sQLLLjwxU") auth.set_access_token("2344102761-3D84t8gRyGEE2N9tBM99n8oTjGMdDJb12lIAWJ9", "MEZtBsCwbFjrku0JOnggvlGmqB2O8x8gBC7s65wkjkJGx") print("Streaming...") twitter_stream = Stream(auth, Streamer(args.data_dir)) twitter_stream.filter(track=[search_word])
def filter_track(): q = Scrapers.all().filter('','') follow=[s.uid for s in q] auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) stream = Stream(auth, MyStreamListener()) api = API(auth) #print 'start filter track ', ','.join(track) stream.filter(follow=follow)
def filter_track(): q = Scrapers.all().filter('', '') follow = [s.uid for s in q] auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) stream = Stream(auth, MyStreamListener()) api = API(auth) #print 'start filter track ', ','.join(track) stream.filter(follow=follow)
class TweepyStreamTests(unittest.TestCase): def setUp(self): self.auth = create_auth() self.listener = MockStreamListener(self) self.stream = Stream(self.auth, self.listener, timeout=3.0) def tearDown(self): self.stream.disconnect() def test_userstream(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream() self.assertEqual(self.listener.status_count, 1) def test_userstream_with_params(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream(_with='user', replies='all', stall_warnings=True) self.assertEqual(self.listener.status_count, 1) def test_sample(self): self.listener.status_stop_count = 10 self.stream.sample() self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_filter_track(self): self.listener.status_stop_count = 5 phrases = ['twitter'] self.stream.filter(track=phrases) self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_track_encoding(self): s = Stream(None, None) s._start = lambda async: None s.filter(track=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.parameters['track']) def test_follow_encoding(self): s = Stream(None, None) s._start = lambda async: None s.filter(follow=[u'Caf\xe9']) # Should be UTF-8 encoded self.assertEqual(u'Caf\xe9'.encode('utf8'), s.parameters['follow'])
def main_logger(basename=None): l = RotatingLogListener(basename=basename) auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) stream = Stream(auth, l) # This fetches ANY geotagged tweet: # https://dev.twitter.com/docs/streaming-apis/parameters#locations stream.filter(locations=[-180,-90,180,90])
def train_tweets(): tokyo = LocationFence("Tokyo", 138.946381, 35.523285, 139.953232, 35.906849) hokkaido = LocationFence("Hokkaido", 139.546509, 41.393294, 145.742798, 45.729191) kyusyu = LocationFence("Kyusyu", 129.538879, 31.147006, 131.856995, 33.934245) locations = [tokyo, hokkaido, kyusyu] request_coordinates = [] for l in locations: request_coordinates += l.get_coordinates() stream = Stream(oauth(), Trainer(locations), secure=True) stream.filter(locations=request_coordinates)
def __init__(self): listener = TweetStreamListener() auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) stream = Stream(auth, listener) stream.filter(track=[ "soccer transfer", "transfer news", "premier league", "liverpool" ], is_async=True, languages=["en"]) logging.basicConfig(filename="tweet_stream.log", level=logging.DEBUG) logging.info("Streaming Soccer Related Tweets\n")
class StreamsTestsPlain(object): def __init__(self): self._stream = None self._oauth = None self._oauth_api = None self._stream_init() self._oauth_init() def _oauth_init(self): self._oauth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET) self._oauth.set_access_token( social_keys.TWITTER_APP_ACCESS_TOKEN, social_keys.TWITTER_APP_ACCESS_TOKEN_SECRET) self._oauth_api = API(self._oauth) def _stream_init(self): api1 = API() headers = {} headers["Accept-Encoding"] = "deflate, gzip" stream_auth = BasicAuthHandler(social_keys.TWITTER_HTTP_AUTH_U, social_keys.TWITTER_HTTP_AUTH_P) l = TestStreamListener(api=api1) self._stream = Stream(auth=stream_auth, listener=l, secure=True, headers=headers) def sample(self): self._stream.sample() def filter_follow(self): follow_names = [ 'hnfirehose', 'StockTwits', 'YahooFinance', 'Street_Insider', 'TheStreet', 'SquawkCNBC', 'CNBC', 'AP_PersonalFin', 'themotleyfool', 'MarketWatch', 'Reuters_Biz' ] follow_usr_objs = self._oauth_api.lookup_users( screen_names=follow_names) follow_ids = [] for follow_usr in follow_usr_objs: follow_ids.append(follow_usr.id) print follow_ids self._stream.filter(follow=follow_ids) def filter(self): self._stream.filter(track=["@newsdotme", "@twixmit", "@app_engine"])
def train_tweets(): tokyo = LocationFence("Tokyo", 138.946381, 35.523285, 139.953232, 35.906849) hokkaido = LocationFence("Hokkaido", 139.546509, 41.393294, 145.742798, 45.729191) kyusyu = LocationFence("Kyusyu", 129.538879, 31.147006, 131.856995, 33.934245) locations = [tokyo, hokkaido, kyusyu] request_coordinates = [] for l in locations: request_coordinates += l.get_coordinates() stream = Stream(oauth(), Trainer(locations), secure=True) stream.filter(locations=request_coordinates)
def do_stream(self): listener = twitter_listen() auth = tweepy.OAuthHandler(credentials.CONSUMER_KEY, credentials.CONSUMER_SECRET) api = tweepy.API(auth) auth.set_access_token(credentials.ACCESS_TOKEN, credentials.ACCESS_TOKEN_SECRET) stream = Stream(auth, listener) #filter tweets stream.filter(languages=["en"], track=[ 'Storm', 'Winter', 'Canada', 'Temprature', 'Flu', 'Snow', 'Indoor', 'Safety' ])
def main(): try: config = get_twitter_config(CONFIG_FILE, TWITTER_SCREEN_NAME) auth = tweepy.OAuthHandler(config['consumer_key'], config['consumer_secret']) auth.set_access_token(config['access_key'], config['access_secret']) # auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) # auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) listener = Listener() stream = Stream(auth, listener, timeout=None,secure=1) #stream = Stream(USERNAME, PASSWORD, listener, timeout=None) # sample returns all tweets stream.filter(track = KEYWORDS) except KeyboardInterrupt: print '\nGoodbye!'
def __listen(self): listener = TweetStreamListener(self.api, self.sentiment, self.error) stream = Stream(self.auth, listener) print 'Starting stream...' try: stream.filter(track=self.track) except: print 'Encountered error!' print 'Exiting application' item = { 'status' : 'stream down', 'timestamp' : datetime.utcnow() } self.error.save(item) stream.disconnect()
def main(api: tweepy.API, main_user): """ Authentication and calling the helper function """ listner = MyListner(api) stream = Stream(auth, listner) ans = stream.filter(follow=[f"{main_user}"], is_async=True)
class TweepyStreamTests(unittest.TestCase): def setUp(self): self.auth = create_auth() self.listener = MockStreamListener(self) self.stream = Stream(self.auth, self.listener, timeout=3.0) def tearDown(self): self.stream.disconnect() def test_userstream(self): # Generate random tweet which should show up in the stream. def on_connect(): API(self.auth).update_status(mock_tweet()) self.listener.connect_cb = on_connect self.listener.status_stop_count = 1 self.stream.userstream() self.assertEqual(self.listener.status_count, 1) def test_sample(self): self.listener.status_stop_count = 10 self.stream.sample() self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_filter_track(self): self.listener.status_stop_count = 5 phrases = ['twitter'] self.stream.filter(track=phrases) self.assertEquals(self.listener.status_count, self.listener.status_stop_count) def test_on_data(self): test_wrong_data = [ '{"disc', # this is actual data read from twitter '600', # this is actual data read from twitter '41\n', # this is actual data read from twitter 'obviously non-json', '"json but not dict"', '{"json dict":"but not a twitter message"}' ] for raw_data in test_wrong_data: # should log errors but not raise / not return False self.assertEquals(self.listener.on_data(raw_data), None) self.assertEquals(self.listener.status_count, 0)
class KeywordStreamer: ''' Streams in tweets matching a set of terms/from a set of user streams. ''' def __init__(self, termPath): try: os.mkdir(OUT_DIR) except: pass kwFile = open(termPath, 'r') self.kws = [line.strip() for line in kwFile if line.strip()] kwFile.close() self.stream = Stream(getKeyAndAuth(), TermListener(NUM_TO_CACHE)) def streamTweets(self, searchForTerms=True): goForIt = True while goForIt: try: goForIt = False if searchForTerms: self.stream.filter(track=self.kws, locations=LOCATIONS) else: self.stream.filter(follow=[int(k) for k in self.kws], locations=LOCATIONS) #except IncompleteRead as ex: # try: # tb = traceback.format_exc() # except: # tb = '' # emailAlert("%s: %s\n%s\n\n" % (getTimeString(), # str(sys.exc_info()) + "\nStill ticking!", tb)) # zzz(SNOOZE) # goForIt = True except: try: tb = traceback.format_exc() except: tb = '' emailAlert('%s: %s\n%s\n\n' % (getTimeString(), str(sys.exc_info()), tb)) zzz(SNOOZE) goForIt = True
class StreamsTestsPlain(object): def __init__(self): self._stream = None self._oauth = None self._oauth_api = None self._stream_init() self._oauth_init() def _oauth_init(self): self._oauth = OAuthHandler(social_keys.TWITTER_CONSUMER_KEY, social_keys.TWITTER_CONSUMER_SECRET) self._oauth.set_access_token(social_keys.TWITTER_APP_ACCESS_TOKEN,social_keys.TWITTER_APP_ACCESS_TOKEN_SECRET) self._oauth_api = API(self._oauth) def _stream_init(self): api1 = API() headers = {} headers["Accept-Encoding"] = "deflate, gzip" stream_auth = BasicAuthHandler(social_keys.TWITTER_HTTP_AUTH_U,social_keys.TWITTER_HTTP_AUTH_P) l = TestStreamListener(api=api1) self._stream = Stream(auth=stream_auth,listener=l,secure=True,headers=headers) def sample(self): self._stream.sample() def filter_follow(self): follow_names = ['hnfirehose','StockTwits','YahooFinance','Street_Insider','TheStreet','SquawkCNBC','CNBC','AP_PersonalFin','themotleyfool','MarketWatch','Reuters_Biz'] follow_usr_objs = self._oauth_api.lookup_users(screen_names=follow_names) follow_ids = [] for follow_usr in follow_usr_objs: follow_ids.append(follow_usr.id) print follow_ids self._stream.filter(follow=follow_ids) def filter(self): self._stream.filter(track=["@newsdotme","@twixmit","@app_engine"])
def __init__(self, settings, publishers): """Constructor.""" if "consumer_key" not in settings: raise ValueError("consumer_key must not be None") if "consumer_secret" not in settings: raise ValueError("consumer_secret must not be None") if "access_token" not in settings: raise ValueError("access_token must not be None") if "access_secret" not in settings: raise ValueError("access_secret must not be None") if "fields" not in settings: raise ValueError("access_secret must not be None") self.auth = OAuthHandler(settings["consumer_key"], settings["consumer_secret"]) self.auth.set_access_token(settings["access_token"], settings["access_secret"]) twitter_stream = Stream(self.auth, StdOutListener(settings["fields"], publishers)) twitter_stream.filter(track=settings["post_strings"])
class TweepyStreamTests(unittest.TestCase): def setUp(self): self.auth = create_auth() self.listener = MockStreamListener(self) self.stream = Stream(self.auth, self.listener, timeout=3.0) def tearDown(self): self.stream.disconnect() def on_connect(self): API(self.auth).update_status(mock_tweet()) def test_sample(self): self.listener.status_stop_count = 10 self.stream.sample() self.assertEqual(self.listener.status_count, self.listener.status_stop_count) def test_filter_track(self): self.listener.status_stop_count = 5 phrases = ['twitter'] self.stream.filter(track=phrases) self.assertEqual(self.listener.status_count, self.listener.status_stop_count) def test_track_encoding(self): s = Stream(None, None) s._start = lambda is_async: None s.filter(track=['Caf\xe9']) # Should be UTF-8 encoded self.assertEqual('Caf\xe9'.encode('utf8'), s.body['track']) def test_follow_encoding(self): s = Stream(None, None) s._start = lambda is_async: None s.filter(follow=['Caf\xe9']) # Should be UTF-8 encoded self.assertEqual('Caf\xe9'.encode('utf8'), s.body['follow'])
def main(): try: parser = ArgumentParser() parser.add_argument("oauth_keys_file", help="The location of a file containing the application " + "oath consumer key, consumer secret, " + "access token, and access token secret, " + "each on its own line, in that order. " + "See the tweepy example on oauth to figure " + "out how to get these keys." ) parser.add_argument("mail_settings_file", help="The automail settings file for sending emails.") args = parser.parse_args() (consumer_key, consumer_secret, access_token, access_token_secret) = \ parse_oauth_file(args.oauth_keys_file) auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) pl = PaxListener(args.mail_settings_file, auth) #open a stream, with ourself as the listener stream = Stream(auth, pl) #stream.filter(track=["the"]) pax_user_id = '26281970' #follow requires userid, found at mytwitterid.com daemon_user_id = '1954653840' stream.filter(follow=[pax_user_id]) #track ignores follow, pulls from firehose regardless (this is testing acct) except BaseException as e: subject = "Exception from paxtix" exc_type, exc_value, exc_traceback = sys.exc_info() message = '\n'.join(["Pax tix listener has hit an exception!",] + traceback.format_exception(exc_type, exc_value, exc_traceback), ) send_email(parse_settings(args.mail_settings_file), subject, message) traceback.print_exception(exc_type, exc_value, exc_traceback)
class Bot: consumer_key='FSWJUlCHsAaiLBDY1MMEA' consumer_secret='aCFv6Tslb038cdUAIv7m4QDplBn076wvLd4YKmC3yU' key = "135845084-FONlvU4wGXyl9o4RkfyPdv4grclbqr4Pi2UH2w3j" secret = "DC6I1xp2uONTIYOL2DtEIljhl46LEkUwlPB5NUEBds" def __init__(self): self.auth = tweepy.OAuthHandler(consumer_key = self.consumer_key, consumer_secret = self.consumer_secret) self.auth.set_access_token(self.key, self.secret) self.api = tweepy.API(self.auth) def auth(self): try: redirect_url = self.auth.get_authorization_url() except tweepy.TweepError: print 'Error! Failed to get request token.' verifier = raw_input('Verifier:') try: self.auth.get_access_token(verifier) except tweepy.TweepError: print 'Error! Failed to get access token.' self.key, self.secret = self.auth.access_token.key, self.auth.access_token.secret print self.key, self.secret def stream(self): self.botname = self.api.me().screen_name print "Bot started: @" + self.botname self.taghandler = TagHandler(self.api, self.botname) self.customhandler = CustomHandler(self.api) self.listener = Listener(self.botname, self.taghandler, self.customhandler) self.stream = Stream(self.auth, self.listener) self.stream.filter(track=(self.botname, ))
def handle(self, *args, **options): if not args or len(args) < 3: self.stdout.write('No hashtags and/or options provided.\n') else: # Fetch info from DB hashtag_name = args.pop(0) hashtag = HashTag.objects.get(name=hashtag_name) options = VoteOption.objects.filter(hashtag=hashtag) self.stdout.write('Following: %s\n' % hashtag_name) self.stdout.write('Options: %s\n' % ', '.join(options)) self.stdout.flush() # OAuth connect consumer_key = settings.TWITTERVOTER['consumer_key'] consumer_secret = settings.TWITTERVOTER['consumer_secret'] access_token = settings.TWITTERVOTER['access_token'] access_token_secret = settings.TWITTERVOTER['access_token_secret'] auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Connect too the streaming API. mylisten = MyStreamListener(self.stdout) mystream = Stream(auth, mylisten, timeout=30) mystream.filter(track=args)
import ctes from tweepy.streaming import StreamListener from tweepy.auth import OAuthHandler from tweepy.streaming import Stream import json class StdOutListener(StreamListener): """ A listener handles tweets are the received from the stream. This is a basic listener that just prints received tweets to stdout. """ def on_data(self, data): data=json.loads(data) print data["user"]["name"]+" -> "+data["text"] #print data return True def on_error(self, status): print status if __name__ == '__main__': l = StdOutListener() auth = OAuthHandler(ctes.consumer_key, ctes.consumer_secret) auth.set_access_token(ctes.access_token, ctes.access_token_secret) stream = Stream(auth, l) stream.filter(track=['Espa'])
def retweet_by_all(status): for auth in get_client_auths(): api = tweepy.API(auth) api.retweet(status["id"]) print "i will retweet", status["id"], status["text"] class StdOutListener(StreamListener): # listens to stream and will start retweet_by_all if condition def on_data(self, data): status = json.loads(data) if condition(status): retweet_by_all(status) def on_error(self, status): print status if __name__ == "__main__": # starts main loop auth = get_main_identity_auth() api = tweepy.API(auth) stream = Stream(auth, StdOutListener(StreamListener)) stream.filter(config["listen"])
'source': status.source, } sys.stdout.write(json.dumps(data)) sys.stdout.write('\n') sys.stdout.flush() if __name__ == '__main__': parser = argparse.ArgumentParser(description='Collect geo-tagged Japanese tweets') parser.add_argument('--consumer_key', required=True) parser.add_argument('--consumer_secret', required=True) parser.add_argument('--access_key', required=True) parser.add_argument('--access_secret', required=True) args = parser.parse_args() while True: # Twitter frequently disconnects the streaming api, # so catch such events and try to reconnect after some time # Refer to https://groups.google.com/forum/#!msg/tweepy/o0lpL3WRoyg/2QvDkJr0WvMJ try: auth = get_oauth(consumer_key=args.consumer_key, consumer_secret=args.consumer_secret, access_key=args.access_key, access_secret=args.access_secret) stream = Stream(auth, Listener(), secure=True) stream.filter(languages=['ja'], locations=[127.64249, 26.060477, 145.347467, 45.344336]) except: time.sleep(30)
conn = connect(opts.dsn) cursor = conn.cursor() # Don't know how this happens if status.geo == None: print 'No geo information' return if status.geo['type'] != 'Point': print 'Geo is of type ' + status.geo['type'] return # Insert the status into the DB. wkt = 'SRID=4326;POINT(%s %s)' % (status.geo['coordinates'][1], status.geo['coordinates'][0]) cursor.execute( 'INSERT INTO ' + opts.table + ' (screenname, tweet, the_geom) VALUES ' + '(%s, %s, ST_GeomFromEWKT(%s))', (status.user.screen_name, status.text, wkt)) conn.commit() listener = PGSQLListener() auth = BasicAuthHandler(args[0], args[1]) s = Stream(auth, listener, secure=True) #s.filter(track="foothill college") s.filter(locations=[float(i) for i in opts.geo.split(',')])
c.execute(""" INSERT INTO tweets ( id, tweet_id, author, content, in_reply_to_screen_name, in_reply_to_status_id, in_reply_to_status_id_str, in_reply_to_user_id, in_reply_to_user_id_str, retweet_count, retweeted, created_at, retweet_of ) VALUES ( NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )""", insert_data) conn.commit() return def on_error( self, error ): print >>sys.stderr, error return #auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) #auth_url = auth.get_authorization_url() #print 'Please authorize: ' + auth_url #verifier = raw_input('PIN: ').strip() #auth.get_access_token(verifier) #print "ACCESS_KEY = '%s'" % auth.access_token.key #print "ACCESS_SECRET = '%s'" % auth.access_token.secret #raise SystemExit if __name__ == "__main__": auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) listener = Listener() stream = Stream(auth, listener); stream.filter(track=sys.argv[2:])
#dont forget this return True if __name__ == "__main__": if not len(sys.argv) > 1: args_dict = {'screen_name':'SmartTypes'} else: args_dict = eval(sys.argv[1]) screen_name = args_dict['screen_name'] twitter_user = TwitterUser.by_screen_name(screen_name) auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) monitor_these_user_ids = twitter_user.following_following_ids[:4000] print "Num of users to monitor: %s" % len(monitor_these_user_ids) listener = Listener(monitor_these_user_ids) stream = Stream(auth,listener) stream.filter(follow=monitor_these_user_ids)
def retweet(tweet_sid): try: api.retweet(tweet_sid) except Exception as e: print(str(e)) pass def fav(tweet_sid): try: api.create_favorite(tweet_sid) except Exception as e: print(str(e)) pass track_words = [ 'NJCycling', 'NJ Cycling', 'NewJersey Cycling', 'NJ Bike', 'NJBike' ] #Whatever you want to retweet should go here. follow_acc = [ '19401785' ] #retweets every tweet from this accounts, handles converted to ids print("Running...") try: twt = Stream(auths, listener()) twt.filter(track=track_words) # or follow = follow_acc except Exception as e: print(str(e)) pass
oauth = read_auth(sys.argv[1]) googleauth = read_auth(sys.argv[3]) print(sys.argv[1]) sc = read_search_criteria(sys.argv[2]) print(sys.argv[2]) auth = OAuthHandler(oauth["C_KEY"], oauth["C_SECRET"]) auth.set_access_token(oauth["A_TOKEN"], oauth["A_SECRET"]) api = tweepy.API(auth) #fetch_tweets("/home/zqz/GDrive/papers/chase/dataset/waseem2016/NAACL_SRW_2016.csv", api, # "/home/zqz/Work/chase/data/ml/w/labeled_data_all.csv") #refetch_waseem_data_missed("/home/zqz/GDrive/papers/chase/dataset/waseem2016/NLP+CSS_2016_log", # api, # "/home/zqz/GDrive/papers/chase/dataset/waseem2016/NLP+CSS_2016_tweets_missed.csv") # fetch_waseem_data_v2("/home/zqz/GDrive/papers/chase/dataset/waseem2016/NLP+CSS_2016.csv", api, # "/home/zqz/GDrive/papers/chase/dataset/waseem2016/NLP+CSS_2016_tweets.csv") #print("end") # ===== streaming ===== twitterStream = Stream(auth, TwitterStream(googleauth["key"])) twitterStream.filter(track=[sc["KEYWORDS"]], languages=LANGUAGES_ACCETED) # ===== index existing data ===== # index_data("/home/zqz/Work/chase/data/ml/public/w+ws/labeled_data.csv", # api, 1,2) # searcher = TwitterSearch(auth) # searcher.index(["#refugeesnotwelcome","#DeportallMuslims", "#banislam","#banmuslims", "#destroyislam", # "#norefugees","#nomuslims"])
def init_operator(self, tweet): self.operator = Operator(tweet) def __filter_users(self, user): return user in self.permitted_users def on_status(self, status): if self.__filter_users(status.author.id): # if self.filter_users(status.author.name): text = status.text print(text) self.operator.operate(text, status.id) return True def on_error(self, status): print ("Stream error") return True def on_timeout(self): raise Exception if __name__ == "__main__": settings = sts("json/settings.json") auth = get_oauth(settings.get_consumer_key(), settings.get_consumer_secret(), settings.get_access_token(), settings.get_access_secret()) listener = Listener() listener.set_permitted_users(settings.get_permitted_users()) listener.init_operator(Tweet(auth)) stream = Stream(auth, listener, secure=True) stream.filter(track=['@RaspiBotTwi']) stream.userstream()
time_ms = data['timestamp_ms'] user = data['user']['screen_name'] date = data['created_at'] location = data['user']['location'] print(time_ms, tweet, user, date, location) c.execute( "INSERT INTO entries (unix, tweet, user, date, location) VALUES (?, ?, ?)", (time_ms, tweet, user, date, location)) conn.commit() except KeyError as e: print(str(e)) return (True) def on_error(self, status): print(status) if __name__ == '__main__': while True: try: auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) api = tweepy.API(auth) l = listener() streamer = Stream(auth, l) streamer.filter(track=['USA', 'Syria', 'Russia', 'ISIS', 'Assad']) except BaseException as e: print(str(e)) time.sleep(5)
except Exception as e: print(str(e)) pass def retweet(tweet_sid): try: api.retweet(tweet_sid) except Exception as e: print(str(e)) pass def fav(tweet_sid): try: api.create_favorite(tweet_sid) except Exception as e: print(str(e)) pass track_words = ['NJCycling','NJ Cycling','NewJersey Cycling','NJ Bike','NJBike'] #Whatever you want to retweet should go here. follow_acc = ['19401785'] #retweets every tweet from this accounts, handles converted to ids print("Running...") try: twt = Stream(auths, listener()) twt.filter(track= track_words) # or follow = follow_acc except Exception as e: print(str(e)) pass
from tweepy.streaming import StreamListener, Stream from tweepy.auth import OAuthHandler from tweepy.api import API from datetime import timedelta def get_oauth(): consumer_key = '' consumer_secret = '' access_key = '' access_secret = '' auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) return auth class StreamListener(StreamListener): def on_status(self, status): status.created_at += timedelta(hours=9) for hashtag in status.entities['hashtags']: printer.write(u"{text}".format(text=status.text).encode('shift-jis', 'replace') + '\n') printer.write(u"{name}({screen})\n{time}\n".format( name=status.author.name, screen=status.author.screen_name, time=status.created_at).encode('shift-jis', 'replace')) printer.write('-' * 32 + '\n') if __name__ == '__main__': printer.write('\x1c\x26') # Enable Full-Widthe printer.write('\x1c\x43\x01') # Enable Shift JIS auth = get_oauth() stream = Stream(auth, StreamListener(), secure=True) stream.filter(track=['#kosenconf'])
#else: #setattr(status, k, v) #setattr(status, 'source_url', None) #elif k == 'retweeted_status': #setattr(status, k, Status.parse(api, v)) #else: #setattr(status, k, v) #return status if __name__ == "__main__": if not len(sys.argv) > 1: raise Exception('Need a twitter handle.') else: screen_name = sys.argv[1] twitter_user = TwitterUser.by_screen_name(screen_name, postgres_handle) if not twitter_user.credentials: raise Exception('%s does not have api credentials.' % screen_name) monitor_these_user_ids = twitter_user.following_following_ids[:4900] monitor_these_user_ids.append(twitter_user.id) print "Num of users to monitor: %s" % len(monitor_these_user_ids) listener = Listener(monitor_these_user_ids) stream = Stream(twitter_user.credentials.auth_handle, listener, secure=True) stream.filter(follow=monitor_these_user_ids)
output_path = arguments.output print('streaming {} Tweets on "{}"'.format( tweets_count, '", "'.join(subjects_to_stream))) # getting the credentials twitter_credentials = parsing_authentication(path=path_to_credentials) # creating an authentication handler authentication_handler = OAuthHandler( consumer_key=twitter_credentials['consumer_key'], consumer_secret=twitter_credentials['consumer_secret']) authentication_handler.set_access_token( key=twitter_credentials['access_token_key'], secret=twitter_credentials['access_token_secret']) # instantiating a listener listener = StoreListener(count=tweets_count) # creating a stream object stream = Stream(auth=authentication_handler, listener=listener) # stream Tweets stream.filter(track=subjects_to_stream) # storing tweets in a json format data_tweets = listener.tweets with open(output_path, 'w') as file: json.dump(data_tweets, file)
else: self.n = self.n + 1 fn.write(tweet) print("Polarity = " + str(var.sentiment.polarity)) print("subjectivity = " + str(var.subjectivity)) self.counter += 1 return True else: return False l1 = Listener(5) twitterStream = Stream(auth, Listener(5)) twitterStream.filter(track=["Donald Trump"], languages=["en"]) class Search(Listener): def __init__(self, num): self.num = num def search_tweets(self): search_results = tweepy.Cursor(a2.search, q="Donald Trump").items(self.num) for result in search_results: var = TextBlob(result.text) #if var.sentiment.polarity != 0 or var.subjectivity != 0:
src=src.encode('utf8') print created_at print id print name print text print s_name print src print "___" print #except: #pass def on_error(self, status): print status if __name__ == '__main__': auth = getOauth() stream = Stream(auth, AbstructListener(), secure=True) query1 = '横浜'.decode('utf8') #query2 = 'usogui'.decode('utf8') #query = query.encode('ascii') query = [] query.append(query1) #query.append(query2) stream.filter(track=query) #query.append(query2) #stream.filter(track=query)
conn = connect(opts.dsn) cursor = conn.cursor() # Don't know how this happens if status.geo == None: print 'No geo information' return if status.geo['type'] != 'Point': print 'Geo is of type ' + status.geo['type'] return # Insert the status into the DB. wkt = 'SRID=4326;POINT(%s %s)' % (status.geo['coordinates'][1], status.geo['coordinates'][0]) cursor.execute('INSERT INTO ' + opts.table + ' (screenname, tweet, the_geom) VALUES ' + '(%s, %s, ST_GeomFromEWKT(%s))', (status.user.screen_name, status.text, wkt )) conn.commit() listener = PGSQLListener() auth = BasicAuthHandler(args[0], args[1]) s = Stream(auth, listener, secure=True) #s.filter(track="foothill college") s.filter(locations=[float(i) for i in opts.geo.split(',')])
class TweepyLib: CONFIG_FILENAME = "twitterConfig.ini" # name of the config file defining e.g. the Twitter API key CONFIG_SECTION_KEYS = "keys" # name of the section of the config file holding the various Twitter API keys/tokens/etc. def __init__(self): config = RawConfigParser() configFilePath = os.path.join(os.path.dirname(__file__), self.__class__.CONFIG_FILENAME) logger.info("HELLO WORLD") logger.info(configFilePath) config.read(configFilePath) # Read API keys/tokens from config file self.consumer_key = config.get(self.__class__.CONFIG_SECTION_KEYS, "consumer_key") self.consumer_secret = config.get(self.__class__.CONFIG_SECTION_KEYS, "consumer_secret") self.access_token = config.get(self.__class__.CONFIG_SECTION_KEYS, "access_token") self.access_token_secret = config.get(self.__class__.CONFIG_SECTION_KEYS, "access_token_secret") self.auth = OAuthHandler(self.consumer_key, self.consumer_secret) self.isStreaming=False self.connect() self.tweetsQueue = Queue.Queue() self.filter=[] def connect(self): #todo: recheck needed auth self.auth.set_access_token(self.access_token, self.access_token_secret) logger.info("Connected with Twitter") def doStreaming(self): class StdOutListener(StreamListener): def __init__(self, twitterObject): super(StdOutListener, self).__init__() self.twitterObject = twitterObject def on_status(self, status): logger.info("adds new tweeter to queue with id %s: %s" % (status.id, status.text)) self.twitterObject.tweetsQueue.put(status) return True def on_error(self, status): logger.warning("Error %s while reading twitter" % status) listener = StdOutListener(self) self.stream = Stream(self.auth, listener) self.stream.filter(track=self.filter, async=True) def startStreamer(self): if(not self.filter): logger.warning("Trying to start stream, but the filter is empty") return self.doStreaming() logger.info("Streamer was started") self.isStreaming = True def stopStreamer(self): if(self.isStreaming): self.isStreaming = False self.stream.disconnect() logger.info("Streamer was stopped") def restartStreamer(self): self.stopStreamer() self.startStreamer() def addHashtagToFilter(self, newHashtag): if(newHashtag[:1] != '#'): newHashtag = '#' + newHashtag if(newHashtag in self.filter): return self.filter.append(newHashtag.encode()) logger.info("%s added to the filter" % newHashtag) self.restartStreamer() def removeHashtagFromFilter(self, existingHashtag): if(existingHashtag[:1] != '#'): existingHashtag = '#' + existingHashtag if(existingHashtag in self.filter): return self.filter.remove(existingHashtag) logger.info("%s removed from the filter" % existingHashtag) self.restartStreamer() def removeAllHashtagsFromFilter(self): changes = 0 for i in self.filter: # we only want to rmeove hashtags, not other filters if(i[:1] == '#'): changes += 1 self.filter.remove(i) logger.info("All hashtags removed from the filter") if(changes != 0): self.restartStreamer()
class TwitterCrawlerAgent(spade.Agent.Agent): def log(self, msg): print '[TWITTER_CRAWLER] ' + str(msg) def init_stream(self): self.q = Queue() self.keywords = [] self.listener = TwitterStreamListener(self.keywords, self.q) auth = OAuthHandler(config.con_secret, config.con_secret_key) auth.set_access_token(config.token, config.token_key) self.stream = Stream(auth, self.listener) def restart_stream(self): self.log("Restarting stream") self.stream.disconnect() self.stream.filter(track=self.keywords, async=True, languages=["en"]) def update_stream(self, keywords): self.log("Updating stream with keywords: ") self.log(keywords) if len(keywords) != 0: self.keywords = keywords self.listener.set_keywords(self.keywords) self.restart_stream() else: self.log("Empty keyword list!") def prepare_and_send_message(self): self.log("Preparing message") receiver = spade.AID.aid(name=config.sentimenter, addresses=["xmpp://" + config.sentimenter]) self.msg = spade.ACLMessage.ACLMessage() self.msg.setPerformative("inform") self.msg.setOntology(config.raw_tweet) self.msg.addReceiver(receiver) content = self.get_tweets_from_queue() str_content = "" for tweet in content: str_content += tweet.serialize() + "|" str_content = str_content[:-1] self.msg.setContent(str_content) self.send(self.msg) self.log("Message sent") def get_tweets_from_queue(self): self.log("Preparing tweet package content") tweets_list = [] while len(tweets_list) < config.batch_size and not self.q.empty(): item = self.q.get() if item is None: break tweets_list.append(item) return tweets_list def _setup(self): # self.setDebugToScreen() self.init_stream() self.addBehaviour(ListenForTweetsBehav(10), None) template = spade.Behaviour.ACLTemplate() template.setOntology(config.keyword_msg) t = spade.Behaviour.MessageTemplate(template) self.addBehaviour(UpdateKeywordsBehav(), t)
# Write to csv with open('%s_tweets.csv' % screen_name, 'w') as f: writer = csv.writer(f) writer.writerow(["id", "date", "time", "text", 'retweet_count', 'favorite_count','favorited','retweeted']) writer.writerows(outtweets) return if __name__ == '__main__': # Getting the data of berkeley users berkeley_users= ['ninaeyu', 'DylanFroscot', 'Mc11wain', 'chanel Shum', 'healyeatsreal', 'wheredeyatzo'] berkeley_shops = ['huckbikes', 'angelinasmn', 'GatherBerkeley'] for name in berkeley_users: get_all_tweets(name, auth) twitter_stream = Stream(auth, MyListener()) twitter_stream.filter(track=['#berkeley'])
if __name__ == "__main__": oauth = read_auth(sys.argv[1]) print(sys.argv[1]) sc = read_search_criteria(sys.argv[2]) print(sys.argv[2]) auth = OAuthHandler(oauth["C_KEY"], oauth["C_SECRET"]) auth.set_access_token(oauth["A_TOKEN"], oauth["A_SECRET"]) api = tweepy.API(auth) # ===== streaming ===== twitterStream = Stream(auth, TwitterStream(sys.argv[3])) while True: try: twitterStream.filter(track=sc["keywords"], languages=LANGUAGES_ACCETED) except Exception as exc: traceback.print_exc(file=sys.stdout) print("unknown exception, wait for 5 seconds to continue") logger.info("unknown exception, wait for 5 seconds to continue") time.sleep(5) continue # ===== index existing data ===== # index_data("/home/zqz/Work/chase/data/ml/public/w+ws/labeled_data.csv", # api, 1,2) # searcher = TwitterSearch(auth) # searcher.index(["#refugeesnotwelcome","#DeportallMuslims", "#banislam","#banmuslims", "#destroyislam", # "#norefugees","#nomuslims"])
except Exception as e: print(str(e)) pass def fav(tweet_sid): try: api.create_favorite(tweet_sid) except Exception as e: print(str(e)) pass def tweetPost(tweet_text): try: api.update_status(status=tweet_text) except Exception as e: print(str(e)) pass track_words = [" "," "," "] #retweet any tweet with these words follow_acc = ['20536157',"20573247"] #retweet every tweet from this accounts, handles converted to ids loc = [-74.255735,40.496044,-73.7002721,40.9152555] #reteet any tweet with goelocation that matches this box print("Running...") try: twt = Stream(auths, listener()) twt.filter(track= track_words) # OR (follow = follow_acc) OR (locations=loc) except Exception as e: print(str(e)) pass
print() self.counter += 1 if self.counter == self.max_count: return False return True def on_error(self, status): print(status) return False if __name__ == '__main__': parser = argparse.ArgumentParser(description='Streaming tweets into kafka') parser.add_argument('--verbose', type=bool, default=False) parser.add_argument('--count', type=int, default=10000) args = parser.parse_args() # connecting to the kafka server kafka_client = SimpleClient("localhost:9092") # creating a producer to this server kafka_producer = SimpleProducer(kafka_client) # creating a standard output listener of tweets listener = StdOutListener(producer=kafka_producer, count=args.count, verbose=args.verbose) # connecting to the twitter api auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # streaming tweets from Trump stream = Stream(auth, listener) stream.filter(track=["trump"]) kafka_producer.stop()