<s> from requests . compat import quote <EOL> from . url import ReaderUrl <EOL> class ItemsContainer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . items = [ ] <EOL> self . itemsById = { } <EOL> self . lastLoadOk = False <EOL> self . lastLoadLength = <NUM_LIT:0> <EOL> self . lastUpdated = None <EOL> self . unread = <NUM_LIT:0> <EOL> self . continuation = None <EOL> def _getContent ( self , excludeRead = False , continuation = None , loadLimit = <NUM_LIT:20> , since = None , until = None ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def loadItems ( self , excludeRead = False , loadLimit = <NUM_LIT:20> , since = None , until = None ) : <EOL> """<STR_LIT>""" <EOL> self . clearItems ( ) <EOL> self . loadtLoadOk = False <EOL> self . lastLoadLength = <NUM_LIT:0> <EOL> self . _itemsLoadedDone ( self . _getContent ( excludeRead , None , loadLimit , since , until ) ) <EOL> def loadMoreItems ( self , excludeRead = False , continuation = None , loadLimit = <NUM_LIT:20> , since = None , until = None ) : <EOL> """<STR_LIT>""" <EOL> self . lastLoadOk = False <EOL> self . lastLoadLength = <NUM_LIT:0> <EOL> if not continuation and not self . continuation : <EOL> return <EOL> self . _itemsLoadedDone ( self . _getContent ( excludeRead , continuation or self . continuation , loadLimit , since , until ) ) <EOL> def _itemsLoadedDone ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if data is None : <EOL> return <EOL> self . continuation = data . get ( '<STR_LIT>' , None ) <EOL> self . lastUpdated = data . get ( '<STR_LIT>' , None ) <EOL> self . lastLoadLength = len ( data . get ( '<STR_LIT>' , [ ] ) ) <EOL> self . googleReader . itemsToObjects ( self , data . get ( '<STR_LIT>' , [ ] ) ) <EOL> self . lastLoadOk = True <EOL> def _addItem ( self , item ) : <EOL> self . items . append ( item ) <EOL> self . itemsById [ item . id ] = item <EOL> def getItem ( self , id ) : <EOL> return self . itemsById [ id ] <EOL> def clearItems ( self ) : <EOL> self . items = [ ] <EOL> self . itemsById = { } <EOL> self . continuation = None <EOL> def getItems ( self ) : <EOL> return self . items <EOL> def countItems ( self , excludeRead = False ) : <EOL> if excludeRead : <EOL> sum ( [ <NUM_LIT:1> for item in self . items if item . isUnread ( ) ] ) <EOL> else : <EOL> return len ( self . items ) <EOL> def markItemRead ( self , item , read ) : <EOL> if read and item . isUnread ( ) : <EOL> self . unread -= <NUM_LIT:1> <EOL> elif not read and item . isRead ( ) : <EOL> self . unread += <NUM_LIT:1> <EOL> def markAllRead ( self ) : <EOL> self . unread = <NUM_LIT:0> <EOL> for item in self . items : <EOL> item . read = True <EOL> item . canUnread = False <EOL> result = self . googleReader . markFeedAsRead ( self ) <EOL> return result . upper ( ) == '<STR_LIT:OK>' <EOL> def countUnread ( self ) : <EOL> self . unread = self . countItems ( excludeRead = True ) <EOL> class Category ( ItemsContainer ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return unicode ( self ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT>" % ( self . label , self . unread , self . id ) <EOL> def __init__ ( self , googleReader , label , id ) : <EOL> """<STR_LIT>""" <EOL> super ( Category , self ) . __init__ ( ) <EOL> self . googleReader = googleReader <EOL> self . label = label <EOL> self . id = id <EOL> self . feeds = [ ] <EOL> self . fetchUrl = ReaderUrl . CATEGORY_URL + Category . urlQuote ( self . label ) <EOL> def _addFeed ( self , feed ) : <EOL> if not feed in self . feeds : <EOL> self . feeds . append ( feed ) <EOL> try : <EOL> self . unread += feed . unread <EOL> except : <EOL> pass <EOL> def getFeeds ( self ) : <EOL> return self . feeds <EOL> def _getContent ( self , excludeRead = False , continuation = None , loadLimit = <NUM_LIT:20> , since = None , until = None ) : <EOL> return self . googleReader . getCategoryContent ( self , excludeRead , continuation , loadLimit , since , until ) <EOL> def countUnread ( self ) : <EOL> self . unread = sum ( [ feed . unread for feed in self . feeds ] ) <EOL> def toArray ( self ) : <EOL> pass <EOL> def toJSON ( self ) : <EOL> pass <EOL> @ staticmethod <EOL> def urlQuote ( string ) : <EOL> """<STR_LIT>""" <EOL> return quote ( string . encode ( "<STR_LIT:utf-8>" ) ) <EOL> class BaseFeed ( ItemsContainer ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return unicode ( self ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def __unicode__ ( self ) : <EOL> return "<STR_LIT>" % ( self . title , self . id ) <EOL> def __init__ ( self , googleReader , title , id , unread , categories = [ ] ) : <EOL> """<STR_LIT>""" <EOL> super ( BaseFeed , self ) . __init__ ( ) <EOL> self . googleReader = googleReader <EOL> self . id = id <EOL> self . title = title <EOL> self . unread = unread <EOL> self . categories = [ ] <EOL> for category in categories : <EOL> self . addCategory ( category ) <EOL> self . continuation = None <EOL> def addCategory ( self , category ) : <EOL> if not category in self . categories : <EOL> self . categories . append ( category ) <EOL> category . _addFeed ( self ) <EOL> def getCategories ( self ) : <EOL> return self . categories <EOL> def _getContent ( self , excludeRead = False , continuation = None , loadLimit = <NUM_LIT:20> , since = None , until = None ) : <EOL> return self . googleReader . getFeedContent ( self , excludeRead , continuation , loadLimit , since , until ) <EOL> def markItemRead ( self , item , read ) : <EOL> super ( BaseFeed , self ) . markItemRead ( item , read ) <EOL> for category in self . categories : <EOL> category . countUnread ( ) <EOL> def markAllRead ( self ) : <EOL> self . unread = <NUM_LIT:0> <EOL> for category in self . categories : <EOL> category . countUnread ( ) <EOL> return super ( BaseFeed , self ) . markAllRead ( ) <EOL> def toArray ( self ) : <EOL> pass <EOL> def toJSON ( self ) : <EOL> pass <EOL> class SpecialFeed ( BaseFeed ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , googleReader , type ) : <EOL> """<STR_LIT>""" <EOL> super ( SpecialFeed , self ) . __init__ ( <EOL> googleReader , <EOL> title = type , <EOL> id = ReaderUrl . SPECIAL_FEEDS_PART_URL + type , <EOL> unread = <NUM_LIT:0> , <EOL> categories = [ ] , <EOL> ) <EOL> self . type = type <EOL> self . fetchUrl = ReaderUrl . CONTENT_BASE_URL + Category . urlQuote ( self . id ) <EOL> class Feed ( BaseFeed ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , googleReader , title , id , siteUrl = None , unread = <NUM_LIT:0> , categories = [ ] ) : <EOL> """<STR_LIT>""" <EOL> super ( Feed , self ) . __init__ ( googleReader , title , id , unread , categories ) <EOL> self . feedUrl = self . id . lstrip ( '<STR_LIT>' ) <EOL> self . siteUrl = siteUrl <EOL> self . fetchUrl = ReaderUrl . FEED_URL + Category . urlQuote ( self . id ) <EOL> class Item ( object ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return unicode ( self ) . encode ( '<STR_LIT:utf-8>' ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' % ( self . title , self . author , self . id ) <EOL> def __init__ ( self , googleReader , item , parent ) : <EOL> """<STR_LIT>""" <EOL> self . googleReader = googleReader <EOL> self . parent = parent <EOL> self . data = item <EOL> self . id = item [ '<STR_LIT:id>' ] <EOL> self . title = item . get ( '<STR_LIT:title>' , '<STR_LIT>' ) <EOL> self . author = item . get ( '<STR_LIT>' , None ) <EOL> self . content = item . get ( '<STR_LIT:content>' , item . get ( '<STR_LIT>' , { } ) ) . get ( '<STR_LIT:content>' , '<STR_LIT>' ) <EOL> self . origin = { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT:url>' : '<STR_LIT>' } <EOL> if '<STR_LIT>' in item : <EOL> self . time = int ( item [ '<STR_LIT>' ] ) // <NUM_LIT:1000> <EOL> else : <EOL> self . time = None <EOL> self . url = None <EOL> for alternate in item . get ( '<STR_LIT>' , [ ] ) : <EOL> if alternate . get ( '<STR_LIT:type>' , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> self . url = alternate [ '<STR_LIT>' ] <EOL> break <EOL> self . read = False <EOL> self . starred = False <EOL> self . shared = False <EOL> for category in item . get ( '<STR_LIT>' , [ ] ) : <EOL> if category . endswith ( '<STR_LIT>' ) : <EOL> self . read = True <EOL> elif category . endswith ( '<STR_LIT>' ) : <EOL> self . starred = True <EOL> elif category in ( '<STR_LIT>' , <EOL> '<STR_LIT>' % self . googleReader . userId ) : <EOL> self . shared = True <EOL> self . canUnread = item . get ( '<STR_LIT>' , '<STR_LIT:false>' ) != '<STR_LIT:true>' <EOL> try : <EOL> f = item [ '<STR_LIT>' ] <EOL> self . origin = { <EOL> '<STR_LIT:title>' : f . get ( '<STR_LIT:title>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:url>' : f . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> self . feed = self . googleReader . getFeed ( f [ '<STR_LIT>' ] ) <EOL> if not self . feed : <EOL> raise <EOL> if not self . feed . title and '<STR_LIT:title>' in f : <EOL> self . feed . title = f [ '<STR_LIT:title>' ] <EOL> except : <EOL> try : <EOL> self . feed = Feed ( self , f . get ( '<STR_LIT:title>' , '<STR_LIT>' ) , f [ '<STR_LIT>' ] , f . get ( '<STR_LIT>' , None ) , <NUM_LIT:0> , [ ] ) <EOL> try : <EOL> self . googleReader . _addFeed ( self . feed ) <EOL> except : <EOL> pass <EOL> except : <EOL> self . feed = None <EOL> self . parent . _addItem ( self ) <EOL> def isUnread ( self ) : <EOL> return not self . read <EOL> def isRead ( self ) : <EOL> return self . read <EOL> def markRead ( self , read = True ) : <EOL> self . parent . markItemRead ( self , read ) <EOL> self . read = read <EOL> if read : <EOL> result = self . googleReader . addItemTag ( self , ReaderUrl . TAG_READ ) <EOL> else : <EOL> result = self . googleReader . removeItemTag ( self , ReaderUrl . TAG_READ ) <EOL> return result . upper ( ) == '<STR_LIT:OK>' <EOL> def markUnread ( self , unread = True ) : <EOL> return self . markRead ( not unread ) <EOL> def isShared ( self ) : <EOL> return self . shared <EOL> def markShared ( self , shared = True ) : <EOL> self . shared = shared <EOL> if shared : <EOL> result = self . googleReader . addItemTag ( self , ReaderUrl . TAG_SHARED ) <EOL> else : <EOL> result = self . googleReader . removeItemTag ( self , ReaderUrl . TAG_SHARED ) <EOL> return result . upper ( ) == '<STR_LIT:OK>' <EOL> def share ( self ) : <EOL> return self . markShared ( ) <EOL> def unShare ( self ) : <EOL> return self . markShared ( False ) <EOL> def isStarred ( self ) : <EOL> return self . starred <EOL> def markStarred ( self , starred = True ) : <EOL> self . starred = starred <EOL> if starred : <EOL> result = self . googleReader . addItemTag ( self , ReaderUrl . TAG_STARRED ) <EOL> else : <EOL> result = self . googleReader . removeItemTag ( self , ReaderUrl . TAG_STARRED ) <EOL> return result . upper ( ) == '<STR_LIT:OK>' <EOL> def star ( self ) : <EOL> return self . markStarred ( ) <EOL> def unStar ( self ) : <EOL> return self . markStarred ( False ) </s>
