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
33
32
log = logging.getLogger(__name__)
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'):
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')
151
151
return self._get_n_rows()
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()
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)
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)
171
172
def send_thread(self, obj_id, message):
172
"""Write a comment on some existing picture.
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."""
177
'media/{}/comments'.format(obj_id),
178
stream='reply_to/{}'.format(obj_id))
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)
181
if not Downloader(url, method=method,
182
params=dict(access_token=token)).get_json():
183
raise FriendsError('Failed to {} like {} on Instagram'.format(
188
params=dict(access_token=token)).get_json():
190
'Failed to {} like {} on Instagram'.format(
187
194
def like(self, obj_id):