Пример #1
0
class CreateAds(Update):

    # Arg 1 is the account id
    # Arg 2 is a dict of new_ads with each sub-dict containing an adgroup_id, criterion_id (keyword id) and new_bid

    def __init__(self, account_id, new_ads):
        self.account_id = account_id
        self.new_ads = new_ads
        self.service_type = "AdGroupAdService"
        self.operations = self.getOperations()
        self.update = Update(self.account_id, self.service_type, self.operations)
        self.sendRequest()

    def getOperations(self):
        # TODO Chunk into 2000 or fewer changes

        operations = [
            {
                'operator': 'ADD',
                'operand': {
                    'xsi_type': 'AdGroupAd',
                    'adGroupId': self.new_ads[unique_id]["ad_group_id"],
                    'ad': {
                        'xsi_type': 'ExpandedTextAd',
                        'headlinePart1': self.new_ads[unique_id]["headlinePart1"],
                        'headlinePart2': self.new_ads[unique_id]["headlinePart2"],
                        'headlinePart3': self.new_ads[unique_id]["headlinePart3"],
                        'description': self.new_ads[unique_id]["description"],
                        'description2': self.new_ads[unique_id]["description2"],
                        'path1': self.new_ads[unique_id]["path1"],
                        'path2': self.new_ads[unique_id]["path2"],
                        'finalUrls': self.new_ads[unique_id]["finalUrls"],
                    },
                    # Optional fields.
                    'status': 'ENABLED'
                }
            } for unique_id in self.new_ads]

        return operations

    def checkOperations(self):
        # keyword specific checks here

        pass

    def convertToMicros(self, bid):
        return int(bid * 10) * 100000

    def sendRequest(self):
        keyword_response = self.update.send_request()
class UpdateKeywordBids(Update):

    # Arg 1 is the account id
    # Arg 2 is a dict of bid_updates with each sub-dict containing an adgroup_id, criterion_id (keyword id) and new_bid

    def __init__(self, account_id, bid_updates):
        self.account_id = account_id
        self.bid_updates = bid_updates
        self.service_type = "AdGroupCriterionService"
        self.operations = self.getOperations()
        self.update = Update(self.account_id, self.service_type,
                             self.operations)
        self.sendRequest()

    def getOperations(self):
        operations = [{
            'operator': 'SET',
            'operand': {
                'xsi_type': 'BiddableAdGroupCriterion',
                'adGroupId': self.bid_updates[unique_id]["ad_group_id"],
                'criterion': {
                    'id': self.bid_updates[unique_id]["criterion_id"],
                },
                'biddingStrategyConfiguration': {
                    'bids': [{
                        'xsi_type': 'CpcBid',
                        'bid': {
                            'microAmount':
                            self.convertToMicros(
                                self.bid_updates[unique_id]["bid"])
                        }
                    }]
                }
            }
        } for unique_id in self.bid_updates]

        return operations

    def checkOperations(self):
        # keyword specific checks here

        pass

    def convertToMicros(self, bid):
        return int(bid * 10) * 100000

    def sendRequest(self):
        keyword_response = self.update.send_request()