示例#1
0
    def import_feed(self, feed_url, force=None, local=False):
        """Import feed.

        If feed is not seen before it will be created, otherwise
        just updated.

        :param feed_url: URL to the feed to import.
        :keyword force: Force import of feed even if it's been updated
            too recently.

        """
        feed_url = feed_url.strip()
        feed = None
        try:
            feed_obj = self.feed_model.objects.get(feed_url=feed_url)
        except self.feed_model.DoesNotExist:
            try:
                feed = self.parse_feed(feed_url)
            except socket.timeout:
                self.feed_model.objects.create(feed_url=feed_url, sort=0)
                raise exceptions.TimeoutError(
                        unicode(models.FEED_TIMEDOUT_ERROR_TEXT))
            except Exception:
                feed = {"status": 500}

            default_status = http.OK if local else http.NOT_FOUND

            status = feed.get("status", default_status)
            if status == http.NOT_FOUND:
                raise exceptions.FeedNotFoundError(
                        unicode(models.FEED_NOT_FOUND_ERROR_TEXT))
            if status not in models.ACCEPTED_STATUSES:
                raise exceptions.FeedCriticalError(
                        unicode(models.FEED_GENERIC_ERROR_TEXT),
                        status=status)

            # Feed can be local/fetched with a HTTP client.
            status = feed.get("status") or feed.get("status\n") or http.OK

            if status == http.FOUND or status == http.MOVED_PERMANENTLY:
                if feed_url != feed.href:
                    return self.import_feed(feed.href, force=force)

            feed_name = feed.channel.get("title", "(no title)").strip()
            feed_data = truncate_field_data(self.feed_model, {
                            "sort": 0,
                            "name": feed_name,
                            "description": feed.channel.get("description", ""),
            })
            feed_obj = self.feed_model.objects.update_or_create(
                                            feed_url=feed_url, **feed_data)

        if self.include_categories:
            feed_obj.categories.add(*self.get_categories(feed.channel))

        if self.update_on_import:
            feed_obj = self.update_feed(feed_obj, feed=feed, force=force)

        return feed_obj
示例#2
0
    def import_feed(self, feed_url, force=None, local=False):
        """Import feed.

        If feed is not seen before it will be created, otherwise
        just updated.

        :param feed_url: URL to the feed to import.
        :keyword force: Force import of feed even if it's been updated
            recently.
        """
        feed_url = feed_url.strip()
        feed = None
        try:
            feed_obj = self.feed_model.objects.get(feed_url=feed_url)
        except self.feed_model.DoesNotExist:
            try:
                feed = self.parse_feed(feed_url)
            except socket.timeout:
                self.feed_model.objects.create(feed_url=feed_url, sort=0)
                raise exceptions.TimeoutError(
                    unicode(models.FEED_TIMEDOUT_ERROR_TEXT))
            except Exception:
                feed = {"status": 500}

            default_status = http.OK if local else http.NOT_FOUND

            status = feed.get("status", default_status)
            if status == http.NOT_FOUND:
                raise exceptions.FeedNotFoundError(
                    unicode(models.FEED_NOT_FOUND_ERROR_TEXT))
            if status not in models.ACCEPTED_STATUSES:
                raise exceptions.FeedCriticalError(unicode(
                    models.FEED_GENERIC_ERROR_TEXT),
                                                   status=status)

            # Feed can be local/fetched with a HTTP client.
            status = feed.get("status") or feed.get("status\n") or http.OK

            if status == http.FOUND or status == http.MOVED_PERMANENTLY:
                if feed_url != feed.href:
                    return self.import_feed(feed.href, force=force)

            feed_name = feed.channel.get("title", "(no title)").strip()
            feed_data = truncate_field_data(
                self.feed_model, {
                    "sort": 0,
                    "name": feed_name,
                    "description": feed.channel.get("description", ""),
                })
            feed_obj = self.feed_model.objects.update_or_create(
                feed_url=feed_url, **feed_data)

        if self.include_categories:
            feed_obj.categories.add(*self.get_categories(feed.channel))

        if self.update_on_import:
            feed_obj = self.update_feed(feed_obj, feed=feed, force=force)

        return feed_obj
示例#3
0
 def update_or_create(self, feed_obj, **fields):
     """Update post with new values."""
     super_update = super(PostManager, self).update_or_create
     defaults = truncate_field_data(self.model, fields)
     try:
         return super_update(guid=defaults["guid"], feed=feed_obj,
                                      defaults=defaults)
     except MultipleObjectsReturned:
         self.filter(guid=defaults["guid"], feed=feed_obj).delete()
         super_update(guid=defaults["guid"], feed=feed_obj,
                      defaults=defaults)