예제 #1
0
def get_tweets(search_string, result_type, time_range):

    api = twitter.Api(
        consumer_key='7SVoyHlwYgm90Y5HSzmTzUQ9O',
        consumer_secret='D0crcrKca9S3TXuqGRPYhzBN0LJut34MecER8Ly8fb3xrM0Gja',
        access_token_key='1199488399238926336-1jV9xq8bs4zdP5qiq96cUwP5GF1Fuz',
        access_token_secret='71hibaH8BPkPwCytm5CH9N4RJonaRCrSKUqG9y3dwo2Ix')

    tweets = {}
    search = "q=" + str(search_string) + "%20&result_type=" + str(
        result_type) + "&since=" + str(time_range) + "&count=100"
    print(search)
    results = api.GetSearch(raw_query=search)
    i = 0
    for result in results:
        json_result = json.loads(str(result))
        t = Tweet(json_result['user']['profile_image_url'],
                  json_result['user']['name'], json_result['text'],
                  json_result['created_at'], json_result['hashtags'])
        if 'retweet_count' in json_result:
            t.retweet_count = json_result['retweet_count']
        if 'favorite_count' in json_result:
            t.favorite_count = json_result['favorite_count']
        tweets[i] = t
        i += 1

    return tweets
예제 #2
0
    def add_tweet(self, tweet):
        """
        :param tweet:  tweepy object making up a tweet
        :return:
        """
        DLlogger.info('insert_tweet ')

        #create object
        tweet_obj = Tweet(id=tweet.id, created_at=tweet.created_at)

        if hasattr(tweet, "text"): tweet_obj.text = tweet.text
        if hasattr(tweet, "source"): tweet_obj.source = tweet.source
        if hasattr(tweet, "in_reply_to_status_id"):
            tweet_obj.in_reply_to_status_id = tweet.in_reply_to_status_id
            if not self.tweetExists(
                    tweet_obj.in_reply_to_status_id
            ) and tweet_obj.in_reply_to_status_id != None:
                self.add_job('tweet', tweet_obj.in_reply_to_status_id)

        if hasattr(tweet, "in_reply_to_user_id"):
            tweet_obj.in_reply_to_user_id = tweet.in_reply_to_user_id
            if not self.userExists(tweet_obj.in_reply_to_user_id
                                   ) and tweet_obj.in_reply_to_user_id != None:
                self.add_job('user', tweet_obj.in_reply_to_user_id)

        if hasattr(tweet, "in_reply_to_screen_name"):
            tweet_obj.in_reply_to_screen_name = tweet.in_reply_to_screen_name
        if hasattr(tweet, "author"):
            tweet_obj.user_id = tweet.author.id_str
            if not self.userExists(tweet_obj.user_id):
                self.add_user(tweet.author)

        if tweet.place:
            tweet_obj.place = 1
            if hasattr(tweet.place, "country"):
                tweet_obj.place_country = tweet.place.country
            if hasattr(tweet.place, "country_code"):
                tweet_obj.place_country_code = tweet.place.country_code
            if hasattr(tweet.place, "full_name"):
                tweet_obj.place_full_name = tweet.place.full_name
            if hasattr(tweet.place, "id"): tweet_obj.place_id = tweet.place.id
            if hasattr(tweet.place, "name"):
                tweet_obj.place_name = tweet.place.name
            if hasattr(tweet.place, "place_type"):
                tweet_obj.place_type = tweet.place.place_type

            if hasattr(tweet.place.bounding_box, "coordinates"):
                tweet_obj.place_coord0 = str(
                    tweet.place.bounding_box.coordinates[0][0])
                tweet_obj.place_coord1 = str(
                    tweet.place.bounding_box.coordinates[0][1])
                tweet_obj.place_coord2 = str(
                    tweet.place.bounding_box.coordinates[0][2])
                tweet_obj.place_coord3 = str(
                    tweet.place.bounding_box.coordinates[0][3])
        else:
            tweet_obj.place = 0

        if hasattr(tweet, "quoted_status_id_str"):
            tweet_obj.quoted_status_id = tweet.quoted_status_id_str
            if not self.tweetExists(tweet_obj.quoted_status_id
                                    ) and tweet_obj.quoted_status_id != None:
                self.add_job('tweet', tweet_obj.quoted_status_id)

        if hasattr(tweet, "quoted_status"):
            tweet_obj.quoted_status = tweet.quoted_status.text

        if hasattr(tweet, "is_quote_status"):
            tweet_obj.is_quote_status = tweet.is_quote_status

        if hasattr(tweet, "retweeted_status"):
            tweet_obj.retweeted_status = tweet.quoted_status.retweeted_status.id
            if not self.tweetExists(tweet_obj.retweeted_status
                                    ) and tweet_obj.retweeted_status != None:
                self.add_job('tweet', tweet_obj.retweeted_status)

        if hasattr(tweet, "quote_count"):
            tweet_obj.quote_count = tweet.quote_count
        if hasattr(tweet, "reply_count"):
            tweet_obj.reply_count = tweet.reply_count
        if hasattr(tweet, "retweet_count"):
            tweet_obj.retweet_count = tweet.retweet_count
        if hasattr(tweet, "favorite_count"):
            tweet_obj.favorite_count = tweet.favorite_count
        if tweet.coordinates: tweet_obj.coordinates = tweet.coordinates

        self.session.add(tweet_obj)

        DLlogger.info('Tweet ID  %i added', tweet.id)
        self.complete_job("tweet", tweet.id_str)
        self.session.commit()