Exemplo n.º 1
0
 def all(klass, account, **kwargs):
     resource = klass.RESOURCE_COLLECTION.format(account_id=account.id)
     request = Request(account.client, 'get', resource, params=kwargs)
     return Cursor(None, request)
Exemplo n.º 2
0
 def delete(self):
     resource = self.RESOURCE.format(account_id=self.account.id,
                                     id=self.media_key)
     response = Request(self.account.client, 'delete', resource).perform()
     self.from_response(response.body['data'])
Exemplo n.º 3
0
 def load(klass, account, **kwargs):
     resource = klass.RESOURCE_COLLECTION.format(account_id=account.id)
     request = Request(account.client, 'get', resource, params=kwargs)
     return Cursor(klass, request, init_with=[account])
Exemplo n.º 4
0
 def all(klass, account, **kwargs):
     """Returns a Cursor instance for a given resource."""
     resource = klass.RESOURCE_COLLECTION.format(account_id=account.id)
     request = Request(account.client, 'get', resource, params=kwargs)
     return Cursor(klass, request, init_with=[account])
    def fetch_tailored_audience(self,
                                data,
                                syncData=False,
                                oauth_token=settings.TW_ACCESS_TOKEN,
                                oauth_token_secret=settings.TW_ACCESS_SECRET):
        res = {}
        res['data'] = []
        res['success'] = False
        account_id = data.get('account_id')

        if isinstance(account_id, (int, long)):
            account_id_int = account_id
            account_id = int_to_base36(account_id)

        if account_id is None:
            res = {
                'data': {},
                'success': False,
                'message': "Missing Twitter Account ID"
            }
            return res

        client = Client(settings.TW_CONSUMER_KEY, settings.TW_CONSUMER_SECRET,
                        oauth_token, oauth_token_secret)
        if settings.TW_SANDBOX:
            client.sandbox = settings.TW_SANDBOX
        try:
            account = client.accounts(account_id)
            resource = '/{api_version}/accounts/{account_id}/tailored_audiences?count=1000'.format(
                api_version=settings.TW_API_VERSION, account_id=account_id)
            response = Request(client, 'get', resource).perform()
            tailored_audiences = response.body['data']

            next_cursor = None
            if response.body['next_cursor'] and response.body[
                    'next_cursor'] is not 0:
                next_cursor = response.body['next_cursor']
                while next_cursor is not 0:
                    resource = '/{api_version}/accounts/{account_id}/tailored_audiences?cursor={next_cursor}&count=1000'.format(
                        api_version=settings.TW_API_VERSION,
                        account_id=account_id,
                        next_cursor=next_cursor)
                    response = Request(client, 'get', resource).perform()
                    next_cursor = response.body['next_cursor'] or 0
                    tailored_audiences += response.body['data']

            api_domain = 'https://ads.twitter.com'

            resource = '/accounts/{account_id}/apps.json'.format(
                account_id=account_id)
            response = Request(client, 'get', resource,
                               domain=api_domain).perform()

            apps = response.body.get('apps_info', {})
            app_table = []
            for (k, v) in apps.iteritems():
                app_store_identifier = v.get('app_store_identifier')
                app_name = v.get('title')
                if app_store_identifier and app_name:
                    app_table.append({
                        'id': app_store_identifier,
                        'name': app_name
                    })

            def replace_app_name(app_name):
                for app_info in app_table:
                    app_name = app_name.replace(app_info['id'],
                                                app_info['name'])

                return app_name.replace('_', ' ')

            def human_format(num):
                magnitude = 0
                while abs(num) >= 1000:
                    magnitude += 1
                    num /= 1000.0
                # add more suffixes if you need them
                return '%.2f%s' % (num, ['', 'K', 'M', 'G', 'T', 'P'
                                         ][magnitude])

            for audience in tailored_audiences:
                audience['name'] = replace_app_name(audience['name'])
                if audience['audience_size']:
                    audience['audience_size'] = human_format(
                        audience['audience_size'])
                    audience['name'] = '%s (%s users)' % (
                        audience['name'], audience['audience_size'])

            res['success'] = True
            res['data'] = tailored_audiences

        except Exception as e:
            res = {'data': {}, 'success': False, 'message': str(e)}

        if syncData and res['data'] and res['success']:
            res['sync'] = {}
            if isinstance(res['data'], (list, tuple)):
                sync_success = 0
                sync_fail = 0
                new_count = 0
                existing_count = 0
                for index, audience in enumerate(res['data'], start=0):
                    audience_res = self.sync_tailored_audience(
                        account_id_int, audience)
                    if 'success' in audience_res and audience_res[
                            'success'] is True:
                        if audience_res['type'] == 'existing':
                            existing_count += 1
                        if audience_res['type'] == 'new':
                            new_count += 1
                        sync_success += 1

                    elif 'success' in audience_res and audience_res[
                            'success'] is False:
                        sync_fail += 1

                res['sync']['type'] = {}
                res['sync']['type']['existing'] = existing_count
                res['sync']['type']['new'] = new_count
                res['sync']['total'] = sync_success
                if sync_fail == 0:
                    res['sync']['success'] = True
                else:
                    res['sync']['success'] = False

            elif isinstance(res['data'], dict):
                audience_res = self.sync_tailored_audience(
                    account_id_int, res['data'])

                if 'success' in audience_res and audience_res[
                        'success'] is True:
                    res['data'] = audience_res['data']
                    res['sync']['success'] = audience_res['success']
                    res['sync']['type'] = {}
                    res['sync']['type'][audience_res['type']] = 1
                    res['sync']['total'] = 1

                elif 'success' in audience_res and audience_res[
                        'success'] is False:
                    res['data'] = audience_res['data']
                    res['sync']['success'] = audience_res['success']
                    res['sync']['message'] = audience_res['message']
        return res
