Exemplo n.º 1
0
def cmd(send, msg, args):
    """
    Search the Twitter API.
    Syntax: {command} <query> <--user username> <--count 1>
    """
    if not msg:
        send('What do you think I am, a bird?')
        return

    parser = arguments.ArgParser(args['config'])
    parser.add_argument('query', nargs='*')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('--user', dest='user', default=None)
    group.add_argument('--count', dest='count', type=int, default=1)
    group.add_argument('--random', action='store_true', default=False)

    try:
        cmdargs = parser.parse_args(msg)
    except arguments.ArgumentException as e:
        send(str(e))
        return

    api = get_search_api(args['config'])

    query = TwitterSearchOrder()
    keywords = [' '.join(cmdargs.query)]
    if cmdargs.user:
        keywords += [f'from:{cmdargs.user}']
    query.set_keywords(keywords)
    query.set_language('en')
    query.set_result_type('recent')
    query.set_include_entities(False)
    query.set_count(cmdargs.count)

    results = list(api.search_tweets_iterable(query))
    if not results:
        send('No tweets here!')
        return

    if cmdargs.random:
        shuffle(results)

    max_chan_tweets = 5
    max_pm_tweets = 25
    if cmdargs.count > max_pm_tweets:
        send(
            f"That's too many tweets! The maximum allowed through PM is {max_pm_tweets}"
        )
        return

    if cmdargs.count > max_chan_tweets:
        send(
            f"That's a lot of tweets! The maximum allowed in a channel is {max_chan_tweets}"
        )

    for i in range(0, min(cmdargs.count, max_pm_tweets)):
        if cmdargs.count <= max_chan_tweets:
            send(tweet_text(results[i]))
        else:
            send(tweet_text(results[i]), target=args['nick'])
Exemplo n.º 2
0
    def poll(self):
        tso = TwitterSearchOrder()
        tso.set_keywords(self._config['query'], True)

        language = self._config.get('language', None)
        if language:
            tso.set_language(language)

        tso.set_result_type('recent')
        tso.set_count(self._config.get('count', 30))
        tso.set_include_entities(False)

        last_id = self._get_last_id()

        if last_id:
            tso.set_since_id(int(last_id))

        try:
            tweets = self._client.search_tweets(tso)
            tweets = tweets['content']['statuses']
        except Exception as e:
            self._logger.exception('Polling Twitter failed: %s' % (str(e)))
            return

        tweets = list(reversed(tweets))

        if tweets:
            self._set_last_id(last_id=tweets[-1]['id'])

        for tweet in tweets:
            self._dispatch_trigger_for_tweet(tweet=tweet)
Exemplo n.º 3
0
    def poll(self):
        tso = TwitterSearchOrder()
        tso.set_keywords(self._config['query'])

        language = self._config.get('language', None)
        if language:
            tso.set_language(language)

        tso.set_result_type('recent')
        tso.set_count(self._config.get('count', 30))
        tso.set_include_entities(False)

        last_id = self._get_last_id()

        if last_id:
            tso.set_since_id(int(last_id))

        try:
            tweets = self._client.search_tweets(tso)
            tweets = tweets['content']['statuses']
        except Exception as e:
            self._logger.exception('Polling Twitter failed: %s' % (str(e)))
            return

        tweets = list(reversed(tweets))

        if tweets:
            self._set_last_id(last_id=tweets[-1]['id'])

        for tweet in tweets:
            self._dispatch_trigger_for_tweet(tweet=tweet)
    def poll(self):
        tso = TwitterSearchOrder()
        tso.set_keywords(self._config["query"])

        language = self._config.get("language", None)
        if language:
            tso.set_language(language)

        tso.set_result_type("recent")
        tso.set_count(self._config.get("count", 30))
        tso.set_include_entities(False)

        last_id = self._get_last_id()

        if last_id:
            tso.set_since_id(int(last_id))

        try:
            tweets = self._client.search_tweets(tso)
            tweets = tweets["content"]["statuses"]
        except Exception as e:
            self._logger.exception("Polling Twitter failed: %s" % (str(e)))
            return

        tweets = list(reversed(tweets))

        if tweets:
            self._set_last_id(last_id=tweets[-1]["id"])

        for tweet in tweets:
            self._dispatch_trigger_for_tweet(tweet=tweet)
