Yesterday I shared some simple Python code to dump a Twitter users information. See post here.
I managed to recreate this in Tweepy using jsonpickle (see code snippet below).
For the interested, I still didn’t manage to figure out the Tweepy Python object navigation. There’s a <cough> remarkably </cough> similar problem posted on stackoverflow here. Thanks to MarcW for summarizing what I’m attempting to do as: “so basically, I want to loop through user.__getstate__() and if I find an object which requires further iteration, loop through that too”
Ultimately I’ll want a nice JSON object to pipe into CouchDB, so I found jsonpickle. Some short playing around and I have a solution (I’m still curious about the object iteration/navigation FWIW as I’m sure it’s something stupid I’m missing).
Here’s the code, it converts the Python object to JSON, so no need to patch Tweepy.
# -*- coding: utf-8 -*-
import sys
import tweepy
import json
import jsonpickle
from pprint import pprint
api = tweepy.API()
def main():
print "Starting."
user = api.get_user('TheSuggmeister',include_entities=1)
print "================ type ================="
print type(user)
print "================ dir ================="
print dir(user)
print "================ user.status ================="
pickled = jsonpickle.encode(user)
print(json.dumps(json.loads(pickled), indent=4, sort_keys=True)) #you could just print pickled, but this makes it pretty
print "================= end ================="
if __name__ == "__main__":
main()