parsers.py 3.0 KB

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