Search code examples
python-3.xtwittermetadatatweepy

crawl only tweets metadata without the tweet text using an ID list


CONTEXT: I have a list of tweet ids and their textual content and I need to crawl their metadata. However, my code crawls the tweet metadata and text as well. Since I have about 100K tweet ids I do not wish to waste time crawling the tweet text again.

Question: How can I adapt the following code so I would be able to download only tweet metadata. I'm using tweepy and python 3.6.

def get_tweets_single(twapi, idfilepath):
    #tweet_id = '522778758168580098'
    tw_list = []
    with open(idfilepath,'r') as f1:#A File that Contains tweet IDS
        lines = f1.readlines()
        for line in lines:
            try:
                print(line.rstrip('\n'))
                tweet = twapi.get_status(line.rstrip('\n'))#tweepy function to crawl tweet metadata
                tw_list.append(tweet)
                #tweet = twapi.statuses_lookup(id_=tweet_id,include_entities=True, trim_user=True)
                with open(idjsonFile,'a',encoding='utf-8')as f2:
                    json.dump(tweet._json,f2)
            except tweepy.TweepError as te:
                print('Failed to get tweet ID %s: %s', tweet_id, te.message)

def main(args):
    print('hello')
# connect to twitter
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
    api = tweepy.API(auth)
    get_tweets_single(api, idfilepath)

Solution

  • You cannot only download metadata about the tweet.

    Looking at the documentation you can choose to exclude information about the user with trim_user=true - but that's the only thing you can strip out.