~super-friends/friends/13.10

« back to all changes in this revision

Viewing changes to friends/protocols/instagram.py

  • Committer: Tarmac
  • Author(s): Robert Bruce Park
  • Date: 2013-07-08 21:20:40 UTC
  • mfrom: (220.1.1 fix-test-tmpdir)
  • Revision ID: tarmac-20130708212040-3h8y3ffez4bfb5gq
Fix Instagram's avatar logic.

Approved by Ken VanDine, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    'Instagram',
21
21
    ]
22
22
 
23
 
import time
 
23
 
24
24
import logging
25
25
 
26
 
from friends.utils.avatar import Avatar
27
26
from friends.utils.base import Base, feature
28
 
from friends.utils.cache import JsonCache
29
 
from friends.utils.http import Downloader, Uploader
 
27
from friends.utils.http import Downloader
30
28
from friends.utils.time import parsetime, iso8601utc
31
29
from friends.errors import FriendsError
32
30
 
 
31
 
33
32
log = logging.getLogger(__name__)
34
33
 
 
34
 
35
35
class Instagram(Base):
36
36
    _api_base = 'https://api.instagram.com/v1/{endpoint}?access_token={token}'
37
37
    def _whoami(self, authdata):
56
56
        name = person.get('full_name')
57
57
        person_id = person.get('id')
58
58
        message= '%s shared a picture on Instagram.' % nick
59
 
        person_icon = Avatar.get_image(person.get('profile_picture'))
 
59
        person_icon = person.get('profile_picture')
60
60
        person_url = 'http://instagram.com/' + nick
61
61
        picture = entry.get('images').get('thumbnail').get('url')
62
62
        if entry.get('caption'):
107
107
        return args['url']
108
108
 
109
109
    def _publish_comment(self, comment, stream):
 
110
        """Publish a single comment into the Dee.SharedModel."""
110
111
        message_id = comment.get('id')
111
112
        if message_id is None:
112
113
            return
116
117
        timestamp = comment.get('created_time')
117
118
        if timestamp is not None:
118
119
            timestamp = iso8601utc(parsetime(timestamp))
119
 
        icon_uri = Avatar.get_image(person.get('profile_picture'))
 
120
        icon_uri = person.get('profile_picture')
120
121
        sender_id = person.get('id')
121
122
        sender = person.get('full_name')
122
123
 
130
131
             sender_id=sender_id,
131
132
             sender=sender,
132
133
             )
133
 
#                print(args)
134
134
        self._publish(**args)
135
135
 
136
136
    @feature
143
143
        values = result.get('data', {})
144
144
        for update in values:
145
145
            self._publish_entry(update)
146
 
    
 
146
 
147
147
    @feature
148
148
    def receive(self):
149
149
        """Gather and publish all incoming messages."""
151
151
        return self._get_n_rows()
152
152
 
153
153
    def _send(self, obj_id, message, endpoint, stream='messages'):
 
154
        """Used for posting a message or comment."""
154
155
        token = self._get_access_token()
155
156
 
156
157
        url = self._api_base.format(endpoint=endpoint, token=token)
161
162
            params=dict(access_token=token, text=message)).get_json()
162
163
        new_id = result.get('id')
163
164
        if new_id is None:
164
 
            raise FriendsError('Failed sending to Instagram: {!r}'.format(result))
165
 
        enpoint = 'media/{}/comments'.format(new_id)
 
165
            raise FriendsError(
 
166
                'Failed sending to Instagram: {!r}'.format(result))
166
167
        url = self._api_base.format(endpoint=endpoint, token=token)
167
168
        comment = Downloader(url, params=dict(access_token=token)).get_json()
168
169
        return self._publish_entry(entry=comment, stream=stream)
169
170
 
170
171
    @feature
171
172
    def send_thread(self, obj_id, message):
172
 
        """Write a comment on some existing picture.
173
 
        """
174
 
        return self._send(obj_id, message, 'media/{}/comments'.format(obj_id),
175
 
                          stream='reply_to/{}'.format(obj_id))
 
173
        """Write a comment on some existing picture."""
 
174
        return self._send(
 
175
            obj_id,
 
176
            message,
 
177
            'media/{}/comments'.format(obj_id),
 
178
            stream='reply_to/{}'.format(obj_id))
176
179
 
177
180
    def _like(self, obj_id, endpoint, method):
 
181
        """Used for liking or unliking an object."""
178
182
        token = self._get_access_token()
179
183
        url = self._api_base.format(endpoint=endpoint, token=token)
180
184
 
181
 
        if not Downloader(url, method=method,
182
 
                          params=dict(access_token=token)).get_json():
183
 
            raise FriendsError('Failed to {} like {} on Instagram'.format(
184
 
                method, obj_id))
 
185
        if not Downloader(
 
186
                url,
 
187
                method=method,
 
188
                params=dict(access_token=token)).get_json():
 
189
            raise FriendsError(
 
190
                'Failed to {} like {} on Instagram'.format(
 
191
                    method, obj_id))
185
192
 
186
193
    @feature
187
194
    def like(self, obj_id):