Implement export, add option -m for merge.

This commit is contained in:
Arun Prakash Jana 2016-06-02 21:09:16 +05:30
parent c54d3112ab
commit 30bd0ad5e8
No known key found for this signature in database
GPG Key ID: C0A712ED95043DCB
6 changed files with 97 additions and 37 deletions

View File

@ -28,7 +28,7 @@ Copyright (C) 2015-2016 [Arun Prakash Jana](mailto:engineerarun@gmail.com).
- Add, tag, comment on, search, update, remove bookmarks
- Merge-able portable database, to sync between systems (not released yet)
- Import HTML bookmark exports from Firefox, Google Chrome or IE
- Import/export bookmarks HTML (Firefox, Google Chrome, IE compatible)
- Fetch page title from web (default), refresh all titles in a go
- Open search results directly in browser
- Manual password protection using AES256 encryption
@ -109,11 +109,11 @@ Shell completion scripts for Bash, Fish and Zsh can be found in respective subdi
**NOTE:** If you are using `buku` v1.9 or below please refer to the installed man page or program help.
usage: buku [-a URL [tags ...]] [-u [N]] [-i file] [-d [N]]
usage: buku [-a URL [tags ...]] [-u [N]] [-d [N]] [-h]
[--url keyword] [--tag [...]] [-t [...]] [-c [...]]
[-s keyword [...]] [-S keyword [...]] [--st [...]]
[-k [N]] [-l [N]] [-p [N]] [-f N] [-r oldtag [newtag ...]]
[-j] [--merge file] [--noprompt] [-o N] [-z] [-h]
[-j] [-e file] [-i file] [-m file] [--noprompt] [-o N]
A private command-line bookmark manager. Your mini web!
@ -126,8 +126,6 @@ Shell completion scripts for Bash, Fish and Zsh can be found in respective subdi
N is specified without any edit options
-d, --delete [N] delete bookmark at DB index N
delete all bookmarks, if no arguments
-i, --import file import bookmarks from html file; Firefox,
Google Chrome and IE formats supported
-h, --help show this information and exit
edit options:
@ -165,7 +163,10 @@ Shell completion scripts for Bash, Fish and Zsh can be found in respective subdi
replace oldtag with newtag everywhere
delete oldtag, if no newtag
-j, --json Json formatted output for -p, -s, -S, --st
--merge file merge bookmarks from another buku database
-e, --export file export bookmarks to Firefox format html
-i, --import file import bookmarks from html file; Firefox,
Google Chrome and IE formats supported
-m, --merge file merge bookmarks from another buku database
--noprompt do not show the prompt, run and exit
-o, --open N open bookmark at DB index N in web browser
-z, --debug show debug information and additional logs

View File

