0

Hi I am modifying some code in Python (2.7) I wrote to work with Twitter's updated API and wondered if anyone could help this fairly simple issue...

I am reading the account's latest mention as a variable 'mention' and a string as such (see code below for how I get it from the api):

Status(contributors=None, truncated=False, text=u'Here we have a tweet', is_quote_status=False, retweeted=True, u'created_at': u'Sun Dec 25 22:26:12 +0000 2011')

(there's obviously a lot more in each mention returned, many many lines, but I've stripped it down to hopefully all that is necessary)

I want to bring this into a function and load it with json.loads in order to use it (and this is where the problem is)... my code as follows:

import ConfigParser
import json
import re
import csv
from tweepy import OAuthHandler
from tweepy import API
from datetime import datetime, time, timedelta
import traceback


consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
account_screen_name = ''
account_user_id = ''

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

mentions = api.mentions_timeline(count=1)
now = datetime.now()

def myfunction(mention):
        tweet = json.loads(mention.strip())    
        retweeted = tweet.get('retweeted')
        from_self = tweet.get('user',{}).get('id_str','') == account_user_id

        if retweeted is not None and not retweeted and not from_self:
            try:
                DO SOME THINGS

        else:
            DON'T DO THINGS

for mention in mentions:
    print mention.text
    if now < (mention.created_at + timedelta(hours=1) + timedelta(seconds=10)):
        print "Mention was in the last ten seconds"
        myfunction(mention)
    else:
        print "Wasn't in last ten seconds, so do nothing."

However, if I do this then I get the error:

Traceback (most recent call last):
  File "stuff.py", line 100, in <module>
    myfunction(mention)
  File "stuff.py", line 40, in replier
    tweet = json.loads(mention.strip())
AttributeError: 'Status' object has no attribute 'strip'

I am not the best when it comes to json so this is probably an obvious problem but can anyone solve it?

I don't want to change any of the other code for now because there's a lot and it will take too long. I know that's not great practice but this is a home project and I just want the line to work, i.e. all changes to happen in:

tweet = json.loads(mention.strip())

I suspect it's because I am trying to load the first of many mentions into the mention string... and that's not reading right for json.loads() ?

the_t_test_1
  • 1,193
  • 1
  • 12
  • 28
  • I'm guessing this is an automated message because there is obviously not that many lines in the code and the code clearly is Minimal, Complete and Verifiable, I just threw numbers in there when pasting it out to make clear it was a dummy. – the_t_test_1 Oct 24 '18 at 18:26
  • 1
    Minimal, Complete and Verifiable means that we should be able to copy/paste the code (and resources if needed) and reproduce the issue. Import statements, create the `mention` object, and then call `json.loads`. It really helps. – sal Oct 24 '18 at 19:22
  • can you give an example of how mention looks like? – Sujil Maharjan Oct 24 '18 at 19:38
  • Thanks sal and Sujil, I have added context... hopefully enough! I am fairly sure this problem is just because I am trying to load the first mention of many that come from Twitter API, and this isn't working with json.loads() ... so hopefully this is a really simple fix? – the_t_test_1 Oct 25 '18 at 09:42
  • `mention` is of class `Status`. Unfortunately I can't find documentation about the `Status` class on `tweety`, which is obviously the main problem here. I would do a `dir(mention)` and see what methods and attributes are exposed to get an idea. – sal Oct 25 '18 at 19:03

1 Answers1

2

The variable mention that you're passing to myfunction() isn't a String. You're passing a Status object which basically has all the elements of the tweet, and looks like this https://gist.github.com/dev-techmoe/ef676cdd03ac47ac503e856282077bf2 This is why the strip() method fails as it's only for Strings.

However, a Status object in Tweepy does have a property that will get you a JSON serializable item that you can then use.

#This is going by the code I see, and on the assumption 'mention' is one tweet
tweet_str = json.dumps(mention._json)

#tweet_str is now a JSON string, so you can try replacing your problematic line with:
tweet = json.loads(tweet_str)

I think it's worth noting that you do not necessarily have to convert to JSON. You can directly access the parameters inside the Status object, as it's sort of similar to JSON already, but this depends on use case. And as I believe you said you've written much of the code elsewhere, you likely have a reliance on JSON later.

This thread on JSON from Status objects is pretty useful, so check it out if you're still stuck Convert Tweepy Status object into JSON

Hope this helps!

Generic Snake
  • 575
  • 1
  • 5
  • 13