Exemplo n.º 6
0
    def create_update_line_item(self,
                                data,
                                oauth_token=settings.TW_ACCESS_TOKEN,
                                oauth_token_secret=settings.TW_ACCESS_SECRET,
                                request_type="post"):
        res = {}
        res['sync'] = {}
        account_id = data.get('account_id', None)
        campaign_id = data.get('campaign_id', None)
        line_item_id = data.get('line_item_id', None)
        campaign_id_int = None

        if isinstance(campaign_id, (int, long)):
            campaign_id_int = campaign_id
            campaign_id = int_to_base36(campaign_id)
            data['campaign_id'] = campaign_id

        if isinstance(line_item_id, (int, long)):
            line_item_id_int = line_item_id
            line_item_id = int_to_base36(line_item_id)

        if isinstance(account_id, (int, long)):
            account_id_int = account_id
            account_id = int_to_base36(account_id)

        if account_id is None:
            res = {
                'data': [],
                'success': False,
                'message': "Missing Twitter Account ID"
            }
            return res

        if request_type == 'post':
            if campaign_id is None:
                res = {
                    'data': [],
                    'success': False,
                    'message': "Missing Twitter Campaign ID"
                }
                return res

        if request_type == 'put':
            if line_item_id is None:
                res = {
                    'data': [],
                    'success': False,
                    'message': "Missing Twitter Line Item ID"
                }
                return res

        params = {}
        params['advertiser_domain'] = data.get('advertiser_domain', None)
        # automatically_set_bid and bid_type cannot be set in the same request
        # See https://dev.twitter.com/ads/reference/post/accounts/%3Aaccount_id/line_items#api-param-line-item-bid_type
        if data.get('automatically_select_bid', False) is True:
            params['automatically_select_bid'] = str(True).lower()
        else:
            params['bid_type'] = data.get('bid_type', None)
            params['bid_amount_local_micro'] = data.get(
                'bid_amount_local_micro', None)

        params['bid_amount_local_micro'] = data.get('bid_amount_local_micro',
                                                    None)
        params['bid_type'] = data.get('bid_type', None)
        params['bid_unit'] = data.get('bid_unit', None)
        params['campaign_id'] = data.get('campaign_id', None)
        params['categories'] = data.get('categories', None)
        params['charge_by'] = data.get('charge_by', None)
        params['end_time'] = data.get('end_time', None)
        params['include_sentiment'] = data.get('include_sentiment',
                                               'POSITIVE_ONLY')
        params['name'] = data.get('name', None)
        params['objective'] = data.get('objective', 'APP_INSTALLS')
        params['optimization'] = data.get('optimization', None)
        if data.get('paused', None) is not None:
            params['paused'] = 'true' if data.get('paused') else 'false'
        params['placements'] = data.get('placements', 'ALL_ON_TWITTER')
        params['product_type'] = data.get('product_type', 'PROMOTED_TWEETS')
        params['start_time'] = data.get('start_time', None)
        params['total_budget_amount_local_micro'] = data.get(
            'total_budget_amount_local_micro', None)

        # total_budget_amount_local_micro = 0 is not permitted
        if not params['total_budget_amount_local_micro']:
            params['total_budget_amount_local_micro'] = None

        params = dict((k, v) for k, v in params.iteritems()
                      if v is not None and v is not "")

        client = Client(settings.TW_CONSUMER_KEY, settings.TW_CONSUMER_SECRET,
                        oauth_token, oauth_token_secret)
        if settings.TW_SANDBOX:
            client.sandbox = settings.TW_SANDBOX

        try:
            account = client.accounts(account_id)

            if request_type == 'put':
                resource = '/{api_version}/accounts/{account_id}/line_items/{line_item_id}'.format(
                    api_version=settings.TW_API_VERSION,
                    account_id=account.id,
                    line_item_id=line_item_id)

            elif request_type == 'post':
                resource = '/{api_version}/accounts/{account_id}/line_items'.format(
                    api_version=settings.TW_API_VERSION, account_id=account.id)

            response = Request(client, request_type, resource,
                               params=params).perform()

            if response.code == 200 or response.code == 201:
                res['success'] = True

            res['data'] = response.body['data']

            if res['data'] and res['success']:
                if campaign_id_int is None and res['data']['campaign_id']:
                    campaign_id_int = base36_to_int(res['data']['campaign_id'])

                line_item_res = self.sync_line_item(account_id_int,
                                                    campaign_id_int,
                                                    res['data'])

                if 'success' in line_item_res and line_item_res[
                        'success'] is True:
                    res['data'] = line_item_res['data']
                    res['sync']['success'] = line_item_res['success']
                    res['sync']['type'] = {}
                    res['sync']['total'] = 1
                    res['sync']['type'][line_item_res['type']] = 1

                elif 'success' in line_item_res and line_item_res[
                        'success'] is False:
                    res['data'] = line_item_res['data']
                    res['sync']['success'] = line_item_res['success']
                    res['sync']['message'] = line_item_res['message']

        except Error as e:
            code = None
            if e.code:
                code = e.code
            elif e.details[0]['code']:
                code = e.details[0]['code']
            res = {
                'data': {},
                'success':
                False,
                'message':
                e.details[0]['message'] if e.details and e.details[0]
                and e.details[0]['message'] else '',
                'errors': {
                    str(code): True
                } if code else {}
            }
        except Exception as e:
            res = {'data': {}, 'success': False, 'message': str(e)}

        return res