Exemplo n.º 5
0
def cmd(send, msg, args):
    """
    Search the Twitter API.
    Syntax: {command} <query> <--user username> <--count 1>
    """
    if not msg:
        send('What do you think I am, a bird?')
        return

    parser = arguments.ArgParser(args['config'])
    parser.add_argument('query', nargs='*')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('--user', dest='user', default=None)
    group.add_argument('--count', dest='count', type=int, default=1)
    group.add_argument('--random', action='store_true', default=False)

    try:
        cmdargs = parser.parse_args(msg)
    except arguments.ArgumentException as e:
        send(str(e))
        return

    api = get_search_api(args['config'])

    query = TwitterSearchOrder()
    keywords = [' '.join(cmdargs.query)]
    if cmdargs.user:
        keywords += ['from:{}'.format(cmdargs.user)]
    query.set_keywords(keywords)
    query.set_language('en')
    query.set_result_type('recent')
    query.set_include_entities(False)
    query.set_count(cmdargs.count)

    results = list(api.search_tweets_iterable(query))
    if not results:
        send('No tweets here!')
        return

    if cmdargs.random:
        shuffle(results)

    max_chan_tweets = 5
    max_pm_tweets = 25
    if cmdargs.count > max_pm_tweets:
        send("That's too many tweets! The maximum allowed through PM is {}".format(max_pm_tweets))
        return

    if cmdargs.count > max_chan_tweets:
        send("That's a lot of tweets! The maximum allowed in a channel is {}".format(max_chan_tweets))

    for i in range(0, min(cmdargs.count, max_pm_tweets)):
        if cmdargs.count <= max_chan_tweets:
            send(tweet_text(results[i]))
        else:
            send(tweet_text(results[i]), target=args['nick'])
Exemplo n.º 6
0
class TwitterService(object):
    def __init__(self):
        self.properties = TwitterSearchOrder()
        self.properties.set_keywords([''])
        self.properties.set_count(100)
        self.properties.set_include_entities(False)
        self.api = TwitterSearch(consumer_key=DATASOURCES['twitter']['api_key'],
                                 consumer_secret=DATASOURCES['twitter']['api_secret'],
                                 access_token=DATASOURCES['twitter']['access_token'],
                                 access_token_secret=DATASOURCES['twitter']['access_token_secret'])

    def get_tweets(self, lat, lng):
        self.properties.set_geocode(float(lat), float(lng), 1)
        response = self.api.search_tweets(self.properties)
        return response
Exemplo n.º 7
0
class TwitterService(object):
    def __init__(self):
        self.properties = TwitterSearchOrder()
        self.properties.set_keywords([''])
        self.properties.set_count(100)
        self.properties.set_include_entities(False)
        self.api = TwitterSearch(
            consumer_key=DATASOURCES['twitter']['api_key'],
            consumer_secret=DATASOURCES['twitter']['api_secret'],
            access_token=DATASOURCES['twitter']['access_token'],
            access_token_secret=DATASOURCES['twitter']['access_token_secret'])

    def get_tweets(self, lat, lng):
        self.properties.set_geocode(float(lat), float(lng), 1)
        response = self.api.search_tweets(self.properties)
        return response
    def getStats(self, url, proxy, headers, timeout):
        """returns (retweet + favorite count) count from twitter API , url is url that could be in a tweet, proxy is 'ip:port' in string, headers should contain user-agent in as an item in dictionary, timeout is maximum time while waiting for response and is an int"""
        count = 0

        tso = TwitterSearchOrder()
        tso.set_search_url('q=' + url)
        tso.set_result_type(result_type='mixed')
        tso.set_include_entities(False)
        tso.set_count(100)

        ts = TwitterSearch(consumer_key=self.ConsumerKey,
                           consumer_secret=self.ConsumerSecret,
                           access_token=self.AccessTokenKey,
                           access_token_secret=self.AccessTokenSecret,
                           proxy=proxy)

        for tweet in ts.search_tweets_iterable(tso):
            count += tweet['retweet_count'] + tweet['favorite_count']

        return count
Exemplo n.º 9
0
    def search(self):
        try:
            tso = TwitterSearchOrder()
            tso.set_keywords(*self.search_terms)
            tso.set_include_entities(False)
            tso.set_count(100)

            ts = TwitterSearch(
                consumer_key='aOUVcCWLIYEbUvHW5dLjVc7Gf',
                consumer_secret='8qb3LTAHbj43J40Rxm0RMLAOaP4QoEHfFVGTeJ3S6iUmSBq6JJ',
                access_token='4251433696-ulZx8dJ3QZE95ds0PhXNldeKFhjhBUoGSuGycSE',
                access_token_secret='wx65NQaBHHgwC4xLOgRxFSs4kWWzkg09KkgNkAKHZryks'
            )

            for tweet in ts.search_tweets_iterable(tso):
                self.data.append(tweet['text'])
                self.save_line(tweet['text'])

            # self.save_data(self.data)
        except TwitterSearchException as exception:
            print(exception)