Started adding support for Twitter Ads API.
This commit is contained in:
parent
885051acdc
commit
3219026432
5 changed files with 604 additions and 0 deletions
|
|
@ -28,3 +28,6 @@ test_list_owner_screen_name = os.environ.get('TEST_LIST_OWNER_SCREEN_NAME',
|
|||
|
||||
test_tweet_object = {u'contributors': None, u'truncated': False, u'text': u'http://t.co/FCmXyI6VHd is a #cool site, lol! @mikehelmick shd #checkitout. Love, @__twython__ https://t.co/67pwRvY6z9 http://t.co/N6InAO4B71', u'in_reply_to_status_id': None, u'id': 349683012054683648, u'favorite_count': 0, u'source': u'web', u'retweeted': False, u'coordinates': None, u'entities': {u'symbols': [], u'user_mentions': [{u'id': 29251354, u'indices': [45, 57], u'id_str': u'29251354', u'screen_name': u'mikehelmick', u'name': u'Mike Helmick'}, {u'id': 1431865928, u'indices': [81, 93], u'id_str': u'1431865928', u'screen_name': u'__twython__', u'name': u'Twython'}], u'hashtags': [{u'indices': [28, 33], u'text': u'cool'}, {u'indices': [62, 73], u'text': u'checkitout'}], u'urls': [{u'url': u'http://t.co/FCmXyI6VHd', u'indices': [0, 22], u'expanded_url': u'http://google.com', u'display_url': u'google.com'}, {u'url': u'https://t.co/67pwRvY6z9', u'indices': [94, 117], u'expanded_url': u'https://github.com', u'display_url': u'github.com'}], u'media': [{u'id': 537884378513162240, u'id_str': u'537884378513162240', u'indices': [118, 140], u'media_url': u'http://pbs.twimg.com/media/B3by_g-CQAAhrO5.jpg', u'media_url_https': u'https://pbs.twimg.com/media/B3by_g-CQAAhrO5.jpg', u'url': u'http://t.co/N6InAO4B71', u'display_url': u'pic.twitter.com/N6InAO4B71', u'expanded_url': u'http://twitter.com/pingofglitch/status/537884380060844032/photo/1', u'type': u'photo', u'sizes': {u'large': {u'w': 1024, u'h': 640, u'resize': u'fit'}, u'thumb': {u'w': 150, u'h': 150, u'resize': u'crop'}, u'medium': {u'w': 600, u'h': 375, u'resize': u'fit'}, u'small': {u'w': 340, u'h': 212, u'resize': u'fit'}}}]}, u'in_reply_to_screen_name': None, u'id_str': u'349683012054683648', u'retweet_count': 0, u'in_reply_to_user_id': None, u'favorited': False, u'user': {u'follow_request_sent': False, u'profile_use_background_image': True, u'default_profile_image': True, u'id': 1431865928, u'verified': False, u'profile_text_color': u'333333', u'profile_image_url_https': u'https://si0.twimg.com/sticky/default_profile_images/default_profile_3_normal.png', u'profile_sidebar_fill_color': u'DDEEF6', u'entities': {u'description': {u'urls': []}}, u'followers_count': 1, u'profile_sidebar_border_color': u'C0DEED', u'id_str': u'1431865928', u'profile_background_color': u'3D3D3D', u'listed_count': 0, u'profile_background_image_url_https': u'https://si0.twimg.com/images/themes/theme1/bg.png', u'utc_offset': None, u'statuses_count': 2, u'description': u'', u'friends_count': 1, u'location': u'', u'profile_link_color': u'0084B4', u'profile_image_url': u'http://a0.twimg.com/sticky/default_profile_images/default_profile_3_normal.png', u'following': False, u'geo_enabled': False, u'profile_background_image_url': u'http://a0.twimg.com/images/themes/theme1/bg.png', u'screen_name': u'__twython__', u'lang': u'en', u'profile_background_tile': False, u'favourites_count': 0, u'name': u'Twython', u'notifications': False, u'url': None, u'created_at': u'Thu May 16 01:11:09 +0000 2013', u'contributors_enabled': False, u'time_zone': None, u'protected': False, u'default_profile': False, u'is_translator': False}, u'geo': None, u'in_reply_to_user_id_str': None, u'possibly_sensitive': False, u'lang': u'en', u'created_at': u'Wed Jun 26 00:18:21 +0000 2013', u'in_reply_to_status_id_str': None, u'place': None}
|
||||
test_tweet_html = '<a href="http://t.co/FCmXyI6VHd" class="twython-url">google.com</a> is a <a href="https://twitter.com/search?q=%23cool" class="twython-hashtag">#cool</a> site, lol! <a href="https://twitter.com/mikehelmick" class="twython-mention">@mikehelmick</a> shd <a href="https://twitter.com/search?q=%23checkitout" class="twython-hashtag">#checkitout</a>. Love, <a href="https://twitter.com/__twython__" class="twython-mention">@__twython__</a> <a href="https://t.co/67pwRvY6z9" class="twython-url">github.com</a> <a href="http://t.co/N6InAO4B71" class="twython-media">pic.twitter.com/N6InAO4B71</a>'
|
||||
|
||||
test_account_id = os.environ.get('TEST_ACCOUNT_ID')
|
||||
test_funding_instrument_id = os.environ.get('TEST_FUNDING_INSTRUMENT_ID')
|
||||
|
|
|
|||
23
tests/test_core_ads.py
Normal file
23
tests/test_core_ads.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
from .config import (
|
||||
test_tweet_object, test_tweet_html, unittest
|
||||
)
|
||||
|
||||
import responses
|
||||
import requests
|
||||
from twython.api_ads import TwythonAds
|
||||
|
||||
from twython.compat import is_py2
|
||||
if is_py2:
|
||||
from StringIO import StringIO
|
||||
else:
|
||||
from io import StringIO
|
||||
|
||||
try:
|
||||
import unittest.mock as mock
|
||||
except ImportError:
|
||||
import mock
|
||||
|
||||
|
||||
class TwythonAPITestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.api = TwythonAds('', '', '', '')
|
||||
72
tests/test_endpoints_ads.py
Normal file
72
tests/test_endpoints_ads.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import datetime
|
||||
from twython import Twython, TwythonError, TwythonAuthError
|
||||
|
||||
from .config import (
|
||||
app_key, app_secret, oauth_token, oauth_token_secret,
|
||||
protected_twitter_1, protected_twitter_2, screen_name,
|
||||
test_tweet_id, test_list_slug, test_list_owner_screen_name,
|
||||
access_token, test_tweet_object, test_tweet_html, test_account_id, test_funding_instrument_id, unittest
|
||||
)
|
||||
|
||||
import time
|
||||
from twython.api_ads import TwythonAds
|
||||
|
||||
|
||||
class TwythonEndpointsTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
|
||||
client_args = {
|
||||
'headers': {
|
||||
'User-Agent': '__twython__ Test'
|
||||
},
|
||||
'allow_redirects': False
|
||||
}
|
||||
|
||||
# This is so we can hit coverage that Twython sets
|
||||
# User-Agent for us if none is supplied
|
||||
oauth2_client_args = {
|
||||
'headers': {}
|
||||
}
|
||||
|
||||
self.api = TwythonAds(app_key, app_secret,
|
||||
oauth_token, oauth_token_secret,
|
||||
client_args=client_args)
|
||||
|
||||
self.oauth2_api = Twython(app_key, access_token=access_token,
|
||||
client_args=oauth2_client_args)
|
||||
|
||||
def test_get_accounts(self):
|
||||
accounts = self.api.get_accounts()
|
||||
self.assertTrue(len(accounts) > 0)
|
||||
|
||||
def test_get_account(self):
|
||||
account = self.api.get_account(test_account_id)
|
||||
self.assertEqual(account['id'], test_account_id)
|
||||
with self.assertRaises(TwythonError):
|
||||
self.api.get_account('1234')
|
||||
|
||||
def test_get_funding_instruments(self):
|
||||
funding_instruments = self.api.get_funding_instruments(test_account_id)
|
||||
self.assertTrue(len(funding_instruments) > 0)
|
||||
|
||||
def test_get_funding_instrument(self):
|
||||
funding_instrument = self.api.get_funding_instrument(test_account_id, test_funding_instrument_id)
|
||||
self.assertEqual(funding_instrument['id'], test_funding_instrument_id)
|
||||
self.assertEqual(funding_instrument['account_id'], test_account_id)
|
||||
with self.assertRaises(TwythonError):
|
||||
self.api.get_funding_instrument('1234', '1234')
|
||||
|
||||
def test_get_campaigns(self):
|
||||
campaigns = self.api.get_campaigns(test_account_id)
|
||||
self.assertTrue(len(campaigns) > 0)
|
||||
|
||||
def test_create_campaign(self):
|
||||
new_campaign = {
|
||||
'name': 'Test Twitter campaign - Twython',
|
||||
'funding_instrument_id': test_funding_instrument_id,
|
||||
'start_time': datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%SZ'),
|
||||
'daily_budget_amount_local_micro': 10 * 1000000
|
||||
}
|
||||
campaign = self.api.create_campaign(test_account_id, **new_campaign)
|
||||
self.assertEqual(campaign['account_id'], test_account_id)
|
||||
self.assertIsNotNone(campaign['id'])
|
||||
Loading…
Add table
Add a link
Reference in a new issue