Exemplo n.º 7
0
 def platform_versions(klass, account, **kwargs):
     """Returns a list of supported platform versions"""
     resource = klass.RESOURCE_OPTIONS + 'platform_versions'
     request = Request(account.client, 'get', resource, params=kwargs)
     return Cursor(None, request)
Exemplo n.º 8
0
    def create_update_promoted_tweet(
            self,
            data,
            oauth_token=settings.TW_ACCESS_TOKEN,
            oauth_token_secret=settings.TW_ACCESS_SECRET,
            request_type="post"):
        res = {}
        res['sync'] = {}
        account_id = data.get('account_id', None)
        line_item_id = data.get('line_item_id', None)
        tweet_ids = data.get('tweet_ids', None)
        promoted_tweet_id = data.get('promoted_tweet_id', None)
        line_item_id_int = None

        line_item_id_int = line_item_id
        line_item_id_base_36 = int_to_base36(line_item_id)
        account_id_int = account_id
        account_id_base36 = int_to_base36(account_id)

        if account_id is None:
            res = {
                'data': {},
                'success': False,
                'message': "Missing Twitter Account ID"
            }
            return res

        if request_type == 'put' or request_type == 'delete':
            if promoted_tweet_id is None:
                res = {
                    'data': {},
                    'success': False,
                    'message': "Missing Twitter Promoted Tweet ID"
                }
                return res

        if request_type == 'post':
            if tweet_ids is None:
                res = {
                    'data': {},
                    'success': False,
                    'message': "Missing Twitter Tweet IDs"
                }
                return res

            if line_item_id is None:
                res = {
                    'data': {},
                    'success': False,
                    'message': "Missing Twitter Line Item ID"
                }
                return res

        params = {}
        params['display_properties'] = data.get('display_properties', None)
        params['tweet_ids'] = data.get('tweet_ids', None)
        params['line_item_id'] = line_item_id_base_36
        params = dict((k, v) for k, v in params.iteritems()
                      if v is not None and v is not "")
        client = Client(settings.TW_CONSUMER_KEY, settings.TW_CONSUMER_SECRET,
                        oauth_token, oauth_token_secret)

        if settings.TW_SANDBOX:
            client.sandbox = settings.TW_SANDBOX

        try:
            account = client.accounts(account_id_base36)

            if request_type == 'put' or request_type == 'delete':
                resource = '/{api_version}/accounts/{account_id}/promoted_tweets/{promoted_tweet_id}'.format(
                    api_version=settings.TW_API_VERSION,
                    account_id=account.id,
                    promoted_tweet_id=promoted_tweet_id)

            elif request_type == 'post':
                resource = '/{api_version}/accounts/{account_id}/promoted_tweets'.format(
                    api_version=settings.TW_API_VERSION, account_id=account.id)
            response = Request(client, request_type, resource,
                               params=params).perform()
            if response.code == 200 or response.code == 201:
                res['success'] = True

            res['data'] = response.body['data']

            if res['data'] and res['success']:

                sync_success = 0
                sync_fail = 0
                new_count = 0
                existing_count = 0
                deleted_count = 0
                if request_type == 'delete':
                    res['data'] = [res['data']]
                for index, api_line_item_promoted_tweet in enumerate(
                        res['data'], start=0):
                    line_item_id_int = base36_to_int(
                        api_line_item_promoted_tweet['line_item_id'])
                    api_line_item_promoted_tweet['account_id'] = account_id

                    line_item_promoted_tweet_res = self.sync_promoted_tweet(
                        account_id_int, line_item_id_int,
                        api_line_item_promoted_tweet)

                    if 'success' in line_item_promoted_tweet_res and line_item_promoted_tweet_res[
                            'success'] is True:
                        if 'skip' in line_item_promoted_tweet_res:
                            continue

                        if line_item_promoted_tweet_res['type'] == 'existing':
                            existing_count += 1
                        if line_item_promoted_tweet_res['type'] == 'new':
                            new_count += 1
                        if line_item_promoted_tweet_res['type'] == 'delete':
                            deleted_count += 1
                        sync_success += 1
                    elif 'success' in line_item_promoted_tweet_res and line_item_promoted_tweet_res[
                            'success'] is False:
                        sync_fail += 1

                res['sync']['success'] = sync_fail == 0
                res['sync']['type'] = {}
                res['sync']['type']['existing'] = existing_count
                res['sync']['type']['new'] = new_count
                res['sync']['type']['delete'] = deleted_count

        except Error as e:
            code = None
            if e.code:
                code = e.code
            elif e.details[0]['code']:
                code = e.details[0]['code']
            res = {
                'data': {},
                'success':
                False,
                'message':
                e.details[0]['message'] if e.details and e.details[0]
                and e.details[0]['message'] else '',
                'errors': {
                    str(code): True
                } if code else {}
            }
        except Exception as e:
            res = {'data': {}, 'success': False, 'message': str(e)}

        return res
# Copyright (C) 2015 Twitter, Inc.

from twitter_ads.client import Client
from twitter_ads.http import Request

CONSUMER_KEY = 'your consumer key'
CONSUMER_SECRET = 'your consumer secret'
ACCESS_TOKEN = 'user access token'
ACCESS_TOKEN_SECRET = 'user access token secret'
ADS_ACCOUNT = 'ads account id'

# initialize the twitter ads api client
client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN,
                ACCESS_TOKEN_SECRET)

# using the Request object you can manually call any twitter API resource including media uploads.

# note: video uploads a bit more specialized and should be done using the
# twitter_ads.http.MediaUpload class.

