Ejemplo n.º 1
0
    def get_tweets(self, q, lang, result_type, count):
        """
        Returns a collection of relevant Tweets matching a specified query.
        Parameters:
        q. Trend name.
        lang. Restricts tweets to the given language. See http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
        result_type. mixed: Include both popular and real time results in the response.
                     recent: return only the most recent results in the response.
                     popular: return only the most popular results in the response.
        count. The number of tweets to return per page, up to a maximum of 100. Defaults to 15
        """
        client_authenticated = TwAuthentication()

        parameters = {
            'q': self.get_trend_name_cleaned(),
            'lang': lang,
            'result_type': result_type,
            'count': count,
        }

        json_data = client_authenticated.get_client().request(BASE_URL + SEARCH_TWEETS_URL + '?' + urllib.urlencode(parameters))

        if json_data[0].status == 200:
            tweets_json = json.loads(json_data[1].replace('null', '"null"'))
            for tweet_item in tweets_json['statuses']:

                new_tweet = Tweet(
                    tweet_twitter_id = tweet_item['id_str'],
                    language = tweet_item['lang'],
                    retweets_count = tweet_item['retweet_count'],
                    from_username = u''+ str(tweet_item['user']['name'].encode('ascii', 'ignore')),
                    from_userid = tweet_item['user']['id_str'],
                    user_screen_name = u''+ str(tweet_item['user']['screen_name'].encode('ascii', 'ignore')),
                    user_profile_image = tweet_item['user']['profile_image_url'],
                    tweet = u''+ str(tweet_item['text'].encode('ascii', 'ignore')),
                    trend = self,
                )

                new_tweet.set_tweet_published_at(tweet_item['created_at'])
                new_tweet.save()

        else:
            raise TwitterRestAPIException(json_data[0].status)
Ejemplo n.º 2
0
    def get_trends(self, exclude_hashtags = False, get_tweets = True, result_type = 'mixed', count = '15'):
        """
        Return the top 10 trending topics from Twitter using Rest API for a specific WOEID.
        """
        client_authenticated = TwAuthentication()

        if exclude_hashtags:
            json_data = client_authenticated.get_client().request(BASE_URL + SEARCH_TRENDS_URL + str(self.woeid) +
                                                                  EXCLUDE_HASHTAGS)
        else:
            json_data = client_authenticated.get_client().request(BASE_URL + SEARCH_TRENDS_URL + str(self.woeid))

        if json_data[0].status == 200:
            tendencias_json = json.loads(json_data[1].replace('null', '"null"'))
            cont_importance = 1

            for trend_item in tendencias_json[0]['trends']:

                new_trend = Trend(
                    name = u''+trend_item['name'],
                    query = u''+trend_item['query'],
                    url = trend_item['url'],
                    importance = cont_importance,
                    place = self,
                )

                new_trend.set_promoted(trend_item['promoted_content'])
                new_trend.set_trend_created_at(tendencias_json[0]['created_at'])
                new_trend.save()

                cont_importance += 1

                #Update Tweets
                if get_tweets:
                    new_trend.get_tweets(new_trend.name, self.code, result_type, count)

        else:
            raise TwitterRestAPIException(json_data[0].status)