From 82ff4318b7cdf226b9f2df3243e1235a10bc6a22 Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Tue, 20 Sep 2016 23:32:04 +0530 Subject: [PATCH] Export specific tags support. --- README.md | 43 ++++++++++++++++++++++++------------------- buku | 47 ++++++++++++++++++++++++++++++++++++++++------- buku.1 | 47 +++++++++++++++++++++++++++++------------------ 3 files changed, 93 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index d8889be..574c62b 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,7 @@ Shell completion scripts for Bash, Fish and Zsh can be found in respective subdi power toys: -e, --export file export bookmarks to Firefox format html + use --tag to export only specific tags -i, --import file import bookmarks from html file; Firefox, Google Chrome and IE formats supported -m, --merge file merge bookmarks from another buku database @@ -250,72 +251,76 @@ Note that URL must precede tags. $ buku -u 15012014 -c this is a new comment Applies to --url, --title and --tag too. -7. Import bookmarks: +7. **Export** bookmarks tagged `tag 1` or `tag 2`: + + $ buku -e bookmarks.html tag 1, tag 2 +All bookmarks are exported if --tag is not specified. +8. **Import** bookmarks: $ buku -i bookmarks.html HTML exports from Firefox, Google Chrome and IE are supported. -8. **Delete only comment** for bookmark at 15012014: +9. **Delete only comment** for bookmark at 15012014: $ buku -u 15012014 -c Applies to --title and --tag too. URL cannot be deleted without deleting the bookmark. -9. **Update** or refresh **full DB** with page titles from the web: +10. **Update** or refresh **full DB** with page titles from the web: $ buku -u This operation does not modify the indexes, URLs, tags or comments. Only title is refreshed if fetched title is non-empty. -10. **Delete** bookmark at index 15012014: +11. **Delete** bookmark at index 15012014: $ buku -d 15012014 Index 15012020 moved to 15012014 The last index is moved to the deleted index to keep the DB compact. -11. **Delete all** bookmarks: +12. **Delete all** bookmarks: $ buku -d -12. **Delete** a **range or list** of bookmarks: +13. **Delete** a **range or list** of bookmarks: $ buku -d 100-200 // delete bookmarks from index 100 to 200 $ buku 100 15 200 // delete bookmarks at indices 100, 15 and 200 -13. **Search** bookmarks for **ANY** of the keywords `kernel` and `debugging` in URL, title or tags: +14. **Search** bookmarks for **ANY** of the keywords `kernel` and `debugging` in URL, title or tags: $ buku -s kernel debugging -14. **Search** bookmarks with **ALL** the keywords `kernel` and `debugging` in URL, title or tags: +15. **Search** bookmarks with **ALL** the keywords `kernel` and `debugging` in URL, title or tags: $ buku -S kernel debugging -15. **Search** bookmarks **tagged** `general kernel concepts`: +16. **Search** bookmarks **tagged** `general kernel concepts`: $ buku --st general kernel concepts -16. List **all unique tags** alphabetically: +17. List **all unique tags** alphabetically: $ buku --st -17. **Encrypt or decrypt** DB with **custom number of iterations** (15) to generate key: +18. **Encrypt or decrypt** DB with **custom number of iterations** (15) to generate key: $ buku -l 15 $ buku -k 15 The same number of iterations must be specified for one lock & unlock instance. Default is 8, if omitted. -18. **Show details** of bookmark at index 15012014: +19. **Show details** of bookmark at index 15012014: $ buku -p 15012014 -19. **Show all** bookmarks with real index from database: +20. **Show all** bookmarks with real index from database: $ buku -p $ buku -p | more -20. **Replace tag** 'old tag' with 'new tag': +21. **Replace tag** 'old tag' with 'new tag': $ buku -r 'old tag' new tag -21. **Delete tag** 'old tag' from DB: +22. **Delete tag** 'old tag' from DB: $ buku -r 'old tag' -22. **Append (or delete) tags** 'tag 1', 'tag 2' to (or from) existing tags of bookmark at index 15012014: +23. **Append (or delete) tags** 'tag 1', 'tag 2' to (or from) existing tags of bookmark at index 15012014: $ buku -u 15012014 --tag + tag 1, tag 2 $ buku -u 15012014 --tag - tag 1, tag 2 -23. **Open URL** at index 15012014 in browser: +24. **Open URL** at index 15012014 in browser: $ buku -o 15012014 -24. To list bookmarks with no title or tags for **bookkeeping**: +25. To list bookmarks with no title or tags for **bookkeeping**: $ buku -S blank -25. More **help**: +26. More **help**: $ buku $ man buku diff --git a/buku b/buku index c013d7d..92690f8 100755 --- a/buku +++ b/buku @@ -950,7 +950,7 @@ class BukuDb: except IndexError: logger.error('Index out of bound') - def export_bookmark(self, fp): + def export_bookmark(self, fp, taglist=None): '''Export bookmarks to a Firefox bookmarks formatted html file. @@ -959,6 +959,38 @@ class BukuDb: import time + count = 0 + timestamp = int(time.time()) + arguments = [] + query = 'SELECT * FROM bookmarks' + tagvalid = False + + if taglist is not None: + tagstr = parse_tags(taglist) + + if len(tagstr) == 0 or tagstr == DELIMITER: + logger.error('Invalid tag') + return + + if len(tagstr) > 0: + tags = tagstr.split(DELIMITER) + query = '%s WHERE' % query + for tag in tags: + if tag != '': + tagvalid = True + query += " tags LIKE '%' || ? || '%' OR" + tag = '%s%s%s' % (DELIMITER, tag, DELIMITER) + arguments += (tag,) + + if tagvalid: + query = query[:-3] + else: + query = query[:-6] + + logger.debug('(%s), %s' % (query, arguments)) + self.cur.execute(query, arguments) + resultset = self.cur.fetchall() + if os.path.exists(fp): resp = input('%s exists. Overwrite? (y/n): ' % fp) if resp != 'y': @@ -970,11 +1002,6 @@ class BukuDb: logger.error(e) return - count = 0 - timestamp = int(time.time()) - self.cur.execute('SELECT * FROM bookmarks') - resultset = self.cur.fetchall() - f.write(''' @@ -1695,6 +1722,7 @@ if __name__ == '__main__': power_grp = argparser.add_argument_group( title='power toys', description='''-e, --export file export bookmarks to Firefox format html + use --tag to export only specific tags -i, --import file import bookmarks from html file; Firefox, Google Chrome and IE formats supported -m, --merge file merge bookmarks from another buku database @@ -1904,7 +1932,12 @@ if __name__ == '__main__': # Export bookmarks if args.export is not None: - bdb.export_bookmark(args.export[0]) + if args.tag is None: + bdb.export_bookmark(args.export[0]) + elif len(args.tag) == 0: + logger.error('Missing tag') + else: + bdb.export_bookmark(args.export[0], args.tag) # Import bookmarks if args.imports is not None: diff --git a/buku.1 b/buku.1 index 79f5afe..a12f449 100644 --- a/buku.1 +++ b/buku.1 @@ -101,7 +101,7 @@ Decrypt (unlock) the DB file with .SH POWER OPTIONS .TP .BI \-e " " \--export " file" -Export bookmarks to Firefox bookmarks formatted HTML. +Export bookmarks to Firefox bookmarks formatted HTML. Works with --tag to export only specific tags. .TP .BI \-i " " \--import " file" Import bookmarks exported from Firefox, Google Chrome or IE in HTML format. @@ -217,6 +217,17 @@ Note that URL must precede tags. Applies to --url, --title and --tag too. .PP .IP 7. 4 +\fBExport\fR bookmarks tagged 'tag 1' or 'tag 2': +.PP +.EX +.IP +.B buku -e bookmarks.html --tag tag 1, tag 2 +.EE +.PP +.IP "" 4 +All bookmarks are exported if --tag is not specified. +.PP +.IP 8. 4 \fBImport\fR bookmarks: .PP .EX @@ -227,7 +238,7 @@ Applies to --url, --title and --tag too. .IP "" 4 HTML exports from Firefox, Google Chrome and IE are supported. .PP -.IP 8. 4 +.IP 9. 4 \fBDelete only comment\fR for bookmark at 15012014: .PP .EX @@ -238,7 +249,7 @@ HTML exports from Firefox, Google Chrome and IE are supported. .IP "" 4 Applies to --title and --tag too. URL cannot be deleted without deleting the bookmark. .PP -.IP 9. 4 +.IP 10. 4 \fBUpdate\fR or refresh \fBfull DB\fR with page titles from the web: .PP .EX @@ -249,7 +260,7 @@ Applies to --title and --tag too. URL cannot be deleted without deleting the boo .IP "" 4 This operation does not modify the indexes, URLs, tags or comments. Only title is refreshed if fetched title is non-empty. .PP -.IP 10. 4 +.IP 11. 4 \fBDelete\fR bookmark at index 15012014: .PP .EX @@ -260,14 +271,14 @@ This operation does not modify the indexes, URLs, tags or comments. Only title i .IP "" 4 The last index is moved to the deleted index to keep the DB compact. .PP -.IP 11. 4 +.IP 12. 4 \fBDelete all\fR bookmarks: .PP .EX .IP .B buku -d .PP -.IP 12. 4 +.IP 13. 4 \fBDelete\fR a \fBrange or list\fR of bookmarks: .PP .EX @@ -275,35 +286,35 @@ The last index is moved to the deleted index to keep the DB compact. .B $ buku -d 100-200 // delete bookmarks from index 100 to 200 .B $ buku 100 15 200 // delete bookmarks at indices 100, 15 and 200 .PP -.IP 13. 4 +.IP 14. 4 \fBSearch\fR bookmarks for \fBANY\fR of the keywords 'kernel' and 'debugging' in URL, title or tags: .PP .EX .IP .B buku -s kernel debugging .PP -.IP 14. 4 +.IP 15. 4 \fBSearch\fR bookmarks with \fBALL\fR the keywords 'kernel' and 'debugging' in URL, title or tags: .PP .EX .IP .B buku -S kernel debugging .PP -.IP 15. 4 +.IP 16. 4 \fBSearch\fR bookmarks \fBtagged\fR 'general kernel concepts': .PP .EX .IP .B buku --st general kernel concepts .PP -.IP 16. 4 +.IP 17. 4 List \fBall unique tags\fR alphabetically: .PP .EX .IP .B buku --st .PP -.IP 17. 4 +.IP 18. 4 \fBEncrypt or decrypt\fR DB with \fBcustom number of iterations\fR (15) to generate key: .PP .EX @@ -316,14 +327,14 @@ List \fBall unique tags\fR alphabetically: .IP "" 4 The same number of iterations must be specified for one lock & unlock instance. Default is 8, if omitted. .PP -.IP 18. 4 +.IP 19. 4 \fBShow details\fR of bookmark at index 15012014: .PP .EX .IP .B buku -p 15012014 .PP -.IP 19. 4 +.IP 20. 4 \fBShow all\fR bookmarks with real index from database: .PP .EX @@ -331,21 +342,21 @@ The same number of iterations must be specified for one lock & unlock instance. .B buku -p .B buku -p | more .PP -.IP 20. 4 +.IP 21. 4 \fBReplace tag\fR 'old tag' with 'new tag': .PP .EX .IP .B buku -r 'old tag' new tag .PP -.IP 21. 4 +.IP 22. 4 \fBDelete tag\fR 'old tag' from DB: .PP .EX .IP .B buku -r 'old tag' .PP -.IP 22. 4 +.IP 23. 4 \fBAppend (or delete) tags\fR 'tag 1', 'tag 2' to (or from) existing tags of bookmark at index 15012014: .PP .EX @@ -353,14 +364,14 @@ The same number of iterations must be specified for one lock & unlock instance. .B buku -u 15012014 --tag + tag 1, tag 2 .B buku -u 15012014 --tag - tag 1, tag 2 .PP -.IP 23. 4 +.IP 24. 4 \fBOpen URL\fR at index 15012014 in browser: .PP .EX .IP .B buku -o 15012014 .PP -.IP 24. 4 +.IP 25. 4 To list bookmarks with no title or tags for \fBbookkeeping\fR: .PP .EX