home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / plugins / myspace / objects.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  14.2 KB  |  412 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import weakref
  5. import operator
  6. import traceback
  7. import logging
  8. import simplejson as json
  9. import time
  10. import calendar
  11. import feedparser
  12. import datetime
  13. import constants
  14. import common
  15. import util
  16. import util.htmlutils as htmlutils
  17. TIMEZONE_FIX = 0
  18. log = logging.getLogger('myspace.new.objects')
  19.  
  20. class InputType(object):
  21.     JSON = 'json'
  22.     XML = 'xml'
  23.     ATOM = 'atom'
  24.  
  25.  
  26. class MyspaceObject(object):
  27.     
  28.     def __init__(self):
  29.         pass
  30.  
  31.     
  32.     def populate(self, x, input_type):
  33.         if input_type == InputType.JSON:
  34.             self.from_json(x)
  35.         elif input_type == InputType.XML:
  36.             self.from_xml(x)
  37.         elif input_type == InputType.ATOM:
  38.             self.from_atom(x)
  39.         else:
  40.             raise NotImplementedError
  41.         return (input_type == InputType.JSON).__dict__
  42.  
  43.     
  44.     def from_json(self, x):
  45.         raise NotImplementedError
  46.  
  47.     
  48.     def from_xml(self, x):
  49.         raise NotImplementedError
  50.  
  51.     
  52.     def __repr__(self):
  53.         return '<%s %s>' % (type(self).__name__, ' '.join((lambda .0: for i in .0:
  54. '%s=%r' % i)(sorted(vars(self).items()))))
  55.  
  56.     
  57.     def __getstate__(self):
  58.         d = self.__dict__.copy()
  59.         d.pop('_user', None)
  60.         d.pop('acct', None)
  61.         d.pop('icon', None)
  62.         return json.dumps(d, default = (lambda o: o.__dict__))
  63.  
  64.     
  65.     def __setstate__(self, x):
  66.         
  67.         def _object_hook(d):
  68.             o = MyspaceObject()
  69.             o.__dict__.update(d)
  70.             return o.__dict__
  71.  
  72.         self.__dict__.update(json.loads(x, object_hook = _object_hook))
  73.  
  74.     
  75.     def __eq__(self, other):
  76.         return self.id == getattr(other, 'id', object())
  77.  
  78.     
  79.     def __ne__(self, other):
  80.         return not self.__eq__(other)
  81.  
  82.  
  83.  
  84. class CommentableMyspaceObject(MyspaceObject):
  85.     
  86.     def __init__(self):
  87.         MyspaceObject.__init__(self)
  88.         self.comments = []
  89.         self.commentsRetrievedDate = 0
  90.  
  91.     
  92.     def user_likes(self, userid):
  93.         if not self.comments:
  94.             return False
  95.         
  96.         try:
  97.             int(userid)
  98.         except ValueError:
  99.             self.comments
  100.             self.comments
  101.         except:
  102.             self.comments
  103.  
  104.         userid = 'myspace.com.person.%s' % userid
  105.         for comment in self.comments:
  106.             if comment.is_like and comment.userid == userid:
  107.                 return True
  108.         
  109.         return False
  110.  
  111.     
  112.     def user_dislikes(self, userid):
  113.         if not self.comments:
  114.             return False
  115.         
  116.         try:
  117.             int(userid)
  118.         except ValueError:
  119.             self.comments
  120.             self.comments
  121.         except:
  122.             self.comments
  123.  
  124.         userid = 'myspace.com.person.%s' % userid
  125.         for comment in self.comments:
  126.             if comment.is_dislike and comment.userid == userid:
  127.                 return True
  128.         
  129.         return False
  130.  
  131.     
  132.     def get_likers(self):
  133.         if self.likable:
  134.             return set((lambda .0: for x in .0:
  135. if x.is_like:
  136. x.useridcontinue)(self.comments))
  137.         return []
  138.  
  139.     
  140.     def get_dislikers(self):
  141.         if self.dislikable:
  142.             return set((lambda .0: for x in .0:
  143. if x.is_dislike:
  144. x.useridcontinue)(self.comments))
  145.         return []
  146.  
  147.     
  148.     def get_comments(self):
  149.         return [](_[1], key = (lambda x: x.postedDate_parsed))
  150.  
  151.     
  152.     def likable(self):
  153.         if self.supports_comments:
  154.             pass
  155.         return common.pref('myspace.like_enabled', type = bool, default = True)
  156.  
  157.     likable = property(likable)
  158.     
  159.     def dislikable(self):
  160.         if self.supports_comments:
  161.             pass
  162.         return common.pref('myspace.dislike_enabled', type = bool, default = True)
  163.  
  164.     dislikable = property(dislikable)
  165.  
  166.  
  167. class ActivitySource(MyspaceObject):
  168.     
  169.     def from_xml(self, x):
  170.         self.title = unicode(x.title)
  171.         id_tag = getattr(x, 'id', None)
  172.         if id_tag is None:
  173.             self.id = None
  174.         else:
  175.             self.id = unicode(id_tag)
  176.         link_tag = getattr(x, 'link', None)
  177.         if link_tag is None:
  178.             self.url = None
  179.         else:
  180.             self.url = link_tag.attrib.get('href')
  181.  
  182.     
  183.     def __eq__(self, other):
  184.         return self.url == getattr(other, 'url', object())
  185.  
  186.  
  187.  
  188. class AtomItem(CommentableMyspaceObject):
  189.     
  190.     def __init__(self):
  191.         CommentableMyspaceObject.__init__(self)
  192.  
  193.     
  194.     def from_xml(self, x):
  195.         
  196.         atomget = lambda k, default = (None,): getattr(x, '{%s}%s' % (constants.NS.Atom, k), default)
  197.         if not atomget('id'):
  198.             pass
  199.         id = unicode('')
  200.         if not atomget('title'):
  201.             pass
  202.         title = unicode('')
  203.         author_tag = atomget('author')
  204.         if author_tag is None:
  205.             author_name = author_id = author_url = author_tag
  206.         else:
  207.             author_name = unicode(author_tag.name)
  208.             author_id = author_url = unicode(author_tag.uri)
  209.         source_tag = atomget('source')
  210.         if source_tag is None:
  211.             source = None
  212.         else:
  213.             source = ActivitySource()
  214.             source.populate(source_tag, InputType.XML)
  215.         icon_url = None
  216.         preview_url = None
  217.         url = None
  218.         links = atomget('link', [])
  219.         for link in links:
  220.             rel = link.attrib.get('rel')
  221.             if rel == 'icon':
  222.                 icon_url = unicode(link.attrib.get('href'))
  223.             
  224.             if rel == 'preview':
  225.                 preview_url = unicode(link.attrib.get('href'))
  226.             
  227.             if rel == 'alternate' and url is None:
  228.                 url = unicode(link.attrib.get('href'))
  229.                 continue
  230.         
  231.         contents = []
  232.         for content in atomget('content', []):
  233.             contents.append((unicode(content.attrib.get('type')), htmlutils.render_contents(content)))
  234.         
  235.         self.__dict__.update(id = id, title = title, author_id = author_id, author_url = author_url, author_name = author_name, source = source, url = url, icon_url = icon_url, preview_url = preview_url, contents = contents)
  236.  
  237.  
  238.  
  239. class ActivityObject(AtomItem):
  240.     
  241.     def from_xml(self, x):
  242.         AtomItem.from_xml(self, x)
  243.         type_uri = x['object-type']
  244.         self.type_uri = unicode(type_uri)
  245.         self.type = constants.Object.get_name(self.type_uri)
  246.  
  247.  
  248.  
  249. class Activity(AtomItem):
  250.     
  251.     def from_xml(self, x):
  252.         AtomItem.from_xml(self, x)
  253.         updated = unicode(x.updated)
  254.         published = unicode(x.published)
  255.         updated_parsed = feedparser._parse_date(updated)
  256.         published_parsed = feedparser._parse_date(published)
  257.         activity_type = unicode(x.category.attrib.get('label'))
  258.         verb = unicode(getattr(x, '{%s}verb' % constants.NS.ActivityStreams, None))
  259.         object_tags = list(getattr(x, '{%s}object' % constants.NS.ActivityStreams, None))
  260.         objects = []
  261.         for object_tag in object_tags:
  262.             if object_tag is not None:
  263.                 object = ActivityObject()
  264.                 object.populate(object_tag, InputType.XML)
  265.                 objects.append(object)
  266.                 source = object.source
  267.                 continue
  268.             object = None
  269.             source = None
  270.         
  271.         mood = getattr(x, '{%s}mood' % constants.NS.ActivityContext, None)
  272.         if mood is None:
  273.             mood_text = None
  274.             mood_icon = None
  275.         else:
  276.             mood_text = unicode(mood)
  277.             mood_icon = unicode(mood.attrib.get('icon'))
  278.         object.mood_text = mood_text
  279.         object.mood_icon = mood_icon
  280.         self.__dict__.update(verb = verb, source = source, objects = [], updated = updated, published = published, updated_parsed = int(calendar.timegm(updated_parsed)) + TIMEZONE_FIX, published_parsed = int(calendar.timegm(published_parsed)) + TIMEZONE_FIX, activity_type = activity_type)
  281.  
  282.     
  283.     def supports_comments(self):
  284.         if not getattr(self, 'comments', None) is not None:
  285.             pass
  286.         return self.activity_type == 'StatusMoodUpdate'
  287.  
  288.     supports_comments = property(supports_comments)
  289.  
  290.  
  291. class StatusUpdate(CommentableMyspaceObject):
  292.     SPACER_URL = 'http://x.myspacecdn.com/modules/common/static/img/spacer.gif'
  293.     activity_type = 'DigsbyStatusUpdate'
  294.     
  295.     def __init__(self, acct):
  296.         CommentableMyspaceObject.__init__(self)
  297.         self.acct = weakref.ref(acct)
  298.  
  299.     
  300.     def author_name(self):
  301.         user = self.acct().user_from_id(self.author_id)
  302.         return getattr(user, 'displayName', getattr(user, 'name', None))
  303.  
  304.     author_name = property(author_name)
  305.     
  306.     def author_uri(self):
  307.         user = self.acct().user_from_id(self.author_id)
  308.         return getattr(user, 'profileUrl', getattr(user, 'webUri', ''))
  309.  
  310.     author_uri = property(author_uri)
  311.     
  312.     def title(self):
  313.         name = self.author_name
  314.         if name is None:
  315.             name = _(u'Private user')
  316.         
  317.         return u'%s %s' % (self.author_name.decode('xml'), self.body.decode('xml'))
  318.  
  319.     title = property(title)
  320.     
  321.     def from_json(self, js):
  322.         updated = self.updated = self.published = getattr(js, 'moodLastUpdated', getattr(js, 'moodStatusLastUpdated', 0))
  323.         if self.updated == 0:
  324.             self.updated_parsed = self.published_parsed = updated
  325.         else:
  326.             
  327.             try:
  328.                 self.updated_parsed = self.published_parsed = int(updated)
  329.             except (ValueError, TypeError):
  330.                 updated_parsed = feedparser._parse_date(updated)
  331.                 self.updated_parsed = self.published_parsed = int(calendar.timegm(updated_parsed))
  332.  
  333.         user = getattr(js, 'user', None)
  334.         if user is None:
  335.             self.author_id = js.userId
  336.         else:
  337.             self.author_id = user.userId
  338.         self.id = '%r//%r//%r' % ('status-update', self.author_id, self.published_parsed)
  339.         moodimage_url = getattr(js, 'moodPictureUrl', getattr(js, 'moodimageurl', None))
  340.         if moodimage_url == self.SPACER_URL:
  341.             moodimage_url = None
  342.         
  343.         self.icon_url = None
  344.         self.contents = [
  345.             (u'xhtml', js.status)]
  346.         self.body = js.status
  347.         self.mood_text = getattr(js, 'moodName', getattr(js, 'mood', None))
  348.         self.mood_icon = moodimage_url
  349.         self._numComments = 0
  350.         
  351.         try:
  352.             if not getattr(js, 'numComments', None):
  353.                 pass
  354.             self._numComments = int(getattr(js, '_numComments', None))
  355.         except (AttributeError, ValueError, TypeError):
  356.             self._numComments = None
  357.  
  358.         self.comments = map(MyspaceComment.from_json, js.get('comments', []))
  359.  
  360.     
  361.     def numComments(self):
  362.         if self.commentsRetrievedDate:
  363.             self._numComments = len(self.get_comments())
  364.         
  365.         return self._numComments
  366.  
  367.     numComments = property(numComments)
  368.     
  369.     def supports_comments(self):
  370.         return True
  371.  
  372.     supports_comments = property(supports_comments)
  373.  
  374.  
  375. class MyspaceComment(MyspaceObject):
  376.     LIKE = _(u'Like! (via http://lnk.ms/C5dls)')
  377.     DISLIKE = _(u'Dislike! (via http://lnk.ms/C5dls)')
  378.     
  379.     def __init__(self, data):
  380.         self.userid = data.author.id
  381.         self.text = data.body
  382.         self.commentId = data.commentId
  383.         if getattr(data, 'postedDate', None) is not None:
  384.             self.postedDate = data.postedDate
  385.             self.postedDate_parsed = int(calendar.timegm(feedparser._parse_date(self.postedDate)))
  386.         else:
  387.             self.postedDate_parsed = data.postedDate_parsed
  388.         
  389.         try:
  390.             int(self.userid)
  391.         except ValueError:
  392.             pass
  393.  
  394.         self.userid = 'myspace.com.person.%s' % self.userid
  395.  
  396.     
  397.     def is_like(self):
  398.         return self.text == self.LIKE
  399.  
  400.     is_like = property(is_like)
  401.     
  402.     def is_dislike(self):
  403.         return self.text == self.DISLIKE
  404.  
  405.     is_dislike = property(is_dislike)
  406.     
  407.     def from_json(self, js):
  408.         return MyspaceComment(util.Storage(author = util.Storage(id = js['userid']), body = js['text'], commentId = js['commentId'], postedDate = js.get('postedDate', None), postedDate_parsed = js.get('postedDate_parsed', None)))
  409.  
  410.     from_json = classmethod(from_json)
  411.  
  412.