Пример #1
0
    def get(self, feed_item, required=False):
        """Retrieves an item.

    Items could be retrieved from a in memory cache in case it has already been
    retrieved within the current execution. Also, this method is capable of
    translating 'ext' placeholder IDs with concrete CM ids.

    Args:
      feed_item: Feed item from the Bulkdozer feed representing the item to
        retrieve.

    Returns:
      The CM object that represents the identified entity.
    """
        result = None
        keys = []
        id_value = feed_item.get(self._id_field, None)

        if not id_value and self._search_field and feed_item.get(
                self._search_field, None):
            store_key = feed_item[self._search_field]

            if self._parent_filter_name:
                if feed_item.get(self._parent_filter_field_name, None):
                    store_key = str(
                        feed_item.get(self._parent_filter_field_name,
                                      None)) + store_key

            result = store.get(self._entity, store_key)

            if not result:
                result, key = self._get_by_name(feed_item)
                keys.append(key)

            if not result and required:
                raise Exception('ERROR: Could not find %s with name %s' %
                                (self._entity, feed_item[self._search_field]))
        elif id_value:
            if type(id_value) in (str, unicode) and id_value.startswith('ext'):
                keys.append(id_value)
                id_value = store.translate(self._entity, id_value)

                if id_value:
                    feed_item[self._id_field] = id_value

            if id_value:
                keys.append(id_value)
                result = store.get(self._entity, id_value)

                if not result:
                    result = self._get(feed_item)

                if not result and required:
                    raise Exception('ERROR: Could not find %s with id %s' %
                                    (self._entity, id_value))

        store.set(self._entity, keys, result)

        return result
Пример #2
0
    def _wait_creative_activation(self, creative_id, timeout=128):
        """Waits for a creative to become active.

    This function checks the if the creative is active in intervals that
    increase exponentially (exponential backoff).

    Args:
      creative_id: Creative identifier.
      timeout: Optional parameter, determines how many seconds to wait for the
        activation.

    Raises:
      Exception: In case the creative doesn't activate within the specified
      timeout

    """
        # Only wait for creative activation if it is a new creative trafficked by
        # this Bulkdozer session
        if store.get('CREATIVE', creative_id):
            creative = self._retry(self.service.creatives().get(
                profileId=self.profile_id, id=creative_id))
            wait = 2

            while not creative['active'] and timeout > 0:
                time.sleep(wait)
                timeout -= wait
                wait *= 2
                creative = self._retry(self.service.creatives().get(
                    profileId=self.profile_id, id=creative_id))

            if not creative['active']:
                raise Exception(
                    'Creative %s failed to activate within defined timeout' %
                    creative['id'])
Пример #3
0
    def _get(self, feed_item):
        """Retrieves an item from DCM or the local cache.

    Args:
      feed_item: The feed item representing the creative asset from the
        Bulkdozer feed.

    Returns:
      Instance of the DCM object either from the API or from the local cache.
    """
        result = store.get(self._entity,
                           feed_item.get(FieldMap.CREATIVE_ASSET_ID, None))

        if not result:
            result = {
                'id': feed_item.get(FieldMap.CREATIVE_ASSET_ID, None),
                'assetIdentifier': {
                    'name': feed_item.get(FieldMap.CREATIVE_ASSET_NAME, None),
                    'type': feed_item.get(FieldMap.CREATIVE_TYPE, None)
                }
            }

            store.set(self._entity,
                      [feed_item.get(FieldMap.CREATIVE_ASSET_ID, None)],
                      result)

        return result