# upload an image to POST media/upload
resource = '/1.1/media/upload.json'
domain = 'https://upload.twitter.com'
files = {'media': (None, open('/path/to/file.jpg', 'rb'))}
response = Request(client, 'post', resource, files=files,
                   domain=domain).perform()

# extract the media_id value from the response
media_id = response.body['media_id']
Exemplo n.º 10
0
 def all(klass, client, **kwargs):
     """Returns a Cursor instance for a given resource."""
     resource = klass.RESOURCE_COLLECTION
     request = Request(client, 'get', resource, params=kwargs)
     return Cursor(klass, request, init_with=[client])
Exemplo n.º 11
0
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''
ACCOUNT_ID = ''

# initialize the client
client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN,
                ACCESS_TOKEN_SECRET)

# load the advertiser account instance
account = client.accounts(ACCOUNT_ID)

# retrieve a Tweet
tweet_id = '973002610033610753'  # use one of your own Tweets
resource = '/1.1/statuses/show/{id}.json'.format(id=tweet_id)
domain = 'https://api.twitter.com'
params = {'include_card_uri': 'true'}
response = Request(client, 'get', resource, domain=domain,
                   params=params).perform()
card_uri = response.body['card_uri']  # Tweet must include a card_uri card

# fetch by card_uri
card = CardsFetch.load(account, card_uris=[card_uri]).first
print card
print card.card_type
print card.id

# fetch by card id
same_card = CardsFetch.load(account, card_id=card.id)
print same_card.card_type
print same_card.card_uri
Exemplo n.º 12
0
 def network_operators(klass, account, **kwargs):
     """Returns a list of supported network operators"""
     resource = klass.RESOURCE_OPTIONS + 'network_operators'
     request = Request(account.client, 'get', resource, params=kwargs)
     return Cursor(None, request)
Exemplo n.º 13
0
 def behavior_taxonomies(klass, account, **kwargs):
     """Returns a list of supported behavior taxonomies"""
     resource = klass.RESOURCE_OPTIONS + 'behavior_taxonomies'
     request = Request(account.client, 'get', resource, params=kwargs)
     return Cursor(None, request)
Exemplo n.º 14
0
 def __create_audience__(self, name):
     params = {'name': name}
     resource = self.RESOURCE_COLLECTION.format(account_id=self.account.id)
     response = Request(self.account.client, 'post', resource, params=params).perform()
     return self.from_response(response.body['data'])
Exemplo n.º 15
0
    def fetch_line_items(self,
                         data,
                         syncData=False,
                         oauth_token=settings.TW_ACCESS_TOKEN,
                         oauth_token_secret=settings.TW_ACCESS_SECRET):
        res = {}
        res['data'] = []
        res['success'] = False
        account_id = data.get('account_id')
        campaign_id = data.get('campaign_id')
        line_item_id = data.get('line_item_id')
        campaign_id_int = None

        if isinstance(account_id, (int, long)):
            account_id_int = account_id
            account_id = int_to_base36(account_id)

        if isinstance(campaign_id, (int, long)):
            campaign_id_int = campaign_id
            campaign_id = int_to_base36(campaign_id)

        if isinstance(line_item_id, (int, long)):
            line_item_id = int_to_base36(line_item_id)

        if account_id is None:
            res = {
                'data': {},
                'success': False,
                'message': "Missing Twitter Account ID"
            }
            return res

        client = Client(settings.TW_CONSUMER_KEY, settings.TW_CONSUMER_SECRET,
                        oauth_token, oauth_token_secret)

        if settings.TW_SANDBOX:
            client.sandbox = settings.TW_SANDBOX

        try:
            account = client.accounts(account_id)
            resource = '/{api_version}/accounts/{account_id}/line_items?with_deleted=true'.format(
                api_version=settings.TW_API_VERSION, account_id=account.id)
            if campaign_id is not None:
                resource = '/{api_version}/accounts/{account_id}/line_items?campaign_ids={campaign_id}&count=1000&with_deleted=true'.format(
                    api_version=settings.TW_API_VERSION,
                    account_id=account.id,
                    campaign_id=campaign_id)

            response = Request(client, 'get', resource).perform()

            if response.headers[
                    'x-rate-limit-remaining'] == "0" and settings.TW_RATE_LIMIT_ALERT:
                send_twitter_alert_email({
                    "account_id": account_id,
                    "endpoint": resource
                })

            res['data'] = response.body['data']

            next_cursor = None
            if response.body['next_cursor'] and response.body[
                    'next_cursor'] is not 0:
                next_cursor = response.body['next_cursor']
                while next_cursor is not 0:
                    resource = '/{api_version}/accounts/{account_id}/line_items?cursor={next_cursor}&count=1000&with_deleted=true'.format(
                        api_version=settings.TW_API_VERSION,
                        account_id=account.id,
                        next_cursor=next_cursor)
                    if campaign_id is not None:
                        resource = '/{api_version}/accounts/{account_id}/line_items?campaign_ids={campaign_id}&count=1000&cursor={next_cursor}&with_deleted=true'.format(
                            api_version=settings.TW_API_VERSION,
                            account_id=account.id,
                            campaign_id=campaign_id,
                            next_cursor=next_cursor)

                    response = Request(client, 'get', resource).perform()
                    next_cursor = response.body['next_cursor'] or 0
                    res['data'] += response.body['data']

            res['success'] = True

        except Error as e:
            code = None
            if e.code:
                code = e.code
            elif e.details[0]['code']:
                code = e.details[0]['code']
            res = {
                'data': {},
                'success':
                False,
                'message':
                e.details[0]['message'] if e.details and e.details[0]
                and e.details[0]['message'] else '',
                'errors': {
                    str(code): True
                } if code else {}
            }
        except Exception as e:
            res = {'data': {}, 'success': False, 'message': str(e)}

        if syncData and res['data'] and res['success']:
            res['sync'] = {}
            if isinstance(res['data'], (list, tuple)):
                sync_success = 0
                sync_fail = 0
                new_count = 0
                existing_count = 0
                for index, api_line_item in enumerate(res['data'], start=0):
                    if campaign_id_int is None:
                        campaign_id_int = base36_to_int(
                            api_line_item['campaign_id'])

                    line_item_res = self.sync_line_item(
                        account_id_int, campaign_id_int, api_line_item)
                    if 'success' in line_item_res and line_item_res[
                            'success'] is True:
                        if line_item_res['type'] == 'existing':
                            existing_count += 1
                        if line_item_res['type'] == 'new':
                            new_count += 1
                        sync_success += 1

                    elif 'success' in line_item_res and line_item_res[
                            'success'] is False:
                        sync_fail += 1
                res['sync']['type'] = {}
                res['sync']['type']['existing'] = existing_count
                res['sync']['type']['new'] = new_count
                res['sync']['total'] = sync_success
                if sync_fail == 0:
                    res['sync']['success'] = True
                else:
                    res['sync']['success'] = False

            elif isinstance(res['data'], dict):
                line_item_res = self.sync_line_item(account_id_int,
                                                    campaign_id_int,
                                                    res['data'])

                if 'success' in line_item_res and line_item_res[
                        'success'] is True:
                    res['data'] = line_item_res['data']
                    res['sync']['success'] = line_item_res['success']
                    res['sync']['type'] = {}
                    res['sync']['total'] = 1
                    res['sync']['type'][line_item_res['type']] = 1

                elif 'success' in line_item_res and line_item_res[
                        'success'] is False:
                    res['data'] = line_item_res['data']
                    res['sync']['success'] = line_item_res['success']
                    res['sync']['message'] = line_item_res['message']
        return res
