Skip to content

Instantly share code, notes, and snippets.

@inactivist
Last active December 26, 2020 12:11
Show Gist options
  • 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)
@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