为什么我不能从rss feed获取日期并将其设置为我的Django模型?

时间:2022-09-24 23:13:08

I am using feed parser to create content from an rss feed. Its something like this:

我正在使用feed解析器从rss feed创建内容。它是这样的:

import feedparser

def parse_rss(rss_url):
    return feedparser.parse(rss_url)

def generate_content_from_feed(feed):
    parsed_feed = parse_rss(feed.rss_url)

    for item in parsed_feed['items']:
        if not Content.objects.filter(link=item['link']).exists():
            content = Content.objects.create(
                title=item['title'],
                link=item['link'],
                description=item['description'],
                pub_date=item['published'],
                category=item['category'],
                feed=feed,
            )
            if item['enclosure']:
                content.media_url = item['enclosure']['url']
                content.media_type = item['enclosure']['type']
            content.save()

Now I am not entirely sure if the above code is working or not, as I can't test it.

现在我不完全确定上面的代码是否有效,因为我无法测试它。

In my models.py, I have these two models :

在我的models.py中,我有这两个模型:

class Feed(models.Model):
    rss_url = models.URLField()

    def save(self, *args, **kwargs):
        super(Feed, self).save(*args, **kwargs)
        generate_content_from_feed(self) # Generating the content

class Content(models.Model):
    title = models.CharField(max_length=500)
    link = models.URLField()
    description = models.TextField()
    pub_date = models.DateTimeField(default=None)
    category = models.CharField(max_length=500, blank=True)
    media_url = models.URLField(blank=True) # Attached media file url
    media_type = models.CharField(max_length=50, blank=True)
    feed = models.ForeignKey(Feed, related_name='content_feed')

In case you are wondering, when a feed is saved, the content from that feed is generated and saved as Content objects in my database. Atleast thats what I am trying to do. However, when I save a feed, it gives an error saying something like this:

如果您想知道,在保存订阅源时,将生成该订阅源中的内容并将其另存为数据库中的内容对象。至少这就是我想做的事情。但是,当我保存一个Feed时,会出现错误,如下所示:

ValidationError at /admin/myapp/feed/add/
[u"'Fri, 08 Apr 2016 14:51:02 +0000' value has an invalid format. It   must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

How do I fix this problem? And also, I am no expert, could anybody tell me if my generate_content_from_feed method has issues or not? Thanks a lot.

我该如何解决这个问题?而且,我不是专家,任何人都可以告诉我,如果我的generate_content_from_feed方法有问题吗?非常感谢。

1 个解决方案

#1


0  

There may be a better way but your code should look something like this

可能有更好的方法,但您的代码看起来应该是这样的

a = 'Fri, 08 A`enter code here`pr 2016 14:51:02 +0000'

dates = re.search(r'(\w+), (\d+) (\w+) (\d{4}) (\d+):(\d+):(\d+) ([\w+]+)', a)
# YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

day_str = dates.group(1)
day = dates.group(2)
month_str = dates.group(3)
year = dates.group(4)
hour = dates.group(5)
minute = dates.group(6)
second = dates.group(7)

new_date = "%s-%s-%s %s:%s:%s" % (year, month_str, day, hour, minute, second)
print(new_date)

>>> 2016-Apr-08 14:51:02

If you have problems again, its probably good trying to convert the Apr to a date number

如果您再次遇到问题,可能会尝试将Apr转换为日期编号

#1


0  

There may be a better way but your code should look something like this

可能有更好的方法,但您的代码看起来应该是这样的

a = 'Fri, 08 A`enter code here`pr 2016 14:51:02 +0000'

dates = re.search(r'(\w+), (\d+) (\w+) (\d{4}) (\d+):(\d+):(\d+) ([\w+]+)', a)
# YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

day_str = dates.group(1)
day = dates.group(2)
month_str = dates.group(3)
year = dates.group(4)
hour = dates.group(5)
minute = dates.group(6)
second = dates.group(7)

new_date = "%s-%s-%s %s:%s:%s" % (year, month_str, day, hour, minute, second)
print(new_date)

>>> 2016-Apr-08 14:51:02

If you have problems again, its probably good trying to convert the Apr to a date number

如果您再次遇到问题,可能会尝试将Apr转换为日期编号