@ -10,10 +10,11 @@ _buku () {
local IFS=$' \n'
local cur=$2 prev=$3
local -a opts opts_with_args
opts=(-a --add -c --comment -d --delete -h --help -i --import -k --unlock
-l --lock --merge --noprompt -o --open -p --print -r --replace -s --sany
-S --sall --st --stag --tag -t --title -u --update --url)
opts_with_arg=(-a --add -i --import --merge -o --open -r --replace -s --sany -S --sall)
opts=(-a --add -c --comment -d --delete -e --export -h --help -i --import
-k --unlock -l --lock -m --merge --noprompt -o --open -p --print -r --replace
-s --sany -S --sall --st --stag --tag -t --title -u --update --url)
opts_with_arg=(-a --add -e --export -i --import -m --merge
-o --open -r --replace -s --sany -S --sall --url)
# Do not complete non option names
[[ $cur == -* ]] || return 1

View File

@ -7,11 +7,12 @@
complete -c buku -s a -l add -r --description 'add bookmark'
complete -c buku -s c -l comment --description 'comment on bookmark'
complete -c buku -s d -l delete --description 'delete bookmark'
complete -c buku -s e -l export -r --description 'export bookmarks'
complete -c buku -s h -l help --description 'show help'
complete -c buku -s i -l import -r --description 'import bookmarks'
complete -c buku -s k -l unlock --description 'decrypt database'
complete -c buku -s l -l lock --description 'encrypt database'
complete -c buku -l merge -r --description 'merge another buku database'
complete -c buku -s m -l merge -r --description 'merge another buku database'
complete -c buku -l noprompt --description 'noninteractive mode'
complete -c buku -s o -l open -r --description 'open bookmark in browser'
complete -c buku -s p -l print --description 'show bookmark details'

View File

@ -12,8 +12,9 @@ args=(
'(-a --add)'{-a,--add}'[add bookmark]:URL tags'
'(-c --comment)'{-c,--comment}'[comment on bookmark]'
'(-d --delete)'{-d,--delete}'[delete bookmark]'
'(-e --export)'{-e,--export}'[export bookmarks]:html output file'
'(-h --help)'{-h,--help}'[show help]'
'(-h --help)'{-i,--import}'[import bookmarks]:html bookmark file'
'(-i --import)'{-i,--import}'[import bookmarks]:html imput file'
'(-k --unlock)'{-k,--unlock}'[decrypt database]'
'(-l --lock)'{-l,--lock}'[encrypt database]'
'(--merge)'{--merge}'[merge another buku database]:buku db file'

91
buku
View File

@ -781,6 +781,57 @@ class BukuDb:
except IndexError:
print('Index out of bound')
def export_bookmark(self, fp):
"""Export bookmarks to a Firefox
bookmarks formatted html file.
Params: Path to file to export to
"""
import time
if os.path.exists(fp):
resp = input('%s exists. Overwrite? Enter \x1b[1my\x1b[21m to confirm: ' % fp)
if resp != 'y':
return
try:
f = open(fp, mode='w', encoding='utf-8')
except Exception as e:
print('\x1b[1mEXCEPTION\x1b[21m [export_bookmark]: (%s) %s' % (type(e).__name__, e))
return
count = 0
timestamp = int(time.time())
self.cur.execute('SELECT * FROM bookmarks')
resultset = self.cur.fetchall()
f.write("""<!DOCTYPE NETSCAPE-Bookmark-file-1>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><H3 ADD_DATE="%s" LAST_MODIFIED="%s" PERSONAL_TOOLBAR_FOLDER="true">Buku bookmarks</H3>
<DL><p>
""" % (timestamp, timestamp))
for row in resultset:
entry = ' <DT><A HREF="%s" ADD_DATE="%s" LAST_MODIFIED="%s"' % (row[1], timestamp, timestamp)
if row[3] != DELIMITER:
entry = '%s TAGS="%s"' % (entry, row[3][1:-1])
entry = '%s>%s</A>\n' % (entry, row[2])
if row[4] != '':
entry = '%s <DD>%s\n' % (entry, row[4])
f.write(entry)
count += 1
f.write(" </DL><p>\n</DL><p>")
f.close()
print('%s bookmarks exported' % count)
def import_bookmark(self, fp):
"""Import bookmarks from a html file.
Supports Firefox, Google Chrome and IE imports
@ -788,10 +839,6 @@ class BukuDb:
Params: Path to file to import
"""
if not os.path.exists(fp):
printmsg('%s not found' % fp, 'ERROR')
self.close_quit(1)
try:
import bs4
with open(fp, mode='r', encoding='utf-8') as f:
@ -1357,11 +1404,11 @@ if __name__ == '__main__':
argparser = ExtendedArgumentParser(
description='A private command-line bookmark manager. Your mini web!',
formatter_class=argparse.RawTextHelpFormatter,
usage='''buku [-a URL [tags ...]] [-u [N]] [-i file] [-d [N]]
usage='''buku [-a URL [tags ...]] [-u [N]] [-d [N]] [-h]
[--url keyword] [--tag [...]] [-t [...]] [-c [...]]
[-s keyword [...]] [-S keyword [...]] [--st [...]]
[-k [N]] [-l [N]] [-p [N]] [-f N] [-r oldtag [newtag ...]]
[-j] [--merge file] [--noprompt] [-o N] [-z] [-h]''',
[-j] [-e file] [-i file] [-m file] [--noprompt] [-o N]''',
add_help=False
)
@ -1376,13 +1423,10 @@ if __name__ == '__main__':
N is specified without any edit options
-d, --delete [N] delete bookmark at DB index N
delete all bookmarks, if no arguments
-i, --import file import bookmarks from html file; Firefox,
Google Chrome and IE formats supported
-h, --help show this information and exit''')
general_group.add_argument('-a', '--add', nargs='+', dest='addurl', metavar=('URL', 'tags'), help=argparse.SUPPRESS)
general_group.add_argument('-u', '--update', nargs='*', dest='update', action=CustomUpdateAction, metavar=('N', 'URL tags'), help=argparse.SUPPRESS)
general_group.add_argument('-d', '--delete', nargs='?', dest='delete', type=int, const=0, metavar='N', help=argparse.SUPPRESS)
general_group.add_argument('-i', '--import', nargs=1, dest='imports', metavar='file', help=argparse.SUPPRESS)
general_group.add_argument('-h', '--help', dest='help', action='store_true', help=argparse.SUPPRESS)
# Edit options
@ -1437,7 +1481,10 @@ if __name__ == '__main__':
replace oldtag with newtag everywhere
delete oldtag, if no newtag
-j, --json Json formatted output for -p, -s, -S, --st
--merge file merge bookmarks from another buku database
-e, --export file export bookmarks to Firefox format html
-i, --import file import bookmarks from html file; Firefox,
Google Chrome and IE formats supported
-m, --merge file merge bookmarks from another buku database
--noprompt do not show the prompt, run and exit
-o, --open N open bookmark at DB index N in web browser
-z, --debug show debug information and additional logs''')
@ -1445,7 +1492,9 @@ if __name__ == '__main__':
power_group.add_argument('-f', '--format', dest='showOpt', type=int, choices=[1, 2], metavar='N', help=argparse.SUPPRESS)
power_group.add_argument('-r', '--replace', nargs='+', dest='replace', metavar=('oldtag', 'newtag'), help=argparse.SUPPRESS)
power_group.add_argument('-j', '--json', dest='jsonOutput', action='store_true', help=argparse.SUPPRESS)
general_group.add_argument('--merge', nargs=1, dest='merge', metavar='file', help=argparse.SUPPRESS)
general_group.add_argument('-e', '--export', nargs=1, dest='export', metavar='file', help=argparse.SUPPRESS)
general_group.add_argument('-i', '--import', nargs=1, dest='imports', metavar='file', help=argparse.SUPPRESS)
general_group.add_argument('-m', '--merge', nargs=1, dest='merge', metavar='file', help=argparse.SUPPRESS)
power_group.add_argument('--noprompt', dest='noninteractive', action='store_true', help=argparse.SUPPRESS)
power_group.add_argument('-o', '--open', dest='openurl', type=int, metavar='N', help=argparse.SUPPRESS)
power_group.add_argument('-z', '--debug', dest='debug', action='store_true', help=argparse.SUPPRESS)
@ -1495,14 +1544,6 @@ if __name__ == '__main__':
# Initialize the database and get handles
bdb = BukuDb()
# Import bookmarks
if args.imports is not None:
bdb.import_bookmark(args.imports[0])
# Merge a database file and exit
if args.merge is not None:
bdb.mergedb(args.merge[0])
# Add a record
if args.addurl is not None:
# Parse tags into a comma-separated string
@ -1576,6 +1617,18 @@ if __name__ == '__main__':
else:
bdb.replace_tag(args.replace[0], args.replace[1:])
# Export bookmarks
if args.export is not None:
bdb.export_bookmark(args.export[0])
# Import bookmarks
if args.imports is not None:
bdb.import_bookmark(args.imports[0])
# Merge a database file and exit
if args.merge is not None:
bdb.mergedb(args.merge[0])
# Open URL in browser
if args.openurl is not None:
if args.openurl < 1:

15
buku.1
View File

@ -3,7 +3,7 @@
buku \- A private command-line bookmark manager. Your mini web!
.SH SYNOPSIS
.B buku
[-a URL [tags ...]] [-u [N]] [-i file] [-d [N]]
[-a URL [tags ...]] [-u [N]] [-d [N]] [-h]
.br
[--url keyword] [--tag [...]] [-t [...]] [-c [...]]
.br
@ -11,7 +11,7 @@ buku \- A private command-line bookmark manager. Your mini web!
.br
[-k [N]] [-l [N]] [-p [N]] [-f N] [-r oldtag [newtag ...]]
.br
[-j] [--merge file] [--noprompt] [-o N] [-z] [-h]
[-j] [-e file] [-i file] [-m file] [--noprompt] [-o N]
.SH DESCRIPTION
.B buku
is a command-line tool to save, tag and search bookmarks.
@ -64,9 +64,6 @@ in DB. If
.I N
and other options are omitted, all titles are refreshed from the web. Works with update modifiers for the fields url, title, tag and comment. If only N is passed without any edit options, title is fetched and updated (if not empty).
.TP
.BI \-i " " \--import " file"
Import bookmarks exported from Firefox, Google Chrome or IE in HTML format.
.TP
.BI \-d " " \--delete " [N]"
Delete bookmark at index
.I N
@ -146,7 +143,13 @@ is omitted.
.BI \-j " " \--json
Output data formatted as json, works with -p, -s, -S, --st.
.TP
.BI \--merge " file"
.BI \-e " " \--export " file"
Export bookmarks to Firefox bookmarks formatted HTML.
.TP
.BI \-i " " \--import " file"
Import bookmarks exported from Firefox, Google Chrome or IE in HTML format.
.TP
.BI \-m " " \--merge " file"
Merge bookmarks from another Buku database file.
.TP
.BI \--noprompt