def save(self, channel):
        """
        Return a Post model class instance.

        :param channel: models.Channel instance to collect messages for.
        :type item: int
        """
        from socialfeedsparser.models import Post

        try:
            sau = Post.objects.get(source_uid=self.uid, channel=channel)
        except Post.DoesNotExist:
            sau = Post(
                source_uid=self.uid,
                channel=channel,
                author=self.author,
                author_uid=self.author_uid,
                content=self.content,
                date=self.date,
                link=self.link,
            )
            if self.image:
                base_file_name = os.path.basename(self.image)
                file_name = hashlib.sha224(base_file_name).hexdigest()[:50]
                downloaded = urllib2.urlopen(self.image).read()
                image_file = ContentFile(downloaded, name=file_name)
                sau.image.save(file_name, image_file)

            sau.save()
        return sau
    def save(self, channel):
        """
        Return a Post model class instance.

        :param channel: models.Channel instance to collect messages for.
        :type item: int
        """
        from socialfeedsparser.models import Post
        from socialfeedsparser.enums import Language

        try:
            sau = Post.objects.get(
                source_uid=self.uid, channel=channel)
        except Post.DoesNotExist:
            if getattr(settings, 'USE_TZ', False):
                date = timezone.make_aware(self.date, timezone.get_current_timezone())
            else:
                date = self.date

            # parse language
            list_languages = [l[0] for l in Language.choices]
            detected_language = detect(self.content)
            if detected_language not in list_languages:
                self.language = None
            else:
                self.language = detected_language

            sau = Post(
                source_uid=self.uid,
                channel=channel,
                author=self.author,
                author_uid=self.author_uid,
                avatar=self.avatar,
                content=self.content,
                video=self.video,
                date=date,
                link=self.link,
                repost=self.content.startswith('RT'),
                language=self.language,
            )

            if self.avatar:
                base_file_name = os.path.basename(self.avatar)
                file_name = hashlib.sha224(base_file_name).hexdigest()[:50]
                downloaded = urllib2.urlopen(self.avatar).read()
                image_file = ContentFile(downloaded, name=file_name)
                sau.avatar.save(file_name, image_file)

            if self.image:
                base_file_name = os.path.basename(self.image)
                file_name = hashlib.sha224(base_file_name).hexdigest()[:50]
                downloaded = urllib2.urlopen(self.image).read()
                image_file = ContentFile(downloaded, name=file_name)
                sau.image.save(file_name, image_file)

            sau.save()
        return sau
    def save(self, channel):
        """
        Return a Post model class instance.

        :param channel: models.Channel instance to collect messages for.
        :type item: int
        """
        from socialfeedsparser.models import Post
        from socialfeedsparser.enums import Language

        try:
            sau = Post.objects.get(source_uid=self.uid, channel=channel)
        except Post.DoesNotExist:
            if getattr(settings, 'USE_TZ', False):
                date = timezone.make_aware(self.date,
                                           timezone.get_current_timezone())
            else:
                date = self.date

            # parse language
            list_languages = [l[0] for l in Language.choices]
            detected_language = detect(self.content)
            if detected_language not in list_languages:
                self.language = None
            else:
                self.language = detected_language

            sau = Post(
                source_uid=self.uid,
                channel=channel,
                author=self.author,
                author_uid=self.author_uid,
                avatar=self.avatar,
                content=self.content,
                video=self.video,
                date=date,
                link=self.link,
                repost=self.content.startswith('RT'),
                language=self.language,
            )

            if self.avatar:
                base_file_name = os.path.basename(self.avatar)
                file_name = hashlib.sha224(base_file_name).hexdigest()[:50]
                downloaded = urllib2.urlopen(self.avatar).read()
                image_file = ContentFile(downloaded, name=file_name)
                sau.avatar.save(file_name, image_file)

            if self.image:
                base_file_name = os.path.basename(self.image)
                file_name = hashlib.sha224(base_file_name).hexdigest()[:50]
                downloaded = urllib2.urlopen(self.image).read()
                image_file = ContentFile(downloaded, name=file_name)
                sau.image.save(file_name, image_file)

            sau.save()
        return sau
    def save(self, channel):
        """
        Return a Post model class instance.

        :param channel: models.Channel instance to collect messages for.
        :type item: int
        """
        from socialfeedsparser.models import Post
        import warnings

        try:
            sau = Post.objects.get(source_uid=self.uid, channel=channel)
        except Post.DoesNotExist:
            sau = Post(
                source_uid=self.uid,
                channel=channel,
                author=self.author[:49],
                author_uid=self.author_uid,
                content=self.content,
                date=self.date,
                link=self.link,
            )

            with warnings.catch_warnings():  # ignoring db warnings, dirty
                warnings.simplefilter("ignore")
                if self.image:
                    base_file_name = os.path.basename(self.image)
                    try:
                        file_name = hashlib.sha224(base_file_name).hexdigest()[:50]
                    except (TypeError):
                        file_name = hashlib.sha224(base_file_name.encode("utf-8")).hexdigest()[:50]
                    downloaded = urlopen(self.image).read()
                    image_file = ContentFile(downloaded, name=file_name)
                    sau.image.save(file_name, image_file)
                sau.save()
        except IntegrityError:
            pass
        return sau
Beispiel #5
0
    def save(self, channel):
        """
        Return a Post model class instance.

        :param channel: models.Channel instance to collect messages for.
        :type item: int
        """
        from socialfeedsparser.models import Post
        import warnings

        try:
            sau = Post.objects.get(source_uid=self.uid, channel=channel)
        except Post.DoesNotExist:
            sau = Post(source_uid=self.uid,
                       channel=channel,
                       author=self.author[:49],
                       author_uid=self.author_uid,
                       content=self.content,
                       date=self.date,
                       link=self.link)

            with warnings.catch_warnings():  # ignoring db warnings, dirty
                warnings.simplefilter("ignore")
                if self.image:
                    base_file_name = os.path.basename(self.image)
                    try:
                        file_name = hashlib.sha224(
                            base_file_name).hexdigest()[:50]
                    except (TypeError):
                        file_name = hashlib.sha224(
                            base_file_name.encode('utf-8')).hexdigest()[:50]
                    downloaded = urlopen(self.image).read()
                    image_file = ContentFile(downloaded, name=file_name)
                    sau.image.save(file_name, image_file)
                sau.save()
        except IntegrityError:
            pass
        return sau