Skip to content

Commit 25b4b41

Browse files
author
kbehrman
committed
Merge pull request #16 from shotgunsoftware/share_thumbnails
Share thumbnails
2 parents 1aa9a48 + 0daa358 commit 25b4b41

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

shotgun_api3/shotgun.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,89 @@ def set_session_uuid(self, session_uuid):
931931

932932
self.config.session_uuid = session_uuid
933933
return
934+
935+
def share_thumbnail(self, entities, thumbnail_path=None, source_entity=None,
936+
filmstrip_thumbnail=False, **kwargs):
937+
if not self.server_caps.version or self.server_caps.version < (4, 0, 0):
938+
raise ShotgunError("Thumbnail sharing support requires server "\
939+
"version 4.0 or higher, server is %s" % (self.server_caps.version,))
940+
941+
if not isinstance(entities, list) or len(entities) == 0:
942+
raise ShotgunError("'entities' parameter must be a list of entity "\
943+
"hashes and may not be empty")
944+
945+
for e in entities:
946+
if not isinstance(e, dict) or 'id' not in e or 'type' not in e:
947+
raise ShotgunError("'entities' parameter must be a list of "\
948+
"entity hashes with at least 'type' and 'id' keys.\nInvalid "\
949+
"entity: %s" % e)
950+
951+
if (not thumbnail_path and not source_entity) or \
952+
(thumbnail_path and source_entity):
953+
raise ShotgunError("You must supply either thumbnail_path OR "\
954+
"source_entity.")
955+
956+
# upload thumbnail
957+
if thumbnail_path:
958+
source_entity = entities.pop(0)
959+
if filmstrip_thumbnail:
960+
thumb_id = self.upload_filmstrip_thumbnail(source_entity['type'],
961+
source_entity['id'], thumbnail_path, **kwargs)
962+
else:
963+
thumb_id = self.upload_thumbnail(source_entity['type'],
964+
source_entity['id'], thumbnail_path, **kwargs)
965+
else:
966+
if not isinstance(source_entity, dict) or 'id' not in source_entity \
967+
or 'type' not in source_entity:
968+
raise ShotgunError("'source_entity' parameter must be a dict "\
969+
"with at least 'type' and 'id' keys.\nGot: %s (%s)" \
970+
% (source_entity, type(source_entity)))
971+
972+
# only 1 entity in list and we already uploaded the thumbnail to it
973+
if len(entities) == 0:
974+
return thumb_id
975+
976+
# update entities with source_entity thumbnail
977+
entities_str = []
978+
for e in entities:
979+
entities_str.append("%s_%s" % (e['type'], e['id']))
980+
# format for post request
981+
if filmstrip_thumbnail:
982+
filmstrip_thumbnail = 1
983+
params = {
984+
"entities" : ','.join(entities_str),
985+
"source_entity": "%s_%s" % (source_entity['type'], source_entity['id']),
986+
"filmstrip_thumbnail" : filmstrip_thumbnail,
987+
"script_name" : self.config.script_name,
988+
"script_key" : self.config.api_key,
989+
}
990+
if self.config.session_uuid:
991+
params["session_uuid"] = self.config.session_uuid
934992

993+
# Create opener with extended form post support
994+
opener = self._build_opener(FormPostHandler)
995+
url = urlparse.urlunparse((self.config.scheme, self.config.server,
996+
"/upload/share_thumbnail", None, None, None))
935997

998+
# Perform the request
999+
try:
1000+
resp = opener.open(url, params)
1001+
result = resp.read()
1002+
# response heaers are in str(resp.info()).splitlines()
1003+
except urllib2.HTTPError, e:
1004+
if e.code == 500:
1005+
raise ShotgunError("Server encountered an internal error. "
1006+
"\n%s\n(%s)\n%s\n\n" % (url, params, e))
1007+
else:
1008+
raise ShotgunError("Unanticipated error occurred %s" % (e))
1009+
else:
1010+
if not str(result).startswith("1"):
1011+
raise ShotgunError("Unable to share thumbnail: %s" % result)
1012+
else:
1013+
attachment_id = int(str(result).split(":")[1].split("\n")[0])
1014+
1015+
return attachment_id
1016+
9361017
def upload_thumbnail(self, entity_type, entity_id, path, **kwargs):
9371018
"""Convenience function for uploading thumbnails, see upload.
9381019
"""

0 commit comments

Comments
 (0)