Skip to content

Instantly share code, notes, and snippets.

@inactivist
Last active December 26, 2020 12:11
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inactivist/5263501 to your computer and use it in GitHub Desktop.
Save inactivist/5263501 to your computer and use it in GitHub Desktop.
How to get the raw Twitter API JSON response from a Tweepy request. It provides a simple way to stash the parsed API request results for later use. It stores the parsed JSON in a _payload field in the returned API results, which can then be iterated over. Not as clean as I'd like, but it allows me to leverage all of Tweepy's API methods and stil…
"""
Taken from my suggestion at:
https://groups.google.com/forum/#!msg/tweepy/OSGRxOmkzL4/yaNT9fL9FAIJ
Note that this is an incomplete snippet; you'll need to create the API auth object.
"""
import simplejson as json
import tweepy
class MyModelParser(tweepy.parsers.ModelParser):
def parse(self, method, payload):
result = super(MyModelParser, self).parse(method, payload)
result._payload = json.loads(payload)
return result
api = tweepy.API(auth, parser=MyModelParser())
results = api.user_timeline(screen_name='twitter')
for s in results._payload:
print json.dumps(s)
@dpenfoldbrown
Copy link

Though it's late, here's a note:

I've done this (getting the raw json in each individual Model object) the dirty way by modifying the Model classes that I want to save raw data for.

For example, in model.Status.parse(...) method, addind a 'setattr(status, '_json', json)' line directly below the 'status = cls(api)' line.

Unfortunately, this is per-class, and also unfortunately, it is a modification as opposed to an extension. The modular and clean option is annoyingly long: subclassing API, subclassing ModelFactory, etc.

@Gijs-Koot
Copy link

A slight problem is that in some cases, notably something like

lg_friends = api.friends_ids()

the result is a list, which doesn't like fields being attached to it. Thanks though, this is helpful!

@royyeah
Copy link

royyeah commented May 3, 2014

Gijs is right, this doesn't work for lists. I used

class ModelParser(tweepy.parsers.ModelParser):
    def parse(self, method, payload):
        result = super(ModelParser, self).parse(method, payload)
        try:
            iter(result)
        except TypeError:
            result._payload = json.loads(payload)
        else:
            for item in result:
                item._payload = json.loads(payload)
        return result

to overcome this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment