Reformat and optimize DB refresh.

Signed-off-by: Arun Prakash Jana <engineerarun@gmail.com>
This commit is contained in:
Arun Prakash Jana 2016-03-25 00:17:57 +05:30
parent ca21ac579a
commit 1a47576649
3 changed files with 54 additions and 43 deletions

View File

@ -173,7 +173,7 @@ Tags are updated too. Original tags are removed.
4. Update or **refresh full DB**:
$ buku -R
This operation does not modify the existing tags. Only titles are refreshed.
This operation does not modify the indexes, URLs or tags. Only titles, if non-empty, are refreshed.
5. **Delete** bookmark at index 15012014:
$ buku -d 15012014

91
buku
View File

@ -107,6 +107,37 @@ def usage():
# Initialize the database connection
# Create bookmarks table is not existing
def initdb():
dbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku')
if not os.path.exists(dbpath):
os.makedirs(dbpath)
dbfile = os.path.join(dbpath, 'bookmarks.db')
encpath = os.path.join(dbpath, 'bookmarks.db.enc')
# Notify if DB file needs to be decrypted first
if os.path.exists(encpath) and not os.path.exists(dbfile):
print("Unlock database first")
sys.exit(1)
# Show info on first creation
if no_crypto == False and not os.path.exists(dbfile):
print("DB file is being created. You may want to encrypt it later.")
# Create a connection
conn = sqlite3.connect(dbfile)
cur = conn.cursor()
# Create table if it doesn't exist
cur.execute('''CREATE TABLE if not exists bookmarks \
(id integer PRIMARY KEY, URL text NOT NULL UNIQUE, metadata text, tags text)''')
conn.commit()
return (conn, cur)
# Get page response data
def getPageResp(url, redir=False):
if url.find("https://") >= 0: # Secure connection
@ -184,37 +215,6 @@ def fetchTitle(url):
# Initialize the database connection
# Create bookmarks table is not existing
def initdb():
dbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku')
if not os.path.exists(dbpath):
os.makedirs(dbpath)
dbfile = os.path.join(dbpath, 'bookmarks.db')
encpath = os.path.join(dbpath, 'bookmarks.db.enc')
# Notify if DB file needs to be decrypted first
if os.path.exists(encpath) and not os.path.exists(dbfile):
print("Unlock database first")
sys.exit(1)
# Show info on first creation
if no_crypto == False and not os.path.exists(dbfile):
print("DB file is being created. You may want to encrypt it later.")
# Create a connection
conn = sqlite3.connect(dbfile)
cur = conn.cursor()
# Create table if it doesn't exist
cur.execute('''CREATE TABLE if not exists bookmarks \
(id integer PRIMARY KEY, URL text NOT NULL UNIQUE, metadata text, tags text)''')
conn.commit()
return (conn, cur)
# Add a new bookmark or update an existing record at index
def AddUpdateEntry(conn, cur, keywords, index):
global titleManual
@ -279,6 +279,25 @@ def AddUpdateEntry(conn, cur, keywords, index):
# Refresh full DB
def dbRefresh(conn, cur):
cur.execute("SELECT id, url FROM bookmarks ORDER BY id ASC")
resultset = cur.fetchall()
for row in resultset:
title = fetchTitle(row[1])
if title == '':
print("\x1B[91mTitle: []")
print("\x1b[1mNOT updating index %d\x1b[21m\x1B[0m\n" % row[0])
continue
else:
print("Title: [%s]" % title)
cur.execute("UPDATE bookmarks SET metadata = ? WHERE id = ?", (title, row[0],))
conn.commit()
print("Updated index %d\n" % row[0])
# Search the database for a tag or matching URL or Title info
def searchdb(cur, keywords):
searchtag = ''
@ -795,7 +814,6 @@ try:
print("You can either add or update or delete in one instance\n")
usage()
online = True
refresh = True
elif opt[0] == "-r":
replace = True
@ -886,14 +904,7 @@ if addurl == True or update == True:
# Refresh full DB
if refresh == True:
cur.execute("SELECT id, url, tags FROM bookmarks")
resultset = cur.fetchall()
for row in resultset:
olddata = []
olddata.append(row[1])
olddata.append(row[2])
AddUpdateEntry(conn, cur, olddata, row[0])
print("")
dbRefresh(conn, cur)
# Search tags, URLs, Title info
if search == True:

4
buku.1
View File

@ -79,7 +79,7 @@ in DB.
Show all bookmark records from the DB along with actual index. Shows URL, title and tags.
.TP
.B \-R
Refresh all bookmarks. Titles are fetched from the web. Tags are retained.
Refresh all bookmarks. Titles are fetched from the web and updated if non-empty. Indexes, URLs and tags are retained.
.TP
.BI \-s " keywords"
Search bookmarks for a (partial) tag or any keyword and show the results. Prompts to enter result number to open in browser. Note that the sequential index number may not match the real index in database. DB index is shown in the end within '()'.
@ -163,7 +163,7 @@ Update or \fBrefresh full DB\fR:
.EE
.PP
.IP "" 4
This operation does not modify the existing tags. Only titles are refreshed.
This operation does not modify the indexes, URLs or tags. Only titles, if non-empty, are refreshed.
.PP
.IP 5. 4
\fBDelete\fR bookmark at index 15012014: