improve feed parsing and display of microblogs
authorNicolas Chauvat <nicolas.chauvat@logilab.fr>
Tue, 31 Aug 2010 14:51:59 +0200
changeset 227 3f6c3b5c133f
parent 226 411f1f56c998
child 228 3dc7e3415005
improve feed parsing and display of microblogs
data/cubes.blog.css
entities.py
sobjects.py
views/primary.py
--- a/data/cubes.blog.css	Thu Aug 12 19:17:48 2010 +0200
+++ b/data/cubes.blog.css	Tue Aug 31 14:51:59 2010 +0200
@@ -97,4 +97,8 @@
 div.microblog span.meta {
   color: #999;
   display: block;
+}
+
+div.microblog span.meta a {
+  color: #999;
 }
\ No newline at end of file
--- a/entities.py	Thu Aug 12 19:17:48 2010 +0200
+++ b/entities.py	Tue Aug 31 14:51:59 2010 +0200
@@ -89,7 +89,7 @@
         return self.entity.content
 
     def isioc_container(self):
-        return self.entity.parent()
+        return self.entity.entry_of
 
     def isioc_type(self):
         return 'BlogPost'
--- a/sobjects.py	Thu Aug 12 19:17:48 2010 +0200
+++ b/sobjects.py	Tue Aug 31 14:51:59 2010 +0200
@@ -126,15 +126,41 @@
     for entry in feed.entries:
         item = {}
         item['uri'] = entry.id
-        item['content'] = entry.description.split(':',1)[1][:140]
+        # fix weird parsing
+        if hasattr(entry, 'content'):
+            content = entry.content[0].value
+            mimetype = entry.content[0].type
+        else:
+            content = entry.description
+            mimetype = u'text/plain'
+        if ': ' in content:
+            author, text = content.split(': ', 1)
+            if ' ' not in author:
+                content = text
+        item['content'] = content
+        item['content_format'] = format_map.get(mimetype, u'text/plain')
         item['creation_date'] = datetime(*entry.date_parsed[:6])
         item['modification_date'] = datetime(*entry.date_parsed[:6])
         item['author'] = feed.channel.link # true for twitter
         item['cwuri'] = feed.channel.link
-        screen_name = feed.channel.link.split('/')[-1]
-        item['avatar'] = get_twitter_avatar(screen_name)
+        for link in entry.links:
+            if link.type.startswith('image/') and link.rel == 'image':
+                item['avatar'] = link.href
+                break
+        else:
+            screen_name = feed.channel.link.split('/')[-1]
+            item['avatar'] = get_twitter_avatar(screen_name)
         yield item
 
+def search_twitter(word):
+    import urllib2
+    from simplejson import loads
+    data = urllib2.urlopen('http://search.twitter.com/search.json?q=%s&rpp=100' % word).read()
+    results = loads(data)
+    # process results
+    # print results
+    return []
+
 AVATAR_CACHE = {}
 
 def get_twitter_avatar(screen_name):
--- a/views/primary.py	Thu Aug 12 19:17:48 2010 +0200
+++ b/views/primary.py	Tue Aug 31 14:51:59 2010 +0200
@@ -121,19 +121,22 @@
                                                          xml_escape(imgurl))
     else:
         ablock = entity.has_creator[0].view('outofcontext')
-    words = []
-    for word in entity.content.split():
-        if word.startswith('http://'):
-            word = u'<a href="%s">%s</a>' % (word, word)
-        else:
-            word = xml_escape(word)
-        words.append(word)
-    content = u' '.join(words)
+    if entity.content_format == 'text/html':
+        content = entity.content
+    else:
+        words = []
+        for word in entity.content.split():
+            if word.startswith('http://'):
+                word = u'<a href="%s">%s</a>' % (word, word)
+            else:
+                word = xml_escape(word)
+            words.append(word)
+        content = u' '.join(words)
     return (u'<div class="microblog">'
             u'<span class="author">%s</span>'
             u'<span class="msgtxt">%s</span>'
-            u'<span class="meta">%s</span>'
-            u'</div>' % (ablock, content, entity.creation_date))
+            u'<span class="meta"><a href="%s">%s</a></span>'
+            u'</div>' % (ablock, content, entity.absolute_url(), entity.creation_date))
 
 class MicroBlogEntryPrimaryView(primary.PrimaryView):
     __select__ = primary.PrimaryView.__select__ & is_instance('MicroBlogEntry')