Ejemplo n.º 1
0
    def transmogrify(self, item):
        self.seen_count += 1
        original_path_key = self.originalPathKey(*item.keys())[0]
        if not original_path_key:
            # not enough info
            raise NothingToDoHere
        path = self.get_path(item)
        original_paths = item[original_path_key]

        if isinstance(original_paths, basestring):
            original_paths = [
                original_paths,
            ]

        original_paths = [self._prepare_path(p) for p in original_paths]
        if self.assure_target_exists:
            # Bails out if can't retrieve object at item's current path:
            if self.get_object(item, raise_=False) is None:
                logger.warn("Ignoring item at %s - object not created" % path)
                raise NothingToDoHere

        if not path.startswith(self.portal_path):
            path = self.portal_path + "/" + path.lstrip("/")

        for original_path in original_paths:
            if original_path != path:
                self.redirector.add(original_path, path)

        self.changed_count += 1
        return item
    def transmogrify(self, item):
        self.seen_count += 1
        original_path_key = self.originalPathKey(*item.keys())[0]
        if not original_path_key:
            # not enough info
            raise NothingToDoHere
        path = self.get_path(item)
        original_paths = item[original_path_key]

        if isinstance(original_paths, basestring):
            original_paths = [original_paths, ]

        original_paths = [self._prepare_path(p) for p in original_paths]
        if self.assure_target_exists:
            # Bails out if can't retrieve object at item's current path:
            if self.get_object(item, raise_=False) is None:
                logger.warn("Ignoring item at %s - object not created" % path)
                raise NothingToDoHere

        if not path.startswith(self.portal_path):
            path = self.portal_path + "/" +  path.lstrip("/")

        for original_path in original_paths:
            if original_path != path:
                self.redirector.add(original_path, path)

        self.changed_count += 1
        return item
def get_remote_image(url, item, img_title="", pathkey="_path",
                     jsonmigrator = False):

    # FIXME: not making an extra call to get the real pathkey
    logger.info("""Fetching image %s for article %s """ % (url,
            item.get(pathkey, "")))
    # Strip plone view from URL:
    url, it_worked = _strip_view(url)
    # it won't work if the image url does not have a proper image
    if not it_worked:
        jsonmigrator = False
    if jsonmigrator:
        url += "/get_item"
    try:
        http = urllib.urlopen(url)
        image_data = http.read()
        if http.code > 399:
            # we can't  get the image here
            raise IOError
        real_url = http.url

    except Exception as error:
        logger.error("Could not retrieve image at %s: %s - skipping" %
                     (url, error))
        return None, None, []

    if jsonmigrator and real_url.endswith("/get_item"):
            real_url = real_url[:len("/get_item")]
    image_filename, post_parts = _get_filename(real_url)

    if jsonmigrator:
        try:
            image = json.loads(image_data)
        except ValueError:
            logger.warn("Could not retrieve image json contents at %s " % url)
            raise None, None, []

    else: # build object item for the pipeline
        image = {}
        image["_type"] = "Image"
        image["image"] = image_data

        image["creation_date"] = item.get("creation_date", None)
        image["modification_date"] = item.get("modification_date", None)
        image["_transitions"] = item.get("_transitions", "published")

        if not img_title:
            img_title = image_filename.split(".")[0].encode("utf-8")
        image["title"] = img_title

    image["_filename"] = image_filename
    return real_url, image, post_parts
 def transmogrify(self, item):
     if (not "image" in item):
         raise NothingToDoHere
     if isinstance(item["image"] , dict):
         data = item["image"]["data"]
     else:
         data = item["image"]
     try:
         data= StringIO(data)
         img = Image.open(data)
     except (TypeError, IOError) as error:
         # We caught it:  a fake image! :-)
         path = self.get_path(item)
         if item.get("_type", "") == "Image":
             logger.warn("Image at %s contains no image data - discarding"
                         % path)
             raise ThouShallNotPass
         logger.warn("Item at %s contains no image data - scrubbing"
                     " incorrecet data" % path)
         del item["image"]
     return item