Support export to Buku database.

This commit is contained in:
Arun Prakash Jana 2017-09-17 11:53:18 +05:30
parent bd53cba38d
commit ff57ae5c2f
No known key found for this signature in database
GPG Key ID: A75979F35C080412
4 changed files with 38 additions and 6 deletions

View File

@ -203,6 +203,7 @@ POWER TOYS:
export markdown, if file ends with '.md'
format: [title](url), 1 entry per line
use --tag to export only specific tags
export buku DB, if file ends with '.db'
-i, --import file import Firefox or Chrome bookmarks html
import markdown, if file ends with '.md'
import buku DB, if file ends with '.db'

View File

@ -15,7 +15,7 @@ args=(
'(--colors)--colors[set output colors in 5-letter string]:color string'
'(-d --delete)'{-d,--delete}'[delete bookmark]'
'(--deep)--deep[search matching substrings]'
'(-e --export)'{-e,--export}'[export bookmarks]:html output file'
'(-e --export)'{-e,--export}'[export bookmarks]:html/md/db output file'
'(--expand)--expand[expand a tny.im shortened URL]:index/shorturl'
'(-f --format)'{-f,--format}'[limit fields in print and Json output]:value'
'(-h --help)'{-h,--help}'[show help]'

4
buku.1
View File

@ -188,7 +188,9 @@ Export bookmarks to Firefox bookmarks formatted HTML. Works with --tag to export
.I file
has extension '.md'.
.br
Markdown format: [title](url), 1 entry per line.
Markdown format: [title](url), 1 entry per line. A buku database is generated if
.I file
has extension '.db'.
.TP
.BI \-i " " \--import " file"
Import bookmarks exported from Firefox or Google Chrome as HTML.

37
buku.py
View File

@ -501,11 +501,11 @@ class BukuDb:
Indicates whether color should be used in output. Default is True.
"""
self.conn, self.cur = BukuDb.initdb(dbfile)
self.json = json
self.field_filter = field_filter
self.chatty = chatty
self.colorize = colorize
self.conn, self.cur = BukuDb.initdb(dbfile, self.chatty)
@staticmethod
def get_default_dbdir():
@ -537,7 +537,7 @@ class BukuDb:
return os.path.join(data_home, 'buku')
@staticmethod
def initdb(dbfile=None):
def initdb(dbfile=None, chatty=False):
"""Initialize the database connection.
Create DB file and/or bookmarks table if they don't exist.
@ -547,6 +547,8 @@ class BukuDb:
----------
dbfile : str, optional
Custom database file path (including filename).
chatty : bool
If True, shows informative message on DB creation.
Returns
-------
@ -580,7 +582,7 @@ class BukuDb:
elif db_exists and enc_exists:
logerr('Both encrypted and flat DB files exist!')
sys.exit(1)
else:
elif chatty:
# not db_exists and not enc_exists
print('DB file is being created at %s.\nYou should encrypt it.' % dbfile)
@ -1918,6 +1920,8 @@ class BukuDb:
def exportdb(self, filepath, taglist=None):
"""Export DB bookmarks to file.
If destination file name ends with '.db', bookmarks are
exported to a Buku database file.
If destination file name ends with '.md', bookmarks are
exported to a markdown file. Otherwise, bookmarks are
exported to a Firefox bookmarks.html formatted file.
@ -1966,7 +1970,7 @@ class BukuDb:
self.cur.execute(query, arguments)
resultset = self.cur.fetchall()
if not resultset:
print('No bookmarks exported')
print('No records found')
return False
if os.path.exists(filepath):
@ -1974,6 +1978,19 @@ class BukuDb:
if resp != 'y':
return False
if filepath.endswith('.db'):
os.remove(filepath)
if filepath.endswith('.db'):
outdb = BukuDb(dbfile=filepath)
qry = 'INSERT INTO bookmarks(URL, metadata, tags, desc, flags) VALUES (?, ?, ?, ?, ?)'
for row in resultset:
outdb.cur.execute(qry, (row[1], row[2], row[3], row[4], row[5]))
outdb.conn.commit()
outdb.close()
return True
try:
outfp = open(filepath, mode='w', encoding='utf-8')
except Exception as e:
@ -2410,6 +2427,17 @@ class BukuDb:
if to_commit:
self.conn.commit()
def close(self):
"""Close a DB connection."""
if self.conn is not None:
try:
self.cur.close()
self.conn.close()
except Exception:
# ignore errors here, we're closing down
pass
def close_quit(self, exitval=0):
"""Close a DB connection and exit.
@ -3873,6 +3901,7 @@ POSITIONAL ARGUMENTS:
-e, --export file export bookmarks in Firefox format html
export markdown, if file ends with '.md'
format: [title](url), 1 entry per line
export buku DB, if file ends with '.db'
use --tag to export only specific tags
-i, --import file import Firefox or Chrome bookmarks html
import markdown, if file ends with '.md'