Skip to content

Commit 4cf6ae6

Browse files
authored
Add support for new Tweets endpoint (#228)
* Add new Tweets endpoint * Add Enum * remove deprecated fixtures * Add test
1 parent b5e11cc commit 4cf6ae6

File tree

5 files changed

+123
-24
lines changed

5 files changed

+123
-24
lines changed

tests/fixtures/tweet_preview.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/fixtures/tweets_get.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"request": {
3+
"params": {
4+
"tweet_ids": [
5+
"1166476031668015104"
6+
],
7+
"tweet_type": "PUBLISHED",
8+
"trim_user": true,
9+
"account_id": "2iqph"
10+
}
11+
},
12+
"next_cursor": null,
13+
"data": [
14+
{
15+
"coordinates": null,
16+
"retweeted": false,
17+
"source": "<a href=\"https://ads-api.twitter.com\" rel=\"nofollow\">Ads API Internal Test App</a>",
18+
"entities": {
19+
"hashtags": [],
20+
"symbols": [],
21+
"user_mentions": [],
22+
"urls": []
23+
},
24+
"display_text_range": [
25+
0,
26+
9
27+
],
28+
"favorite_count": 0,
29+
"in_reply_to_status_id_str": null,
30+
"geo": null,
31+
"id_str": "1166476031668015104",
32+
"scopes": {
33+
"followers": false
34+
},
35+
"in_reply_to_user_id": null,
36+
"truncated": false,
37+
"retweet_count": 0,
38+
"id": 1166476031668015104,
39+
"in_reply_to_status_id": null,
40+
"nullcast": true,
41+
"created_at": "Tue Aug 27 22:22:12 +0000 2019",
42+
"place": null,
43+
"scheduled_at": null,
44+
"tweet_type": "PUBLISHED",
45+
"favorited": false,
46+
"full_text": "hello, v6",
47+
"lang": "es",
48+
"contributors": [
49+
2417045708
50+
],
51+
"in_reply_to_screen_name": null,
52+
"in_reply_to_user_id_str": null,
53+
"user": {
54+
"id": 756201191646691328,
55+
"id_str": "756201191646691328"
56+
},
57+
"tweet_id": "1166476031668015104"
58+
}
59+
]
60+
}

tests/test_tweets_get.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import responses
2+
import unittest
3+
4+
from tests.support import with_resource, with_fixture, characters
5+
6+
from twitter_ads.account import Account
7+
from twitter_ads.creative import Tweets
8+
from twitter_ads.client import Client
9+
from twitter_ads.cursor import Cursor
10+
from twitter_ads.enum import TWEET_TYPE
11+
from twitter_ads import API_VERSION
12+
13+
14+
@responses.activate
15+
def test_tweets_get_all():
16+
responses.add(responses.GET,
17+
with_resource('/' + API_VERSION + '/accounts/2iqph'),
18+
body=with_fixture('accounts_load'),
19+
content_type='application/json')
20+
21+
responses.add(responses.GET,
22+
with_resource('/' + API_VERSION + '/accounts/2iqph/tweets'),
23+
body=with_fixture('tweets_get'),
24+
content_type='application/json')
25+
26+
client = Client(
27+
characters(40),
28+
characters(40),
29+
characters(40),
30+
characters(40)
31+
)
32+
33+
account = Account.load(client, '2iqph')
34+
35+
tweets = Tweets.all(
36+
account,
37+
tweet_ids=['1166476031668015104'],
38+
tweet_type=TWEET_TYPE.PUBLISHED,
39+
trim_user=True
40+
)
41+
42+
assert tweets is not None
43+
assert isinstance(tweets, Cursor)
44+
assert tweets.count == 1
45+
assert tweets.first['tweet_id'] == '1166476031668015104'

twitter_ads/creative.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,3 +580,15 @@ def load(klass, account, **kwargs):
580580
# read-only
581581
resource_property(TweetPreview, 'preview', readonly=True)
582582
resource_property(TweetPreview, 'tweet_id', readonly=True)
583+
584+
585+
class Tweets(object):
586+
587+
RESOURCE_COLLECTION = '/' + API_VERSION + '/accounts/{account_id}/tweets'
588+
589+
@classmethod
590+
@FlattenParams
591+
def all(klass, account, **kwargs):
592+
resource = klass.RESOURCE_COLLECTION.format(account_id=account.id)
593+
request = Request(account.client, 'get', resource, params=kwargs)
594+
return Cursor(None, request)

twitter_ads/enum.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ def enum(**enums):
195195
SCHEDULED='SCHEDULED'
196196
)
197197

198+
TIMELINE_TYPE = enum(
199+
ALL='ALL',
200+
NULLCAST='NULLCAST',
201+
ORGANIC='ORGANIC'
202+
)
203+
198204
SEGMENTATION_TYPE = enum(
199205
AGE='AGE',
200206
APP_STORE_CATEGORY='APP_STORE_CATEGORY',

0 commit comments

Comments
 (0)