~parinporecha/gtg/global_shortcut

« back to all changes in this revision

Viewing changes to GTG/backends/tweepy/parsers.py

  • Committer: Izidor Matušov
  • Date: 2013-01-10 15:03:42 UTC
  • Revision ID: izidor.matusov@gmail.com-20130110150342-ajwnwmc2trh9ia2v
Removing broken twitter and tweepy services

I don't know anybody uses them. They are broken, and pretty artificial (more proof of the concept). We should focus our efforts on normal synchronization.

Show diffs side-by-side

added added

removed removed

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