Updated packages and code to python3. Won't work with python 2
[twitter-api-cdsw] / tweepy / parsers.py
1 # Tweepy
2 # Copyright 2009-2010 Joshua Roesslein
3 # See LICENSE for details.
4
5 from __future__ import print_function
6
7 from tweepy.models import ModelFactory
8 from tweepy.utils import import_simplejson
9 from tweepy.error import TweepError
10
11
12 class Parser(object):
13
14     def parse(self, method, payload):
15         """
16         Parse the response payload and return the result.
17         Returns a tuple that contains the result data and the cursors
18         (or None if not present).
19         """
20         raise NotImplementedError
21
22     def parse_error(self, payload):
23         """
24         Parse the error message from payload.
25         If unable to parse the message, throw an exception
26         and default error message will be used.
27         """
28         raise NotImplementedError
29
30
31 class RawParser(Parser):
32
33     def __init__(self):
34         pass
35
36     def parse(self, method, payload):
37         return payload
38
39     def parse_error(self, payload):
40         return payload
41
42
43 class JSONParser(Parser):
44
45     payload_format = 'json'
46
47     def __init__(self):
48         self.json_lib = import_simplejson()
49
50     def parse(self, method, payload):
51         try:
52             json = self.json_lib.loads(payload)
53         except Exception as e:
54             raise TweepError('Failed to parse JSON payload: %s' % e)
55
56         needs_cursors = 'cursor' in method.session.params
57         if needs_cursors and isinstance(json, dict):
58             if 'previous_cursor' in json:
59                 if 'next_cursor' in json:
60                     cursors = json['previous_cursor'], json['next_cursor']
61                     return json, cursors
62         else:
63             return json
64
65     def parse_error(self, payload):
66         error = self.json_lib.loads(payload)
67         if error.has_key('error'):
68             return error['error']
69         else:
70             return error['errors']
71
72
73 class ModelParser(JSONParser):
74
75     def __init__(self, model_factory=None):
76         JSONParser.__init__(self)
77         self.model_factory = model_factory or ModelFactory
78
79     def parse(self, method, payload):
80         try:
81             if method.payload_type is None:
82                 return
83             model = getattr(self.model_factory, method.payload_type)
84         except AttributeError:
85             raise TweepError('No model for this payload type: '
86                              '%s' % method.payload_type)
87
88         json = JSONParser.parse(self, method, payload)
89         if isinstance(json, tuple):
90             json, cursors = json
91         else:
92             cursors = None
93
94         if method.payload_list:
95             result = model.parse_list(method.api, json)
96         else:
97             result = model.parse(method.api, json)
98
99         if cursors:
100             return result, cursors
101         else:
102             return result

Benjamin Mako Hill || Want to submit a patch?