def getentitiesfromshotgun(name): sg = Shotgun(const.SHOTGUN_URL, const.API_NAME, const.API_KEY) sgname = name if name in const.ENTITY_MAP: sgname = const.ENTITY_MAP[name] fields = sg.schema_field_read(sgname) rawents = sg.find(sgname, [['project', 'is', { 'type': 'Project', 'id': 70 }]], list(fields.keys())) pp(rawents) clean = [] for ent in rawents: if 'image' in ent and ent['image']: directory = "data/filestorage/%s/%s/%s" % (name, ent['id'], "image") if not os.path.exists(os.path.dirname(directory)): os.makedirs(os.path.dirname(directory)) sg.download_attachment({'url': ent['image']}, directory) ent['image'] = directory for field in fields: if fields[field]['data_type']['value'] == 'url' and ent[field]: pp(ent) directory = "data/filestorage/%s/%s/%s" % (name, ent['id'], ent[field]['name']) if not isinstance(ent['id'], int): directory = "data/filestorage/%s/%s/%s" % ( name, ent['id']['value'], ent[field]['name']) if not os.path.exists(os.path.dirname(directory)): os.makedirs(os.path.dirname(directory)) sg.download_attachment(ent[field], directory) ent[field] = directory ent[field] = { "value": ent[field], "type": fields[field]['data_type']['value'] } ent.pop("created_at", None) ent.pop("created_by", None) ent.pop("updated_at", None) ent.pop("updated_by", None) ent.pop("filmstrip_image", None) ent.pop("cached_display_name", None) dic = {} dic['type'] = {'type': 'text', 'value': ent.pop('type')} for f in ent: dic[fields[f]['name']['value']] = ent[f] clean.append(dic) return clean
from shotgun_api3 import Shotgun import os SCRIPT_NAME = 'Samuel - API' SCRIPT_KEY = '2b3f3b6e442242c067501a9e17503bac1d27b6ea244a4e4b5987e26d5f6520e2' sg = Shotgun("https://objeus.shotgunstudio.com", SCRIPT_NAME, SCRIPT_KEY) epers = sg.find('Attachment', [['attachment_links', 'type_is', 'CustomEntity07']], ['attachment_links']) for eper in epers: sg.download_attachment( eper, os.path.abspath( os.path.expanduser("importdata/" + eper['attachment_links'][0]['name'] + ".json")))
class ShotgunUtils(): ''' a light version of the shotgun utils class to connect and update shotgun task for external artist ''' def __init__(self): ''' creates the connection to the shotgun site by api ''' ''' shotgun conection ''' SERVER_PATH = "https://hcpstudio.shotgunstudio.com" SCRIPT_NAME = 'Tracker' SCRIPT_KEY = '99b5c166044037cc2d04646b1dfd58b2f44e8a146b710b425b8f561f2a21e49d' self.sg = Shotgun(SERVER_PATH, SCRIPT_NAME, SCRIPT_KEY) self.userId = None self.userDic = {} self.tasks = None self.projectPath = None def getsgParameters(self, sgType, parameter=None): schema = self.sg.schema_field_read(sgType, parameter) print schema def getUserId(self, userName): filters = [['name', 'is', userName]] field = ['id', 'name', 'sg_projectpath', 'sg_keyword', 'login'] user = self.sg.find_one('HumanUser', filters, field) if user: self.userId = user['id'] self.projectPath = user['sg_projectpath'] self.userDic = user return user['id'] else: print "no {0} User found ".format(userName) return None def taskByUser(self): if not self.userId == None: filter = [[ 'task_assignees', 'is', { 'type': 'HumanUser', 'id': self.userId } ], ['sg_status_list', 'is_not', 'fin'], ['sg_status_list', 'is_not', 'apr'], ['sg_status_list', 'is_not', 'cmpt']] fields = [ 'id', 'content', 'sg_status_list', 'start_date', 'due_date', 'sg_complexity', 'sg_priority_1', 'sg_note', 'project', 'entity', 'sg_digitalmedia', 'sg_displayname' ] order = [{ 'field_name': 'due_date', 'direction': 'asc' }, { 'field_name': 'sg_priority_1', 'direction': 'asc' }, { 'field_name': 'sg_complexity', 'direction': 'desc' }] taskList = self.sg.find('Task', filter, fields, order) if taskList: self.tasks = taskList return taskList else: self.tasks = [] print "no task Asigned to: ", self.userId return taskList else: taskList = [] print "no Id found" return taskList def getTaskById(self, taskId): filter = [['id', 'is', taskId]] fields = [ 'id', 'content', 'sg_status_list', 'start_date', 'due_date', 'sg_complexity', 'sg_priority_1', 'sg_note', 'project', 'entity', 'sg_digitalmedia', 'sg_displayname' ] task = self.sg.find_one('Task', filter, fields) if task: return task else: print 'no task found' return None def updateStatusFromUser(self, taskId, status): task = self.getTaskById(taskId) if task: project = task['project'] sgStatus = status data = {'project': project, 'sg_status_list': sgStatus} self.sg.update('Task', taskId, data) return 1 else: print 'No task by this id' return None def updateProjectPath(self, projectPath): data = {'sg_projectpath': projectPath} self.sg.update('HumanUser', self.userId, data) def getNotes(self, sgTaskId): task = self.getTaskById(sgTaskId) if task: filters = [['tasks', 'is', task]] fields = ['content', 'created_at', 'user', 'addressings_to'] order = [{'field_name': 'created_at', 'direction': 'asc'}] notes = self.sg.find('Note', filters, fields, order) return notes else: print 'no Task' return None def createNote(self, taskId, content): task = self.getTaskById(taskId) if task: data = { 'project': task['project'], 'content': content, 'tasks': [task], 'user': self.userDic, 'subject': 'Note' } note = self.sg.create('Note', data) return note['id'] else: return None def uploadAttachment(self, taskId, filePath, tag): task = self.getTaskById(taskId) if task: entity = task['entity'] if entity: entityType = entity['type'] entityId = entity['id'] uploadedfile = self.sg.upload(entityType, entityId, filePath) if uploadedfile: data = {'sg_type': tag, 'sg_taskid': taskId} self.sg.update('Attachment', uploadedfile, data) return uploadedfile else: return None else: print 'no entity set' return None def downloadAttachment(self, taskId, downloadPath, parent=None): filters = [['sg_taskid', 'is', taskId], ['sg_type', 'is', 'REFERENCE']] fields = ['id', 'attachment_links', 'filename', 'created_at'] attachments = self.sg.find('Attachment', filters, fields) if attachments: progressBar = QtGui.QProgressBar(parent) progressBar.show() size = len(attachments) for x, attach in enumerate(attachments): extension = path.splitext(attach['filename']) dt = attach['created_at'].strftime("%Y_%m_%d_%H-%M-%S") name = attach['attachment_links'][0]['id'] namePadded = '{0}_{3}.{1:04d}{2}'.format( name, x, extension[-1], dt) fullPath = path.join(downloadPath, namePadded) dwFile = self.sg.download_attachment(attach, fullPath, attach['id']) progressBar.setValue(((x + 1) / size) * 100) progressBar.close() return attachments else: return None def uploadReference(self, entityType, entityId, filePath, tag, taskId): uploadedfile = self.sg.upload(entityType, entityId, filePath) if uploadedfile: data = { 'sg_type': tag, 'sg_type': 'REFERENCE', 'sg_taskid': taskId } self.sg.update('Attachment', uploadedfile, data) return uploadedfile else: return None