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() ?