def add(self, title, uri, guid, tags, atime=0): """ Add a new bookmark @param title as str @param uri as str @param guid as str @param tags as [str] @param parent_guid as str @param ctime as int @return bookmark id as int """ # Find an uniq guid while guid is None: guid = get_random_string(12) if self.exists_guid(guid): guid = None with SqlCursor(self) as sql: result = sql.execute( "INSERT INTO bookmarks\ (title, uri, popularity, guid, atime, mtime)\ VALUES (?, ?, ?, ?, ?, ?)", (title, uri.rstrip('/'), 0, guid, atime, 0)) bookmarks_id = result.lastrowid for tag in tags: if not tag: continue tag_id = self.get_tag_id(tag) if tag_id is None: tag_id = self.add_tag(tag) sql.execute( "INSERT INTO bookmarks_tags\ (bookmark_id, tag_id) VALUES (?, ?)", (bookmarks_id, tag_id)) return bookmarks_id
def add(self, title, uri, mtime, guid=None, atimes=[], commit=True): """ Add a new entry to history, if exists, update it @param title as str @param uri as str @param mtime as int @parma guid as str @param atime as [int] @param commit as bool @return history id as int """ if not uri: return uri = uri.rstrip('/') if title is None: title = "" # No guid provided, first search in bookmarks # Then in history. Db may be broken and contains multiple guid # for same uri if guid is None: bookmark_id = El().bookmarks.get_id(uri) if bookmark_id is not None: guid = El().bookmarks.get_guid(bookmark_id) else: history_id = El().history.get_id(uri) guid = El().history.get_guid(history_id) # Find an uniq guid if none exists in db while guid is None: guid = get_random_string(12) if self.exists_guid(guid): guid = None with SqlCursor(self) as sql: result = sql.execute("SELECT rowid, popularity FROM history\ WHERE guid=?", (guid,)) v = result.fetchone() if v is not None: history_id = v[0] sql.execute("UPDATE history\ SET uri=?, mtime=?, title=?, popularity=?\ WHERE rowid=?", (uri, mtime, title, v[1]+1, history_id)) else: result = sql.execute("INSERT INTO history\ (title, uri, mtime, popularity, guid)\ VALUES (?, ?, ?, ?, ?)", (title, uri, mtime, 0, guid)) history_id = result.lastrowid # Only add new atimes to db if not atimes: atimes = [mtime] current_atimes = self.get_atimes(history_id) for atime in atimes: if atime not in current_atimes: sql.execute("INSERT INTO history_atime\ (history_id, atime)\ VALUES (?, ?)", (history_id, atime)) if commit: sql.commit() return history_id
def add(self, title, uri, mtime, guid=None, atimes=[]): """ Add a new entry to history, if exists, update it @param title as str @param uri as str @param mtime as int @parma guid as str @param atime as [int] @return history id as int """ if not uri: return uri = uri.rstrip('/') parsed = urlparse(uri) with SqlCursor(self) as sql: result = sql.execute( "SELECT rowid, popularity FROM history\ WHERE uri=?", (uri, )) v = result.fetchone() # Update current item if v is not None: history_id = v[0] guid = self.get_guid(history_id) sql.execute( "UPDATE history\ SET uri=?, netloc=?, mtime=?,\ title=?, guid=?, popularity=?\ WHERE rowid=?", (uri, parsed.netloc, mtime, title, guid, v[1] + 1, history_id)) # Add a new item else: # Find an uniq guid while guid is None: guid = get_random_string(12) if self.exists_guid(guid): guid = None result = sql.execute( "INSERT INTO history\ (title, uri, netloc,\ mtime, popularity, guid)\ VALUES (?, ?, ?, ?, ?, ?)", (title, uri, parsed.netloc, mtime, 0, guid)) history_id = result.lastrowid # Only add new atimes to db if not atimes: atimes = [mtime] current_atimes = self.get_atimes(history_id) for atime in atimes: if atime not in current_atimes: sql.execute( "INSERT INTO history_atime\ (history_id, atime)\ VALUES (?, ?)", (history_id, atime)) return history_id