# initialize the twitter ads api client
client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)

# load up the account instance
account = client.accounts(ADS_ACCOUNT)

# using the Request object you can manually request any
# twitter ads api resource that you want.

resource = '/0/accounts/{account_id}/features'.format(account_id=account.id)
params = {'feature_keys': 'AGE_TARGETING,CPI_CHARGING'}

# try, build and execute the request with error handling
try:
    response = Request(client, 'get', resource, params=params).perform()
    print(response.body['data'][0])
except Error as e:
    # see twitter_ads.error for more details
    print e.details
    raise

# you can also manually construct requests to be
# used in Cursor objects.

resource = '/0/targeting_criteria/locations'
params = {'location_type': 'CITY', 'q': 'port'}
request = Request(client, 'get', resource, params=params)
cursor = Cursor(None, request)

# execute requests and iterate cursor until exhausted
Exemplo n.º 17
0
    def get_line_item(self,
                      data,
                      oauth_token=settings.TW_ACCESS_TOKEN,
                      oauth_token_secret=settings.TW_ACCESS_SECRET):
        res = {}
        account_id = data.get('account_id')
        line_item_id = data.get('line_item_id')

        if isinstance(line_item_id, (int, long)):
            line_item_id = int_to_base36(line_item_id)

        if isinstance(account_id, (int, long)):
            account_id = int_to_base36(account_id)

        if account_id is None:
            res = {
                'data': {},
                'success': False,
                'message': "Missing Twitter Account ID"
            }
            return res

        if line_item_id is None:
            res = {
                'data': {},
                'success': False,
                'message': "Missing Twitter Line Item ID"
            }
            return res

        client = Client(settings.TW_CONSUMER_KEY, settings.TW_CONSUMER_SECRET,
                        oauth_token, oauth_token_secret)
        if settings.TW_SANDBOX:
            client.sandbox = settings.TW_SANDBOX
        try:
            account = client.accounts(account_id)
            resource = '/{api_version}/accounts/{account_id}/line_items/{line_item_id}?count=1000&with_deleted=true'.format(
                api_version=settings.TW_API_VERSION,
                account_id=account.id,
                line_item_id=line_item_id)
            response = Request(client, 'get', resource).perform()
            res['data'] = response.body['data']
            res['success'] = True
        except Error as e:
            code = None
            if e.code:
                code = e.code
            elif e.details[0]['code']:
                code = e.details[0]['code']
            res = {
                'data': {},
                'success':
                False,
                'message':
                e.details[0]['message'] if e.details and e.details[0]
                and e.details[0]['message'] else '',
                'errors': {
                    str(code): True
                } if code else {}
            }
        except Exception as e:
            res = {'data': {}, 'success': False, 'message': str(e)}
        return res
    def fetch_app_cards(self,
                        data,
                        syncData=False,
                        oauth_token=settings.TW_ACCESS_TOKEN,
                        oauth_secret=settings.TW_ACCESS_SECRET):
        res = {}
        account_id = data.get('account_id')

        if isinstance(account_id, (int, long)):
            account_id_int = account_id
            account_id = int_to_base36(account_id)
        else:
            account_id_int = base36_to_int(account_id)

        if account_id is None:
            res = {
                'data': {},
                'success': False,
                'message': "Missing Twitter Account ID"
            }
            return res

        client = Client(settings.TW_CONSUMER_KEY, settings.TW_CONSUMER_SECRET,
                        oauth_token, oauth_secret)
        if settings.TW_SANDBOX:
            client.sandbox = settings.TW_SANDBOX
        try:
            res['data'] = []
            account = client.accounts(account_id)
            resource = '/{api_version}/accounts/{account_id}/cards/app_download'.format(
                api_version=settings.TW_API_VERSION, account_id=account.id)
            response = Request(client, 'get', resource).perform()
            for card in response.body['data']:
                res['data'].append(card)

            resource = '/{api_version}/accounts/{account_id}/cards/image_app_download'.format(
                api_version=settings.TW_API_VERSION, account_id=account.id)
            response = Request(client, 'get', resource).perform()
            for card in response.body['data']:
                res['data'].append(card)

            resource = '/{api_version}/accounts/{account_id}/cards/video_app_download'.format(
                api_version=settings.TW_API_VERSION, account_id=account.id)
            response = Request(client, 'get', resource).perform()

            if response.headers[
                    'x-rate-limit-remaining'] == "0" and settings.TW_RATE_LIMIT_ALERT:
                send_twitter_alert_email({
                    "account_id": account_id,
                    "endpoint": resource
                })

            for card in response.body['data']:
                res['data'].append(card)
            res['success'] = True

        except Error as e:
            code = None
            if e.code:
                code = e.code
            elif e.details[0]['code']:
                code = e.details[0]['code']
            res = {
                'data': {},
                'success':
                False,
                'message':
                e.details[0]['message'] if e.details and e.details[0]
                and e.details[0]['message'] else '',
                'errors': {
                    str(code): True
                } if code else {}
            }
        except Exception as e:
            res = {'data': {}, 'success': False, 'message': str(e)}

        if syncData and res['data'] and res['success']:
            res['sync'] = {}
            if isinstance(res['data'], (list, tuple)):
                sync_success = 0
                sync_fail = 0
                new_count = 0
                existing_count = 0
                for index, api_app_card in enumerate(res['data'], start=0):
                    app_card_res = self.sync_app_card(account_id_int,
                                                      api_app_card)

                    if 'success' in app_card_res and app_card_res[
                            'success'] is True:
                        if app_card_res['type'] == 'existing':
                            existing_count += 1
                        if app_card_res['type'] == 'new':
                            new_count += 1
                        sync_success += 1
                    elif 'success' in app_card_res and app_card_res[
                            'success'] is False:
                        sync_fail += 1

                res['sync']['type'] = {}
                res['sync']['type']['existing'] = existing_count
                res['sync']['type']['new'] = new_count
                res['sync']['total'] = sync_success
                if sync_fail == 0:
                    res['sync']['success'] = True
                else:
                    res['sync']['success'] = False

            elif isinstance(res['data'], dict):
                app_card_res = self.sync_app_card(account_id_int, res['data'])
                if 'success' in app_card_res and app_card_res[
                        'success'] is True:
                    res['data'] = app_card_res['data']
                    res['sync']['success'] = app_card_res['success']
                    res['sync']['type'] = {}
                    res['sync']['total'] = 1
                    res['sync']['type'][app_card_res['type']] = 1
                    # sync_success

                if 'success' in app_card_res and app_card_res[
                        'success'] is False:
                    res['data'] = app_card_res['data']
                    res['sync']['success'] = app_card_res['success']
                    res['sync']['message'] = app_card_res['message']

        return res
