开发者

Parse Facebook feed datetime in python?

开发者 https://www.devze.com 2022-12-15 02:24 出处:网络
I am reading a Facebook updates feed using the python library \'feedparser\'. I loop through the collection of entries in my Django templates, and disp开发者_如何学编程lay the results.
  1. I am reading a Facebook updates feed using the python library 'feedparser'.
  2. I loop through the collection of entries in my Django templates, and disp开发者_如何学编程lay the results.
  3. The updated field is returned in a big long string, of some format I am unfamiliar with.

    Tue, 01 Dec 2009 23:55:52 +0000

How can I...

A) Use a Django filter to clean the date time in the for loop on the template.

...or...

B) Parse the date and format the updated date in the view, esentially cleaning the date in the collection of entries before it is iterated over in the view.

NOTE: I have tried both approaches. Django's date filter does't recognize it, and the iso8601 library I tried to parse the string didn't either.

Anybody have any experience with this? Thanks for your help!

UPDATE:

Using the updated_parsed value from feedparser in a Django template didn't work so well. But a Django snippet of a filter for this very thing already exists!**

Django Snippet: http://www.djangosnippets.org/snippets/1595/


Use entries[i].updated_parsed instead of entries[i].updated, and feedparser will return a parsed 9-tuple for you. (Documentation)

Then build a datetime object and pass it to Django or format to a string by yourself.

There is a similar question here.


This worked but wasn't what my final solution ended up becoming.

This solution iterates over the feed entries collection I get back from Facebook. I then parse the datetime and set the updated property to that new datetime. (Also, ignoring the +0000)

for entry in feed.entries:
        entry.updated = datetime.strptime(entry.updated, "%a, %d %b %Y %H:%M:%S +0000")

The entries collection is returned to the template which I can now use the Django 'date' filter to format the date.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号