Example #1
0
 def __init__(self):
     log.debug("opening cache...")
     if not config.username:
         raise ValueError("username not specified")
     log.debug(" for delicious user : %s", config.username)
     self.dao = DAO(os.path.join(config.config_dir, "%s.cache" % config.username))
     pydelicious.DEBUG = 0
     pydelicious.Waiter = _FileWaiter(DLCS_WAIT_TIME, os.path.join(config.config_dir, "pydelicious.stamp"))       
     self.api = DeliciousAPI(config.username, config.password)
     log.debug("opening cache...Ok")
Example #2
0
class Cache(object):
    
    def __init__(self):
        log.debug("opening cache...")
        if not config.username:
            raise ValueError("username not specified")
        log.debug(" for delicious user : %s", config.username)
        self.dao = DAO(os.path.join(config.config_dir, "%s.cache" % config.username))
        pydelicious.DEBUG = 0
        pydelicious.Waiter = _FileWaiter(DLCS_WAIT_TIME, os.path.join(config.config_dir, "pydelicious.stamp"))       
        self.api = DeliciousAPI(config.username, config.password)
        log.debug("opening cache...Ok")
    
    def refresh(self):
        last_sync = self.dao.get_last_sync()
        if not last_sync or last_sync and time.time() - float(last_sync) > sync_timeout:
            if last_sync and time.time() - float(last_sync) > sync_timeout:
                log.debug(" sync timeout exceeded : %s", time.time() - float(last_sync))
            last_update_in_cache = self.dao.get_last_update()
            ttt = self.api.posts_update()
            last_update = time.strftime("%a, %d %b %Y %H:%M:%S +0000", ttt["update"]["time"])
            log.debug(" last_update : %s, in cache : %s", last_update, last_update_in_cache)
            self._update_last_sync()
            if(last_update != last_update_in_cache):
                log.debug("refreshing cache...")
                tags = self.api.tags_get()['tags']
                posts = self.api.posts_all()['posts']
                self.dao.clear_posts()
                self.dao.clear_tags()
                self.dao.update_tags(tags)
                self.dao.update_posts(posts)
                self.dao.update_last_update(last_update)              
                log.debug("refreshing cache...Ok")
                return True
        return False
        
    def _update_last_sync(self):
        self.dao.update_last_sync(time.time())
        
    def find_posts_by_pattern(self, pattern, order=ORDER_POSTS_LAST):
        return self.dao.find_posts_by_pattern(pattern, order)
        
    def find_posts_by_tag(self, tag, exact, order=ORDER_POSTS_LAST):
        return self.dao.find_posts_by_tag(tag, exact, order)
    
    def find_tags(self, pattern, order=ORDER_TAGS_ALPHA):
        return self.dao.find_tags(pattern, order)
    
    def save_post(self, url, title, tags):
        log.debug("saving post...")
        try:
            self.api.posts_add(url=url, description=title, tags=tags)
        except DeliciousItemExistsError, url:
            raise SaveException('Item already exists : %s' % url)
        except DeliciousError, message:
            raise SaveException(message)