0

I used the tweepy library (for twitter api-v1.1) to get some metadata (e.g., tweet text, #retweets, userid, etc.) for a list of tweet ids. Here is my code:

consumer_key = 'xxxxxxxxxxxx'
consumer_key_secret = 'xxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxxxx'

auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

def createTrainingSet(corpusFile, tweetContent):
    import csv
    import time
    import json

    counter = 0
    corpus = []

    with open(corpusFile, 'r') as csvfile:
        lineReader = csv.reader(csvfile, delimiter=',')
        for row in lineReader:
            corpus.append({"tweet_id": row[0], "unreliable": row[1], "conspiracy": row[2],\
                           "clickbait": row[3], "political/biased": row[4], "date": row[5]})

    sleepTime = 2
    trainingDataSet = []  
    
    for tweet in corpus:
        try:
            tweetFetched = api.get_status(tweet["tweet_id"])
            print("Tweet fetched" + tweetFetched.text)
            print("followers_count: "+ str(tweetFetched.user.followers_count))
            print("friends_count: " + str(tweetFetched.user.friends_count))
            
            tweet["text"] = tweetFetched.text
            tweet["retweet_count"] = tweetFetched.retweet_count
            tweet["favorite_count"] = tweetFetched.favorite_count
            tweet["created_at"] = tweetFetched.created_at   
            tweet["user_id"] = tweetFetched.user.id_str
            tweet["user_created_at"] = tweetFetched.user.created_at              
            
            trainingDataSet.append(tweet)
            time.sleep(sleepTime)

        except:
            print("Inside the exception - no:2")
            continue

# This is corpus dataset
corpusFile = "sample.csv"
# This is my target file
tweetContent = "tweetContent.csv"
# Call the method
resultFile = createTrainingSet(corpusFile, tweetContent)

I don't know why this code doesn't work any more (the last time it worked was a bout a couple of months ago). However, when I run it now, it returns "Inside the exception - no:2". Why is that?

halfer
  • 19,824
  • 17
  • 99
  • 186
mOna
  • 2,341
  • 9
  • 36
  • 60

1 Answers1

0

Here is the two lines of code that helped me find the erros:

except tweepy.TweepError as e:
        print ('the error code:', e.args[0][0]['code'])
        print ('the error message:', e.args[0][0]['message'])  

Also, thanks to Jeyekomon's answer in this post, I found that the e.message[0]['code'] is not working anymore:

The error code used to be accessed using e.message[0]['code'] which no longer works. The message attribute has been deprecated in Python 2.6 and removed in Python 3.0. Currently you get an error 'TweepError' object has no attribute 'message'

In addition, it seems there are some other helpful attributes (api_code, reason and response) in TweepError exception class that are not in the documentation.

mOna
  • 2,341
  • 9
  • 36
  • 60