Commits

Julian Rosse committed f190473

facebook comments, likes etc

  • Participants
  • Parent commits 5fead39
  • Branches optional

Comments (0)

Files changed (1)

sleepy/facebookish.py

                   handle_exceptions=_exceptions,
                   cache_name="facebook_albums" )
 
+class User( object ):
+    def __init__( self,
+                  user ):
+        self.id = user[ "id" ]
+        self.name = user[ "name" ]
+
+    @property
+    def page( self ):
+        return "http://facebook.com/" + self.id
+
+class Comments( object ):
+    def __init__( self,
+                  d ):
+        self._data = d
+        self.count = d.get( "count",
+                            len( d[ "data" ] ))
+        self.comments = [ Comment( comment )
+                           for comment
+                           in d[ "data" ]]
+
+class Comment( PubUpMixin ):
+    def __init__( self,
+                  comment ):
+        self.id = comment[ "id" ]
+        self.message = comment[ "message" ]
+        self._published = comment[ "created_time" ]
+        self._updated = comment[ "created_time" ]
+        self.user = User( comment[ "from" ] )
+
 class Post( TipeMixin,
             PubUpMixin ):
     @lazy_property
                   d,
                   user=None,
                   use_cache=True ):
+        self._data = d
         self._published = d[ "created_time" ]
         self._updated = d[ "updated_time" ]
         self.message = d.get( "message" )
         self.caption = d.get( "caption" )
         self.classification = self._classification()
         self.video = self._video()
-        self._data = d
+        self.comments = self._comments()
+        self.actions = d.get( "actions" )
+        self.comment_link = self._comment_link()
+        self.like_link = self._like_link()
+        self.likes = self._likes()
+
+    def _likes( self ):
+        likes = ( self
+                   ._data
+                   .get( "likes" ))
+        if not likes:
+            return None
+        return likes[ "count" ]
+
+    def _like_link( self ):
+        if not self.actions:
+            return None
+        for action in self.actions:
+            if action[ "name" ] == "Like":
+                return action[ "link" ]
+
+    def _comment_link( self ):
+        if not self.actions:
+            return None
+        for action in self.actions:
+            if action[ "name" ] == "Comment":
+                return action[ "link" ]
 
     def _classification( self ):
         try:
         if self.tipe == "photo":
             return picture( self.object_id )
 
+    def _comments( self ):
+        comments = ( self
+                      ._data
+                      .get( "comments" ))
+        if not comments or not comments[ "count" ]:
+            return None
+        if comments[ "count" ] == len( comments.get( "data", [] )):
+            return Comments( comments )
+        return Comments( cacher( cache_name="facebook_comments",
+                                 key=self.id,
+                                 createfunc=lambda:
+                                             _service()
+                                              .get_connections( self.id,
+                                                                "comments" )))
+
     def _video( self ):
         if self.classification != "youtube":
             return None