|
9 | 9 | from twitter_ads.cursor import Cursor
|
10 | 10 | from twitter_ads import API_VERSION
|
11 | 11 |
|
| 12 | +import json |
| 13 | + |
12 | 14 |
|
13 | 15 | class TailoredAudience(Resource):
|
14 | 16 |
|
@@ -183,3 +185,67 @@ def delete(self):
|
183 | 185 | resource_property(TailoredAudiencePermission, 'tailored_audience_id')
|
184 | 186 | resource_property(TailoredAudiencePermission, 'granted_account_id')
|
185 | 187 | resource_property(TailoredAudiencePermission, 'permission_level')
|
| 188 | + |
| 189 | + |
| 190 | +class AudienceIntelligence(Resource): |
| 191 | + |
| 192 | + PROPERTIES = {} |
| 193 | + |
| 194 | + RESOURCE_BASE = '/' + API_VERSION + '/accounts/{account_id}/audience_intelligence/' |
| 195 | + RESOURCE_CONVERSATIONS = RESOURCE_BASE + 'conversations' |
| 196 | + RESOURCE_DEMOGRAPHICS = RESOURCE_BASE + 'demographics' |
| 197 | + METHOD = 'post' |
| 198 | + HEADERS = {'Content-Type': 'application/json'} |
| 199 | + |
| 200 | + @classmethod |
| 201 | + def __get(klass, account, client, params): |
| 202 | + """ |
| 203 | + Helper function to get the conversation data |
| 204 | + Returns a Cursor instance |
| 205 | + """ |
| 206 | + resource = klass.RESOURCE_CONVERSATIONS.format(account_id=account.id) |
| 207 | + |
| 208 | + request = Request( |
| 209 | + account.client, klass.METHOD, |
| 210 | + resource, headers=klass.HEADERS, body=params) |
| 211 | + return Cursor(klass, request, init_with=[account]) |
| 212 | + |
| 213 | + def conversations(self): |
| 214 | + """ |
| 215 | + Get the conversation topics for an input targeting criteria |
| 216 | + """ |
| 217 | + body = { |
| 218 | + "conversation_type": self.conversation_type, |
| 219 | + "audience_definition": self.audience_definition, |
| 220 | + "targeting_inputs": self.targeting_inputs |
| 221 | + } |
| 222 | + return self.__get(account=self.account, client=self.account.client, params=json.dumps(body)) |
| 223 | + |
| 224 | + def demographics(self): |
| 225 | + """ |
| 226 | + Get the demographic breakdown for an input targeting criteria |
| 227 | + """ |
| 228 | + body = { |
| 229 | + "audience_definition": self.audience_definition, |
| 230 | + "targeting_inputs": self.targeting_inputs |
| 231 | + } |
| 232 | + resource = self.RESOURCE_DEMOGRAPHICS.format(account_id=self.account.id) |
| 233 | + response = Request( |
| 234 | + self.account.client, self.METHOD, |
| 235 | + resource, headers=self.HEADERS, body=json.dumps(body)).perform() |
| 236 | + return response.body['data'] |
| 237 | + |
| 238 | + |
| 239 | +# tailored audience permission properties |
| 240 | +# read-only |
| 241 | +resource_property(AudienceIntelligence, 'operator_type', readonly=True) |
| 242 | +resource_property(AudienceIntelligence, 'targeting_type', readonly=True) |
| 243 | +resource_property(AudienceIntelligence, 'targeting_value', readonly=True) |
| 244 | +resource_property(AudienceIntelligence, 'localized', readonly=True) |
| 245 | +# writable |
| 246 | +resource_property(AudienceIntelligence, 'conversation_type') |
| 247 | +resource_property(AudienceIntelligence, 'targeting_inputs') |
| 248 | +resource_property(AudienceIntelligence, 'audience_definition') |
| 249 | +# demographics only |
| 250 | +resource_property(AudienceIntelligence, 'start_time', transform=TRANSFORM.TIME) |
| 251 | +resource_property(AudienceIntelligence, 'end_time', transform=TRANSFORM.TIME) |
0 commit comments