Exemplo n.º 19
0
    def batch_create_update_line_item(
            self,
            data,
            account_id,
            oauth_token=settings.TW_ACCESS_TOKEN,
            oauth_token_secret=settings.TW_ACCESS_SECRET,
            request_type="post"):
        res = {}
        res['success'] = False
        campaign_id_int = None

        if isinstance(account_id, (int, long)):
            account_id_int = account_id
            account_id = int_to_base36(account_id)

        if account_id is None:
            res = {
                'data': [],
                'success': False,
                'message': "Missing Twitter Account ID"
            }
            return res

        if isinstance(data, (list, tuple)):
            post_data = []
            for line_item in data:
                line_item_data = {}
                params = {}
                params['bid_amount_local_micro'] = line_item.get(
                    'bid_amount_local_micro', None)
                params['bid_type'] = line_item.get('bid_type', None)
                params['bid_unit'] = line_item.get('bid_unit', None)
                params['campaign_id'] = line_item.get('campaign_id', None)
                params['categories'] = line_item.get('categories', None)
                params['charge_by'] = line_item.get('charge_by', None)
                params['end_time'] = line_item.get('end_time', None)
                params['include_sentiment'] = line_item.get(
                    'include_sentiment', None)
                params['line_item_id'] = line_item.get('line_item_id', None)
                params['name'] = line_item.get('name', None)
                params['objective'] = line_item.get('objective',
                                                    'APP_INSTALLS')
                params['primary_web_event_tag'] = line_item.get(
                    'primary_web_event_tag', None)
                params['optimization'] = line_item.get('optimization', None)
                params['paused'] = str(line_item.get(
                    'paused')).lower() if line_item.get('paused') else None
                params['placements'] = line_item.get('placements',
                                                     'ALL_ON_TWITTER')
                params['product_type'] = line_item.get('product_type',
                                                       'PROMOTED_TWEETS')
                params['start_time'] = line_item.get('start_time', None)
                params['total_budget_amount_local_micro'] = line_item.get(
                    'total_budget_amount_local_micro', None)

                # total_budget_amount_local_micro = 0 is not permitted
                if not params['total_budget_amount_local_micro']:
                    params['total_budget_amount_local_micro'] = None

                params = dict((k, v) for k, v in params.iteritems()
                              if v is not None and v is not "")

                if request_type == 'post':
                    line_item_data['operation_type'] = 'Create'
                if request_type == 'put':
                    line_item_data['operation_type'] = 'Update'
                line_item_data['params'] = params
                post_data.append(line_item_data)

            client = Client(settings.TW_CONSUMER_KEY,
                            settings.TW_CONSUMER_SECRET, oauth_token,
                            oauth_token_secret)

            if settings.TW_SANDBOX:
                client.sandbox = settings.TW_SANDBOX

            # Split up requests into batches of 20
            batch = []
            batches = []
            for x in range(0, len(post_data), 20):
                batch = post_data[x:x + 20]
                batches.append(batch)

            success_batch = []
            error_batch = []
            error_details = []
            success = False
            error = False

            for batch_post in batches:
                try:
                    account = client.accounts(account_id)
                    headers = {"Content-Type": "application/json"}
                    resource = '/{api_version}/batch/accounts/{account_id}/line_items'.format(
                        api_version=settings.TW_API_VERSION,
                        account_id=account.id)
                    response = Request(client,
                                       'post',
                                       resource,
                                       body=json.dumps(batch_post),
                                       headers=headers).perform()

                    if response.code == 200 or response.code == 201:
                        success = True
                        success_batch.extend(response.body['data'])

                except Error as e:
                    error = True
                    if e.response.body.get('operation_errors',
                                           None) is not None:
                        for err in e.response.body.get('operation_errors'):
                            if err:
                                if isinstance(err, dict):
                                    err = [err]
                                error_details.extend(err)
                    if e.response.body.get('errors', None) is not None:
                        for err in e.response.body.get('errors'):
                            if err:
                                if isinstance(err, dict):
                                    err = [err]
                                error_details.extend(err)

                    error_batch.extend(batch_post)

                except Exception as e:
                    res = {'data': [], 'success': False, 'message': str(e)}
                    error_batch.extend(batch_post)

            if success_batch and success:
                res['sync'] = {}

                if isinstance(success_batch, dict):
                    success_batch = [success_batch]

                sync_success = 0
                sync_fail = 0
                new_count = 0
                existing_count = 0

                for index, api_line_item in enumerate(success_batch, start=0):
                    #campaign_id could be different in line item bach
                    campaign_id_int = base36_to_int(
                        api_line_item['campaign_id'])
                    line_item_res = self.sync_line_item(
                        account_id_int, campaign_id_int, api_line_item)
                    if 'success' in line_item_res and line_item_res[
                            'success'] is True:
                        if line_item_res['type'] == 'existing':
                            existing_count += 1
                        if line_item_res['type'] == 'new':
                            new_count += 1
                        sync_success += 1

                    elif 'success' in line_item_res and line_item_res[
                            'success'] is False:
                        sync_fail += 1

                res['sync']['type'] = {}
                res['sync']['type']['existing'] = existing_count
                res['sync']['type']['new'] = new_count
                res['sync']['total'] = sync_success

                if sync_fail == 0:
                    res['sync']['success'] = True
                else:
                    res['sync']['success'] = False

            res['success'] = success
            res['count'] = {}
            res['count']['success'] = len(success_batch)
            res['count']['total'] = len(data)
            res['count']['error'] = len(error_batch)
            res['data'] = success_batch

            if error:
                res['success'] = False
                res['error'] = {}
                res['error']['data'] = error_batch
                res['error']['messages'] = filter(None, error_details)

            return res

        elif isinstance(data, dict):
            if request_type == 'post':
                return self.create(data, oauth_token, oauth_token_secret)
            if request_type == 'put':
                return self.update(data, oauth_token, oauth_token_secret)
    def post(self, request, *args, **kwargs):
        csv_file = request.data.get('file')
        tw_account_id = request.data.get('tw_account_id')
        list_type = request.data.get('list_type')
        audience_name = request.data.get('audience_name')

        # save uploaded file on /tmp folder
        full_path = "/tmp/%s" % csv_file.name
        fout = open(full_path, "wb+")
        for chunk in csv_file.chunks():
            fout.write(chunk)
        fout.close()

        # validate params
        if not csv_file or not tw_account_id or not list_type \
            or not audience_name:
            return Response(
                data=dict(msg="file or tw_account_id or list_type or" \
                    "audience_name is missing"),
                status=status.HTTP_400_BAD_REQUEST)

        # check tw_account_id and make Client
        try:
            tw_account = TwitterAccount.objects.get(
                tw_account_id=tw_account_id)
            oauth_token = tw_account.tw_twitter_user_id.oauth_token \
                or settings.TW_ACCESS_TOKEN
            oauth_token_secret = tw_account.tw_twitter_user_id.oauth_secret \
                or settings.TW_ACCESS_SECRET
            _key = 'twitter_tailored_audiences_%s' % tw_account_id
            client = Client(settings.TW_CONSUMER_KEY,
                            settings.TW_CONSUMER_SECRET, oauth_token,
                            oauth_token_secret)
            tw_account_id = int_to_base36(int(tw_account_id))
        except Exception as e:
            return Response(data=dict(msg='invalid tw_account_id',
                                      detail=str(e)),
                            status=status.HTTP_400_BAD_REQUEST)

        error_details = ''
        error = False
        try:
            # TON upload
            input_file_path = TONUpload(client, full_path).perform()
            # remove query string
            input_file_path = urlparse.urljoin(
                input_file_path,
                urlparse.urlparse(input_file_path).path)

            # Create a new placeholder audience with the POST accounts/:account_id/tailored_audiences endpoint.
            query_string = dict(name=audience_name, list_type=list_type)
            query_string = urllib.urlencode(query_string)

            resource = "/{api_version}/accounts/{account_id}/tailored_audiences?" \
                "{qs}".format(
                    api_version=settings.TW_API_VERSION,
                    account_id=tw_account_id,
                    qs=query_string)
            response = Request(client, 'post', resource).perform()

            # if success
            if response.code == 200 or response.code == 201:
                tailored_audience_id = response.body['data'].get('id')
                query_string = dict(tailored_audience_id=tailored_audience_id,
                                    operation='ADD',
                                    input_file_path=input_file_path)
                query_string = urllib.urlencode(query_string)

                # Change the audience by adding
                resource = "/{api_version}/accounts/{account_id}/" \
                "tailored_audience_changes?{query_string}".format(
                    api_version=settings.TW_API_VERSION,
                    account_id=tw_account_id,
                    query_string=query_string
                    )

                response = Request(client, 'post', resource).perform()
                redis_cache.delete(_key)
        except Error as e:
            error = True
            error_details = str(e)
        except Exception as e:
            error = True
            error_details = str(e)

        if error:
            return Response(data=dict(msg=error_details),
                            status=status.HTTP_400_BAD_REQUEST)

        return Response(data=dict(status='ok'))
