<s> import time <EOL> import dateutil . parser <EOL> import dateutil . tz <EOL> import logging <EOL> import urllib <EOL> from django . conf import settings <EOL> from django . db import transaction <EOL> from django . utils . encoding import smart_unicode <EOL> from jellyroll . models import Item , Bookmark <EOL> from jellyroll . providers import utils <EOL> class DeliciousClient ( object ) : <EOL> """<STR_LIT>""" <EOL> lastcall = <NUM_LIT:0> <EOL> def __init__ ( self , username , password , method = '<STR_LIT>' ) : <EOL> self . username , self . password = username , password <EOL> self . method = method <EOL> def __getattr__ ( self , method ) : <EOL> return DeliciousClient ( self . username , self . password , '<STR_LIT>' % ( self . method , method ) ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . method <EOL> def __call__ ( self , ** params ) : <EOL> delta = time . time ( ) - DeliciousClient . lastcall <EOL> if delta < <NUM_LIT:2> : <EOL> time . sleep ( <NUM_LIT:2> - delta ) <EOL> DeliciousClient . lastcall = time . time ( ) <EOL> url = ( "<STR_LIT>" % self . method ) + urllib . urlencode ( params ) <EOL> return utils . getxml ( url , username = self . username , password = self . password ) <EOL> log = logging . getLogger ( "<STR_LIT>" ) <EOL> def enabled ( ) : <EOL> ok = hasattr ( settings , '<STR_LIT>' ) and hasattr ( settings , '<STR_LIT>' ) <EOL> if not ok : <EOL> log . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return ok <EOL> def update ( ) : <EOL> delicious = DeliciousClient ( settings . DELICIOUS_USERNAME , settings . DELICIOUS_PASSWORD ) <EOL> last_update_date = Item . objects . get_last_update_of_model ( Bookmark ) <EOL> last_post_date = utils . parsedate ( delicious . posts . update ( ) . get ( "<STR_LIT:time>" ) ) <EOL> if last_post_date <= last_update_date : <EOL> log . info ( "<STR_LIT>" , last_update_date , last_post_date ) <EOL> return <EOL> for datenode in reversed ( list ( delicious . posts . dates ( ) . getiterator ( '<STR_LIT:date>' ) ) ) : <EOL> dt = utils . parsedate ( datenode . get ( "<STR_LIT:date>" ) ) <EOL> if dt > last_update_date : <EOL> log . debug ( "<STR_LIT>" ) <EOL> _update_bookmarks_from_date ( delicious , dt ) <EOL> def _update_bookmarks_from_date ( delicious , dt ) : <EOL> log . debug ( "<STR_LIT>" , dt ) <EOL> xml = delicious . posts . get ( dt = dt . strftime ( "<STR_LIT>" ) ) <EOL> for post in xml . getiterator ( '<STR_LIT>' ) : <EOL> info = dict ( ( k , smart_unicode ( post . get ( k ) ) ) for k in post . keys ( ) ) <EOL> if ( info . has_key ( "<STR_LIT>" ) and settings . DELICIOUS_GETDNS ) or ( not info . has_key ( "<STR_LIT>" ) ) : <EOL> log . debug ( "<STR_LIT>" , info [ "<STR_LIT>" ] ) <EOL> _handle_bookmark ( info ) <EOL> else : <EOL> log . debug ( "<STR_LIT>" , info [ "<STR_LIT>" ] ) <EOL> _update_bookmarks_from_date = transaction . commit_on_success ( _update_bookmarks_from_date ) <EOL> def _handle_bookmark ( info ) : <EOL> b , created = Bookmark . objects . get_or_create ( <EOL> url = info [ '<STR_LIT>' ] , <EOL> defaults = dict ( <EOL> description = info [ '<STR_LIT:description>' ] , <EOL> extended = info . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> ) <EOL> if not created : <EOL> b . description = info [ '<STR_LIT:description>' ] <EOL> b . extended = info . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> b . save ( ) <EOL> return Item . objects . create_or_update ( <EOL> instance = b , <EOL> timestamp = utils . parsedate ( info [ '<STR_LIT:time>' ] ) , <EOL> tags = info . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> source = __name__ , <EOL> source_id = info [ '<STR_LIT>' ] , <EOL> ) </s>
