Esempio n. 1
0
 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)
Esempio n. 2
0
    def __init__(self,
                 stream_ref,
                 auth_params,
                 stream_manager,
                 db=None,
                 bot_user_agent=None):
        """
        :param username: twitter username
        :param auth: tuple of auth credentials representing twitter user
        :param mstream: multi stream container owning this stream
            `mstream` should implement listener protocol to handle user events
        """
        # self.me = self.username = stream_id
        self.stream_ref = stream_ref
        self.stream_id = stream_ref.id
        self.auth_params = auth_params

        self.auth = TwitterApiWrapper.auth_from_settings(*auth_params)
        self.api = TwitterApiWrapper.make_api(self.auth)

        self.listener = self.ListenerCls()(self, stream_manager, db=db)
        headers = {'X-User-Agent': bot_user_agent} if bot_user_agent else {}
        self.stream = Stream(self.auth, self.listener, headers=headers)
        self.listener.log_event('initialized')

        if on_test():
            self.test_stream = TestStream(self.listener)
Esempio n. 3
0
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)
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
Esempio n. 5
0
    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'])
Esempio n. 6
0
    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'])
Esempio n. 7
0
def main():
    inifile = configparser.ConfigParser()
    inifile.read('./user.conf')
    init = {
        'consumer_key': inifile.get('twitter', 'consumer_key'),
        'consumer_secret': inifile.get('twitter', 'consumer_secret'),
        'access_key': inifile.get('twitter', 'access_key'),
        'access_secret': inifile.get('twitter', 'access_secret'),
    }

    github_user = {
        'repo_owner': inifile.get('github', 'repo_owner'),
        'repo_name': inifile.get('github', 'repo_name'),
        'username': inifile.get('github', 'username'),
        'password': inifile.get('github', 'password'),
    }

    auth = get_oauth(init)
    stream = Stream(auth,
                    AbstractedlyListener(github_user=github_user),
                    secure=True)
    error = 0
    while True:
        try:
            stream.userstream()
        except:
            error += 1
            print(error, traceback.format_exc())
Esempio n. 8
0
    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'])
Esempio n. 9
0
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()
Esempio n. 10
0
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 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 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])
Esempio n. 13
0
 def test_exp_backoff_cap(self):
     self.stream = Stream(self.auth,
                          self.listener,
                          timeout=3.0,
                          retry_count=1,
                          retry_time=1.0,
                          retry_time_cap=3.0)
     self.stream.sample()
     # 1 retry, but 4x the retry_time exceeds the cap, so should be capped
     self.assertEqual(self.stream.retry_time, 3.0)
Esempio n. 14
0
 def test_exp_backoff(self):
     self.stream = Stream(self.auth,
                          self.listener,
                          timeout=3.0,
                          retry_count=1,
                          retry_time=1.0,
                          retry_time_cap=100.0)
     self.stream.sample()
     # 1 retry, should be 4x the retry_time
     self.assertEqual(self.stream.retry_time, 4.0)
Esempio n. 15
0
 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))
Esempio n. 16
0
 def test_420(self):
     self.stream = Stream(self.auth,
                          self.listener,
                          timeout=3.0,
                          retry_count=0,
                          retry_time=1.0,
                          retry_420=1.5,
                          retry_time_cap=20.0)
     self.stream.sample()
     # no retries, but error 420, should be double the retry_420, not double the retry_time
     self.assertEqual(self.stream.retry_time, 3.0)
 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")
Esempio n. 18
0
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)
Esempio n. 19
0
    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 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'
                      ])
Esempio n. 21
0
    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"])
Esempio n. 22
0
    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)
Esempio n. 23
0
 def setUp(self):
     #bad auth causes twitter to return 401 errors
     self.auth = OAuthHandler("bad-key", "bad-secret")
     self.auth.set_access_token("bad-token", "bad-token-secret")
     self.listener = MockStreamListener(self)
     self.stream = Stream(self.auth, self.listener)
            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)
Esempio n. 25
0
    def __init__(self, kw_list):

        self.kws = kw_list
        self.api = get_api()
        self.stream = Stream(auth=self.api.auth, listener=MyStreamListener())
Esempio n. 26
0
    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)
Esempio n. 27
0
                filename = '/home/pi/sif/cap/manage_sif/icon' + str(
                    num) + '.gif'
                command = "wget '" + str(member_url) + "' -O " + filename
                check = commands.getoutput(command)


#				print check

            if dobu == 1:
                tweet = tweet + 'ド(・8・)ブ'
            tweet = tweet + "#スクフェス"
            print "tweet:" + tweet

            icons = []
            for i in range(0, 11):
                icons.insert(
                    i, "/home/pi/sif/cap/manage_sif/icon" + str(i) + ".gif")

            self.useCV.create_gacha_image(icons)

            self.useTwitter.post_tweet_image(
                status.id, tweet, '/home/pi/sif/cap/manage_sif/result.jpg')

if __name__ == '__main__':
    auth = get_oauth()
    stream = Stream(auth, AbstractedlyListener(), secure=True)
    #	stream.timeout = None
    stream.userstream()

    print "gatcha.py end\n"
Esempio n. 28
0
def test_stream():
    listener = StdOutListener(
        model=unweighted_model())  # using unweighted model for faster tests
    stream = Stream(listener.auth, listener)
    assert isinstance(stream, Stream)
Esempio n. 29
0
 def setUp(self):
     self.auth = create_auth()
     self.listener = MockStreamListener(self)
     self.stream = Stream(self.auth, self.listener, timeout=3.0)
        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(',')])