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' export markdown, if file ends with '.md'
format: [title](url), 1 entry per line format: [title](url), 1 entry per line
use --tag to export only specific tags use --tag to export only specific tags
export buku DB, if file ends with '.db'
-i, --import file import Firefox or Chrome bookmarks html -i, --import file import Firefox or Chrome bookmarks html
import markdown, if file ends with '.md' import markdown, if file ends with '.md'
import buku DB, if file ends with '.db' 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' '(--colors)--colors[set output colors in 5-letter string]:color string'
'(-d --delete)'{-d,--delete}'[delete bookmark]' '(-d --delete)'{-d,--delete}'[delete bookmark]'
'(--deep)--deep[search matching substrings]' '(--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' '(--expand)--expand[expand a tny.im shortened URL]:index/shorturl'
'(-f --format)'{-f,--format}'[limit fields in print and Json output]:value' '(-f --format)'{-f,--format}'[limit fields in print and Json output]:value'
'(-h --help)'{-h,--help}'[show help]' '(-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 .I file
has extension '.md'. has extension '.md'.
.br .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 .TP
.BI \-i " " \--import " file" .BI \-i " " \--import " file"
Import bookmarks exported from Firefox or Google Chrome as HTML. 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. Indicates whether color should be used in output. Default is True.
""" """
self.conn, self.cur = BukuDb.initdb(dbfile)
self.json = json self.json = json
self.field_filter = field_filter self.field_filter = field_filter
self.chatty = chatty self.chatty = chatty
self.colorize = colorize self.colorize = colorize
self.conn, self.cur = BukuDb.initdb(dbfile, self.chatty)
@staticmethod @staticmethod
def get_default_dbdir(): def get_default_dbdir():
@ -537,7 +537,7 @@ class BukuDb:
return os.path.join(data_home, 'buku') return os.path.join(data_home, 'buku')
@staticmethod @staticmethod
def initdb(dbfile=None): def initdb(dbfile=None, chatty=False):
"""Initialize the database connection. """Initialize the database connection.
Create DB file and/or bookmarks table if they don't exist. Create DB file and/or bookmarks table if they don't exist.
@ -547,6 +547,8 @@ class BukuDb:
---------- ----------
dbfile : str, optional dbfile : str, optional
Custom database file path (including filename). Custom database file path (including filename).
chatty : bool
If True, shows informative message on DB creation.
Returns Returns
------- -------
@ -580,7 +582,7 @@ class BukuDb:
elif db_exists and enc_exists: elif db_exists and enc_exists:
logerr('Both encrypted and flat DB files exist!') logerr('Both encrypted and flat DB files exist!')
sys.exit(1) sys.exit(1)
else: elif chatty:
# not db_exists and not enc_exists # not db_exists and not enc_exists
print('DB file is being created at %s.\nYou should encrypt it.' % dbfile) 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): def exportdb(self, filepath, taglist=None):
"""Export DB bookmarks to file. """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 If destination file name ends with '.md', bookmarks are
exported to a markdown file. Otherwise, bookmarks are exported to a markdown file. Otherwise, bookmarks are
exported to a Firefox bookmarks.html formatted file. exported to a Firefox bookmarks.html formatted file.
@ -1966,7 +1970,7 @@ class BukuDb:
self.cur.execute(query, arguments) self.cur.execute(query, arguments)
resultset = self.cur.fetchall() resultset = self.cur.fetchall()
if not resultset: if not resultset:
print('No bookmarks exported') print('No records found')
return False return False
if os.path.exists(filepath): if os.path.exists(filepath):
@ -1974,6 +1978,19 @@ class BukuDb:
if resp != 'y': if resp != 'y':
return False 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: try:
outfp = open(filepath, mode='w', encoding='utf-8') outfp = open(filepath, mode='w', encoding='utf-8')
except Exception as e: except Exception as e:
@ -2410,6 +2427,17 @@ class BukuDb:
if to_commit: if to_commit:
self.conn.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): def close_quit(self, exitval=0):
"""Close a DB connection and exit. """Close a DB connection and exit.
@ -3873,6 +3901,7 @@ POSITIONAL ARGUMENTS:
-e, --export file export bookmarks in Firefox format html -e, --export file export bookmarks in Firefox format html
export markdown, if file ends with '.md' export markdown, if file ends with '.md'
format: [title](url), 1 entry per line format: [title](url), 1 entry per line
export buku DB, if file ends with '.db'
use --tag to export only specific tags use --tag to export only specific tags
-i, --import file import Firefox or Chrome bookmarks html -i, --import file import Firefox or Chrome bookmarks html
import markdown, if file ends with '.md' import markdown, if file ends with '.md'