|
| 1 | +from collections import OrderedDict |
| 2 | + |
| 3 | +from .exception import ValidationException |
| 4 | +from .hasty_object import HastyObject |
| 5 | + |
| 6 | + |
| 7 | +class ActivityType(HastyObject): |
| 8 | + endpoint = '/v1/projects/{project_id}/activity_types' |
| 9 | + endpoint_class = '/v1/projects/{project_id}/activity_types/{activity_type_id}' |
| 10 | + |
| 11 | + def __repr__(self): |
| 12 | + return self.get__repr__(OrderedDict({"id": self._id, "name": self._name, "color": self._color})) |
| 13 | + |
| 14 | + @property |
| 15 | + def id(self): |
| 16 | + """ |
| 17 | + :type: string |
| 18 | + """ |
| 19 | + return self._id |
| 20 | + |
| 21 | + @property |
| 22 | + def name(self): |
| 23 | + """ |
| 24 | + :type: string |
| 25 | + """ |
| 26 | + return self._name |
| 27 | + |
| 28 | + @property |
| 29 | + def project_id(self): |
| 30 | + """ |
| 31 | + :type: string |
| 32 | + """ |
| 33 | + return self._project_id |
| 34 | + |
| 35 | + @property |
| 36 | + def color(self): |
| 37 | + """ |
| 38 | + :type: string |
| 39 | + """ |
| 40 | + return self._color |
| 41 | + |
| 42 | + def _init_properties(self): |
| 43 | + self._id = None |
| 44 | + self._name = None |
| 45 | + self._project_id = None |
| 46 | + self._color = None |
| 47 | + |
| 48 | + def _set_prop_values(self, data): |
| 49 | + if "id" in data: |
| 50 | + self._id = data["id"] |
| 51 | + if "name" in data: |
| 52 | + self._name = data["name"] |
| 53 | + if "project_id" in data: |
| 54 | + self._project_id = data["project_id"] |
| 55 | + if "color" in data: |
| 56 | + self._color = data["color"] |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def _create(cls, requester, project_id, name, color=None): |
| 60 | + res = requester.post(cls.endpoint.format(project_id=project_id), |
| 61 | + json_data={"name": name, |
| 62 | + "color": color}) |
| 63 | + return cls(requester, res, {"project_id": project_id}) |
| 64 | + |
| 65 | + def edit(self, name, color=None): |
| 66 | + """ |
| 67 | + Edit activity type properties |
| 68 | +
|
| 69 | + Arguments: |
| 70 | + name (str): Label class name |
| 71 | + color (str, optional): Color in HEX format #0f0f0faa |
| 72 | + """ |
| 73 | + self._requester.put(self.endpoint_class.format(project_id=self._project_id, activity_type_id=self._id), |
| 74 | + json_data={"name": name, "color": color}) |
| 75 | + self._name = name |
| 76 | + self._color = color |
| 77 | + |
| 78 | + def delete(self): |
| 79 | + """ |
| 80 | + Delete activity type |
| 81 | + """ |
| 82 | + self._requester.delete(self.endpoint_class.format(project_id=self._project_id, activity_type_id=self._id)) |
| 83 | + |
| 84 | + |
| 85 | +class Activity(HastyObject): |
| 86 | + endpoint = '/v1/projects/{project_id}/videos/{video_id}/segments' |
| 87 | + endpoint_class = '/v1/projects/{project_id}/segments/{segment_id}' |
| 88 | + |
| 89 | + def __repr__(self): |
| 90 | + return self.get__repr__(OrderedDict({"id": self._id, "activities": self._activities, |
| 91 | + "start": self._start_time_ms, "end": self._end_time_ms})) |
| 92 | + |
| 93 | + @property |
| 94 | + def id(self): |
| 95 | + """ |
| 96 | + :type: string |
| 97 | + """ |
| 98 | + return self._id |
| 99 | + |
| 100 | + @property |
| 101 | + def video_id(self): |
| 102 | + """ |
| 103 | + :type: string |
| 104 | + """ |
| 105 | + return self._video_id |
| 106 | + |
| 107 | + @property |
| 108 | + def activities(self): |
| 109 | + """ |
| 110 | + :type: list |
| 111 | + """ |
| 112 | + return self._activities |
| 113 | + |
| 114 | + @property |
| 115 | + def start_time_ms(self): |
| 116 | + """ |
| 117 | + :type: int |
| 118 | + """ |
| 119 | + return self._start_time_ms |
| 120 | + |
| 121 | + @property |
| 122 | + def end_time_ms(self): |
| 123 | + """ |
| 124 | + :type: int |
| 125 | + """ |
| 126 | + return self._end_time_ms |
| 127 | + |
| 128 | + def _init_properties(self): |
| 129 | + self._id = None |
| 130 | + self._video_id = None |
| 131 | + self._activities = None |
| 132 | + self._start_time_ms = None |
| 133 | + self._end_time_ms = None |
| 134 | + self._project_id = None |
| 135 | + |
| 136 | + def _set_prop_values(self, data): |
| 137 | + if "id" in data: |
| 138 | + self._id = data["id"] |
| 139 | + if "video_id" in data: |
| 140 | + self._video_id = data["video_id"] |
| 141 | + if "activities" in data: |
| 142 | + self._activities = data["activities"] |
| 143 | + if "start_time_ms" in data: |
| 144 | + self._start_time_ms = data["start_time_ms"] |
| 145 | + if "end_time_ms" in data: |
| 146 | + self._end_time_ms = data["end_time_ms"] |
| 147 | + if "project_id" in data: |
| 148 | + self._project_id = data["project_id"] |
| 149 | + |
| 150 | + @classmethod |
| 151 | + def _create(cls, requester, project_id, video_id, activities, |
| 152 | + start_time_ms, end_time_ms, replace_overlap=False): |
| 153 | + if len(activities) == 0 or not isinstance(activities, list): |
| 154 | + raise ValidationException.invalid_activities() |
| 155 | + type_ids = [] |
| 156 | + for a in activities: |
| 157 | + if isinstance(a, ActivityType): |
| 158 | + type_ids.append(a.id) |
| 159 | + elif isinstance(a, str): |
| 160 | + type_ids.append(a) |
| 161 | + else: |
| 162 | + raise ValidationException.invalid_activities() |
| 163 | + query_params = None |
| 164 | + if replace_overlap: |
| 165 | + query_params = {"replace_overlap": replace_overlap} |
| 166 | + res = requester.post(cls.endpoint.format(project_id=project_id, video_id=video_id), |
| 167 | + json_data={"activities": type_ids, |
| 168 | + "start_time_ms": start_time_ms, |
| 169 | + "end_time_ms": end_time_ms}, |
| 170 | + query_params=query_params) |
| 171 | + return cls(requester, res, {"project_id": project_id}) |
| 172 | + |
| 173 | + def delete(self): |
| 174 | + """ |
| 175 | + Delete activity |
| 176 | + """ |
| 177 | + self._requester.delete(self.endpoint_class.format(project_id=self._project_id, segment_id=self._id)) |
| 178 | + |
| 179 | + def edit(self, start_time_ms, end_time_ms, activities): |
| 180 | + """ |
| 181 | + Edit activity properties |
| 182 | +
|
| 183 | + Arguments: |
| 184 | + start_time_ms (int): Start time in milliseconds |
| 185 | + end_time_ms (int): End time in milliseconds |
| 186 | + activities (list): List of `~hasty.ActivityType` or `str` (IDs) |
| 187 | + """ |
| 188 | + if len(activities) == 0 or not isinstance(activities, list): |
| 189 | + raise ValidationException.invalid_activities() |
| 190 | + type_ids = [] |
| 191 | + for a in activities: |
| 192 | + if isinstance(a, ActivityType): |
| 193 | + type_ids.append(a.id) |
| 194 | + elif isinstance(a, str): |
| 195 | + type_ids.append(a) |
| 196 | + else: |
| 197 | + raise ValidationException.invalid_activities() |
| 198 | + res = self._requester.put(self.endpoint_class.format(project_id=self._project_id, segment_id=self._id), |
| 199 | + json_data={"activities": type_ids, |
| 200 | + "start_time_ms": start_time_ms, |
| 201 | + "end_time_ms": end_time_ms}) |
| 202 | + self._set_prop_values(res) |
| 203 | + return Activity(self._requester, res, {"project_id": self._project_id}) |
0 commit comments