Exemplo n.º 21
0
 def tv_shows(klass, account, **kwargs):
     """Returns a list of supported TV shows"""
     resource = klass.RESOURCE_OPTIONS + 'tv_shows'
     request = Request(account.client, 'get', resource, params=kwargs)
     return Cursor(None, request)
Exemplo n.º 22
0
    def load(klass, account, id, **kwargs):
        """Returns an object instance for a given resource."""
        resource = klass.RESOURCE.format(account_id=account.id, id=id)
        response = Request(account.client, 'get', resource, params=kwargs).perform()

        return klass(account).from_response(response.body['data'])
Exemplo n.º 23
0
 def app_store_categories(klass, account, **kwargs):
     """Returns a list of supported app store categories"""
     resource = klass.RESOURCE_OPTIONS + 'app_store_categories'
     request = Request(account.client, 'get', resource, params=kwargs)
     return Cursor(None, request)
Exemplo n.º 24
0
def home(request):
    Accounts = ['18ce559z5re', '18ce559wc04', '18ce557t3m2', '18ce552xv0y']
    for AccountID in Accounts:
        CONSUMER_KEY = 'HDoSqCGCB3myJNoMP5RziL97w'
        CONSUMER_SECRET = '1WUOTQZsbNxIyVFpjCfSK4NslZqGUsvroYF4UdPFhGIVA4QPsh'
        ACCESS_TOKEN = '786549565717491713-3Qd2klEkIroIHYZ7f78ACd36qutNVHx'
        ACCESS_TOKEN_SECRET = '0aFaWFTpgXnrnhUCB2yZyGbcs8LF48Q1LQJiUt1VSvadn'
        ACCOUNT_ID = AccountID

        # initialize the client
        client = Client(CONSUMER_KEY,
                        CONSUMER_SECRET,
                        ACCESS_TOKEN,
                        ACCESS_TOKEN_SECRET,
                        options={
                            'handle_rate_limit': True,
                            'retry_max': 3,
                            'retry_delay': 10,
                            'retry_on_status': [404, 500, 503]
                        })

        # load the advertiser account instance
        account = client.accounts(ACCOUNT_ID)
        # print(account.name)

        yesterday = ("2020-03-05") + ("T00:00:00+08:00")
        today = ("2020-03-07") + ("T00:00:00+08:00")

        SpendList = []
        DateList = []
        # iterate through campaigns
        for camp in account.campaigns():

            if camp.id:

                resource = f"/8/stats/accounts/" + AccountID + "/"  #Enter Account Id Here
                params = {
                    "entity": ENTITY.CAMPAIGN,
                    "entity_ids": camp.id,
                    "start_time": yesterday,
                    "end_time": today,
                    "granularity": GRANULARITY.TOTAL,
                    "metric_groups": METRIC_GROUP.BILLING,
                    "placement": PLACEMENT.ALL_ON_TWITTER
                }

                req = Request(client=client,
                              method="GET",
                              resource=resource,
                              params=params)

                response = req.perform()
                spend_in_micros_value = response.body["data"][0]["id_data"][0][
                    "metrics"]["billed_charge_local_micro"]

                if spend_in_micros_value != None:
                    spendAll = response.body["data"][0]["id_data"][0][
                        "metrics"]["billed_charge_local_micro"][0]

                    spend = round((spendAll / 1000000), 2)
                    SpendList.append(spend)
                else:
                    spend = 0
                    SpendList.append(spend)

                StartTime = response.body["request"]["params"]["start_time"]
                #           Subtraction one Day
                x = slice(10)
                startTime = (StartTime[x])
                start = datetime.strptime(startTime, "%Y-%m-%d")
                Start = start + timedelta(days=1)

                EndTime = response.body["request"]["params"]["end_time"]

                #           Subtraction one Day
                x = slice(10)
                endTime = (EndTime[x])
                end = datetime.strptime(endTime, "%Y-%m-%d")
                End = end - timedelta(days=0)

                DateList.append(Start)
                DateList.append(End)

            else:
                print("No Camp")

        total = 0
        for OneMonth in range(0, len(SpendList)):
            total = total + SpendList[OneMonth]
        account = (account.name)
        print("Total Spend= ", total)
        StartDate = (DateList[0])
        EndDate = (DateList[1])

        currentDate = datetime.today()
        print("Current Date= ", currentDate)

        DataFrame = DataSet(currentDate=currentDate,
                            account=account,
                            spend=total,
                            StartDate=StartDate,
                            EndDate=EndDate)
        DataFrame.save()
    return render(request, "index.html")