buku/buku

1822 lines
61 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
#
# Bookmark management utility
#
# Copyright (C) 2015-2016 Arun Prakash Jana <engineerarun@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with buku. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import sqlite3
2016-09-05 03:18:21 -05:00
import re
2016-04-24 14:19:32 -05:00
import argparse
import webbrowser
import html.parser as HTMLParser
2016-04-04 04:50:23 -05:00
from http.client import HTTPConnection, HTTPSConnection
from urllib.parse import urljoin, quote, unquote
import gzip
import io
import signal
import logging
import inspect
import atexit
import readline
# Globals
2016-04-05 23:55:25 -05:00
update = False # Update a bookmark in DB
2016-05-18 22:24:46 -05:00
tagManual = None # Tags for update command
2016-04-05 23:55:25 -05:00
titleManual = None # Manually add a title offline
2016-05-17 15:11:31 -05:00
description = None # Description of the bookmark
2016-05-18 22:24:46 -05:00
tagsearch = False # Search bookmarks by tag
titleData = None # Title fetched from a webpage
interrupted = False # Received SIGINT
2016-05-29 08:33:58 -05:00
DELIMITER = ',' # Delimiter used to store tags in DB
2016-06-21 10:51:04 -05:00
_VERSION_ = '2.3' # Program version
2016-04-05 23:55:25 -05:00
# Crypto globals
BLOCKSIZE = 65536
SALT_SIZE = 32
CHUNKSIZE = 0x80000 # Read/write 512 KB chunks
# Set up logging
logging.basicConfig(format='[%(levelname)s] %(message)s')
logger = logging.getLogger()
2016-04-05 06:25:40 -05:00
class BMHTMLParser(HTMLParser.HTMLParser):
2016-04-05 23:39:56 -05:00
"""Class to parse and fetch the title from a HTML page, if available"""
2016-04-05 06:25:40 -05:00
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self.inTitle = False
2016-05-24 12:51:38 -05:00
self.data = ''
2016-04-05 06:25:40 -05:00
self.lasttag = None
def handle_starttag(self, tag, attrs):
self.inTitle = False
2016-05-24 12:51:38 -05:00
if tag == 'title':
2016-04-05 06:25:40 -05:00
self.inTitle = True
self.lasttag = tag
def handle_endtag(self, tag):
global titleData
2016-05-24 12:51:38 -05:00
if tag == 'title':
2016-04-05 06:25:40 -05:00
self.inTitle = False
2016-05-24 12:51:38 -05:00
if self.data != '':
2016-04-05 06:25:40 -05:00
titleData = self.data
self.reset() # We have received title data, exit parsing
2016-04-05 06:25:40 -05:00
def handle_data(self, data):
2016-05-31 12:39:34 -05:00
if self.lasttag == 'title' and self.inTitle:
self.data = '%s%s' % (self.data, data)
2016-04-05 06:25:40 -05:00
def error(self, message):
pass
2016-05-31 12:39:34 -05:00
class BukuCrypt:
""" Class to handle encryption and decryption
of the database file. Functionally a separate entity.
Involves late imports in the static functions but it
saves ~100ms each time. Given that encrypt/decrypt are
not done automatically and any one should be called at
a time, this doesn't seem to be an outrageous approach.
"""
@staticmethod
def get_filehash(filepath):
"""Get the SHA256 hash of a file
Params: path to the file
"""
from hashlib import sha256
with open(filepath, 'rb') as f:
hasher = sha256()
buf = f.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = f.read(BLOCKSIZE)
return hasher.digest()
@staticmethod
def encrypt_file(iterations):
"""Encrypt the bookmarks database file"""
try:
from getpass import getpass
import struct
from hashlib import sha256
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
except Exception:
logger.error('cryptography lib(s) missing')
sys.exit(1)
if iterations < 1:
logger.error('Iterations must be >= 1')
sys.exit(1)
dbpath = os.path.join(BukuDb.get_dbdir_path(), 'bookmarks.db')
encpath = '%s.enc' % dbpath
if not os.path.exists(dbpath):
logger.error('%s missing. Already encrypted?', dbpath)
sys.exit(1)
# If both encrypted file and flat file exist, error out
if os.path.exists(dbpath) and os.path.exists(encpath):
logger.error('Both encrypted and flat DB files exist!')
sys.exit(1)
password = ''
password = getpass()
passconfirm = getpass()
if password == '':
logger.error('Empty password')
sys.exit(1)
if password != passconfirm:
logger.error("Passwords don't match")
sys.exit(1)
# Get SHA256 hash of DB file
dbhash = BukuCrypt.get_filehash(dbpath)
# Generate random 256-bit salt and key
salt = os.urandom(SALT_SIZE)
key = ('%s%s' % (password, salt.decode('utf-8', 'replace'))).encode('utf-8')
for _ in range(iterations):
key = sha256(key).digest()
iv = os.urandom(16)
encryptor = Cipher(
algorithms.AES(key),
modes.CBC(iv),
backend=default_backend()
).encryptor()
filesize = os.path.getsize(dbpath)
with open(dbpath, 'rb') as infile:
with open(encpath, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
outfile.write(salt)
outfile.write(iv)
# Embed DB file hash in encrypted file
outfile.write(dbhash)
while True:
chunk = infile.read(CHUNKSIZE)
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk = '%s%s' % (chunk, ' ' * (16 - len(chunk) % 16))
outfile.write(encryptor.update(chunk) + encryptor.finalize())
os.remove(dbpath)
print('File encrypted')
sys.exit(0)
@staticmethod
def decrypt_file(iterations):
"""Decrypt the bookmarks database file"""
try:
from getpass import getpass
import struct
from hashlib import sha256
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
except Exception:
logger.error('cryptography lib(s) missing')
sys.exit(1)
if iterations < 1:
logger.error('Decryption failed')
sys.exit(1)
dbpath = os.path.join(BukuDb.get_dbdir_path(), 'bookmarks.db')
encpath = '%s.enc' % dbpath
if not os.path.exists(encpath):
logger.error('%s missing', encpath)
sys.exit(1)
# If both encrypted file and flat file exist, error out
if os.path.exists(dbpath) and os.path.exists(encpath):
logger.error('Both encrypted and flat DB files exist!')
sys.exit(1)
password = ''
password = getpass()
if password == '':
logger.error('Decryption failed')
sys.exit(1)
with open(encpath, 'rb') as infile:
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
# Read 256-bit salt and generate key
salt = infile.read(32)
key = ('%s%s' % (password, salt.decode('utf-8', 'replace'))).encode('utf-8')
for _ in range(iterations):
key = sha256(key).digest()
iv = infile.read(16)
decryptor = Cipher(
algorithms.AES(key),
modes.CBC(iv),
backend=default_backend(),
).decryptor()
# Get original DB file's SHA256 hash from encrypted file
enchash = infile.read(32)
with open(dbpath, 'wb') as outfile:
while True:
chunk = infile.read(CHUNKSIZE)
if len(chunk) == 0:
break
outfile.write(decryptor.update(chunk) + decryptor.finalize())
outfile.truncate(origsize)
# Match hash of generated file with that of original DB file
dbhash = BukuCrypt.get_filehash(dbpath)
if dbhash != enchash:
os.remove(dbpath)
logger.error('Decryption failed')
sys.exit(1)
else:
os.remove(encpath)
print('File decrypted')
class BukuDb:
2016-04-05 06:25:40 -05:00
2016-06-29 13:22:07 -05:00
def __init__(self, noninteractive=False, json=False, showOpt=0):
conn, cur = BukuDb.initdb()
self.conn = conn
self.cur = cur
2016-05-28 08:15:03 -05:00
self.noninteractive = noninteractive
2016-06-29 13:06:33 -05:00
self.json = json
2016-06-29 13:22:07 -05:00
self.showOpt = showOpt
2016-04-10 07:28:49 -05:00
@staticmethod
def get_dbdir_path():
"""Determine the directory path where dbfile will be stored:
if $XDG_DATA_HOME is defined, use it
else if $HOME exists, use it
else use the current directory
"""
data_home = os.environ.get('XDG_DATA_HOME')
if data_home is None:
if os.environ.get('HOME') is None:
data_home = '.'
else:
data_home = os.path.join(os.environ.get('HOME'), '.local', 'share')
return os.path.join(data_home, 'buku')
@staticmethod
def move_legacy_dbfile():
"""Move database file from earlier path used in versions <= 1.8
to new path. Errors out if both the old and new DB files exist.
"""
olddbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku')
olddbfile = os.path.join(olddbpath, 'bookmarks.db')
if not os.path.exists(olddbfile):
return
newdbpath = BukuDb.get_dbdir_path()
newdbfile = os.path.join(newdbpath, 'bookmarks.db')
if os.path.exists(newdbfile):
logger.error('Both old (%s) and new (%s) databases exist, need manual action', olddbfile, newdbfile)
sys.exit(1)
if not os.path.exists(newdbpath):
os.makedirs(newdbpath)
os.rename(olddbfile, newdbfile)
2016-05-24 12:51:38 -05:00
print('Database was moved from old (%s) to new (%s) location.\n' % (olddbfile, newdbfile))
os.rmdir(olddbpath)
@staticmethod
def initdb():
"""Initialize the database connection. Create DB file and/or bookmarks table
if they don't exist. Alert on encryption options on first execution.
Returns: connection, cursor
"""
dbpath = BukuDb.get_dbdir_path()
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):
logger.error('Unlock database first')
sys.exit(1)
# Show info on first creation
if not os.path.exists(dbfile):
2016-05-24 12:51:38 -05:00
print('DB file is being created. You may want to encrypt it later.')
try:
# Create a connection
conn = sqlite3.connect(dbfile)
2016-09-05 03:18:21 -05:00
conn.create_function("REGEXP", 2, regexp)
cur = conn.cursor()
# Create table if it doesn't exist
2016-05-24 12:51:38 -05:00
cur.execute("CREATE TABLE if not exists bookmarks \
(id integer PRIMARY KEY, URL text NOT NULL UNIQUE, metadata text default \'\', tags text default \',\', desc text default \'\')")
conn.commit()
except Exception as e:
_, _, linenumber, func, _, _ = inspect.stack()[0]
logger.error('%s(), ln %d: %s', func, linenumber, e)
sys.exit(1)
# Add description column in existing DB (from version 2.1)
try:
2016-05-24 12:51:38 -05:00
cur.execute("ALTER TABLE bookmarks ADD COLUMN desc text default \'\'")
conn.commit()
2016-05-31 12:39:34 -05:00
except Exception:
pass
return (conn, cur)
def get_bookmark_by_index(self, index):
"""Get a bookmark from database by its ID.
Return data as a tuple. Return None if index not found.
"""
self.cur.execute('SELECT * FROM bookmarks WHERE id = ?', (index,))
results = self.cur.fetchall()
if len(results) == 0:
return None
else:
return results[0]
def get_bookmark_index(self, url):
"""Check if URL already exists in DB
Params: URL to search
Returns: DB index if URL found, else -1
"""
2016-05-24 12:51:38 -05:00
self.cur.execute('SELECT id FROM bookmarks WHERE URL = ?', (url,))
resultset = self.cur.fetchall()
if len(resultset) == 0:
return -1
return resultset[0][0]
def add_bookmark(self, url, title_manual=None, tag_manual=None, desc=None, delayed_commit=False):
2016-05-22 14:20:50 -05:00
"""Add a new bookmark
:param url: url to bookmark
:param tag_manual: string of comma-separated tags to add manually
2016-05-22 14:33:24 -05:00
:param title_manual: string title to add manually
:param desc: string description
"""
# Ensure that the URL does not exist in DB already
id = self.get_bookmark_index(url)
if id != -1:
logger.error('URL [%s] already exists at index %d', url, id)
return
# Process title
2016-05-22 14:33:24 -05:00
if title_manual is not None:
meta = title_manual
else:
meta = network_handler(url)
if meta == '':
2016-05-24 12:51:38 -05:00
print('\x1B[91mTitle: []\x1B[0m\n')
logger.debug('Title: [%s]', meta)
# Process tags
if tag_manual is None:
2016-05-29 08:33:58 -05:00
tag_manual = DELIMITER
else:
2016-05-29 08:33:58 -05:00
if tag_manual[0] != DELIMITER:
tag_manual = '%s%s' % (DELIMITER, tag_manual)
2016-05-29 08:33:58 -05:00
if tag_manual[-1] != DELIMITER:
tag_manual = '%s%s' % (tag_manual, DELIMITER)
# Process description
2016-05-22 14:33:24 -05:00
if desc is None:
desc = ''
try:
self.cur.execute('INSERT INTO bookmarks(URL, metadata, tags, desc) VALUES (?, ?, ?, ?)', (url, meta, tag_manual, desc))
if not delayed_commit:
self.conn.commit()
self.print_bookmark(self.cur.lastrowid)
2016-05-22 14:20:50 -05:00
except Exception as e:
_, _, linenumber, func, _, _ = inspect.stack()[0]
logger.error('%s(), ln %d: %s', func, linenumber, e)
def append_tag_at_index(self, index, tag_manual):
2016-06-12 05:30:54 -05:00
""" Append tags for bookmark at index
:param index: int position of record, 0 for all
:tag_manual: string of comma-separated tags to add manually
"""
if index == 0:
resp = input('Tags will be appended for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ')
if resp != 'y':
return
self.cur.execute('SELECT id, tags FROM bookmarks ORDER BY id ASC')
else:
self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,))
2016-06-12 05:30:54 -05:00
resultset = self.cur.fetchall()
for row in resultset:
tags = '%s%s' % (row[1], tag_manual[1:])
tags = parse_tags([tags])
self.cur.execute('UPDATE bookmarks SET tags = ? WHERE id = ?', (tags, row[0],))
self.conn.commit()
def delete_tag_at_index(self, index, tag_manual):
2016-07-03 16:50:44 -05:00
""" Delete tags for bookmark at index
:param index: int position of record, 0 for all
:tag_manual: string of comma-separated tags to delete manually
"""
tags_to_delete = tag_manual.strip(DELIMITER).split(DELIMITER)
if index == 0:
resp = input('Tags will be deleted for ALL bookmarks. Enter \x1b[1my\x1b[21m to confirm: ')
if resp != 'y':
2016-07-03 16:50:44 -05:00
return
for tag in tags_to_delete:
self.cur.execute("SELECT id, tags FROM bookmarks WHERE tags LIKE '%' || ? || '%' ORDER BY id ASC", (tag,))
resultset = self.cur.fetchall()
2016-07-03 16:50:44 -05:00
for row in resultset:
tags = row[1]
2016-07-03 16:50:44 -05:00
tags = tags.replace('%s%s%s' % (DELIMITER, tag, DELIMITER,), DELIMITER)
self.cur.execute('UPDATE bookmarks SET tags = ? WHERE id = ?', (parse_tags([tags]), row[0],))
2016-07-03 16:50:44 -05:00
self.conn.commit()
else:
self.cur.execute('SELECT id, tags FROM bookmarks WHERE id = ?', (index,))
resultset = self.cur.fetchall()
2016-07-03 16:50:44 -05:00
for row in resultset:
tags = row[1]
2016-07-03 16:50:44 -05:00
for tag in tags_to_delete:
tags = tags.replace('%s%s%s' % (DELIMITER, tag, DELIMITER,), DELIMITER)
self.cur.execute('UPDATE bookmarks SET tags = ? WHERE id = ?', (parse_tags([tags]), row[0],))
self.conn.commit()
2016-07-03 16:50:44 -05:00
2016-06-12 05:30:54 -05:00
def update_bookmark(self, index, url='', title_manual=None,
2016-07-03 16:50:44 -05:00
tag_manual=None, desc=None, append_tag=False, delete_tag=False):
""" Update an existing record at index
Update all records if index is 0 and url is not specified.
URL is an exception because URLs are unique in DB.
2016-06-12 05:30:54 -05:00
:param index: int position to update, 0 for all
:param url: address
:param tag_manual: string of comma-separated tags to add manually
:param title_manual: string title to add manually
:param desc: string description
:return:
"""
arguments = []
2016-05-24 12:51:38 -05:00
query = 'UPDATE bookmarks SET'
to_update = False
# Update URL if passed as argument
if url != '':
if index == 0:
logger.error('All URLs cannot be same')
return
query = '%s URL = ?,' % query
arguments += (url,)
to_update = True
# Update tags if passed as argument
if tag_manual is not None:
2016-06-12 05:30:54 -05:00
if append_tag:
self.append_tag_at_index(index, tag_manual)
2016-07-03 16:50:44 -05:00
elif delete_tag:
self.delete_tag_at_index(index, tag_manual)
2016-06-12 05:30:54 -05:00
else:
query = '%s tags = ?,' % query
arguments += (tag_manual,)
to_update = True
# Update description if passed as an argument
if desc is not None:
query = '%s desc = ?,' % query
arguments += (desc,)
to_update = True
# Update title
#
# 1. if -t has no arguments, delete existing title
# 2. if -t has arguments, update existing title
# 3. if -t option is omitted at cmdline:
# if URL is passed, update the title from web using the URL
# 4. if no other argument (url, tag, comment) passed update title from web using DB URL
meta = None
if title_manual is not None:
meta = title_manual
elif url != '':
meta = network_handler(url)
if meta == '':
2016-05-24 12:51:38 -05:00
print('\x1B[91mTitle: []\x1B[0m')
logger.debug('Title: [%s]', meta)
2016-07-11 12:18:40 -05:00
elif not to_update and not (append_tag or delete_tag):
self.refreshdb(index)
if index > 0:
self.print_bookmark(index)
return
if meta is not None:
query = '%s metadata = ?,' % query
arguments += (meta,)
to_update = True
if not to_update: # Nothing to update
return
if index == 0: # Update all records
resp = input('ALL bookmarks will be updated. Enter \x1b[1my\x1b[21m to confirm: ')
if resp != 'y':
return
query = query[:-1]
else:
query = '%s WHERE id = ?' % query[:-1]
arguments += (index,)
logger.debug('query: "%s", args: %s', query, arguments)
try:
self.cur.execute(query, arguments)
self.conn.commit()
if self.cur.rowcount == 1:
self.print_bookmark(index)
elif self.cur.rowcount == 0:
logger.error('No matching index')
except sqlite3.IntegrityError:
2016-05-24 12:51:38 -05:00
print('URL already exists')
def refreshdb(self, index):
"""Refresh ALL records in the database. Fetch title for each
bookmark from the web and update the records. Doesn't udpate
the record if title is empty.
This API doesn't change DB index, URL or tags of a bookmark.
2016-05-23 14:16:21 -05:00
:param index: index of record to update, or 0 for all records
"""
if index == 0:
2016-05-24 12:51:38 -05:00
self.cur.execute('SELECT id, url FROM bookmarks ORDER BY id ASC')
else:
2016-05-24 12:51:38 -05:00
self.cur.execute('SELECT id, url FROM bookmarks WHERE id = ?', (index,))
resultset = self.cur.fetchall()
for row in resultset:
title = network_handler(row[1])
if title == '':
print('\x1b[1mIndex %d: empty title\x1b[21m\x1B[0m\n' % row[0])
continue
else:
print('Title: [%s]' % title)
self.cur.execute('UPDATE bookmarks SET metadata = ? WHERE id = ?', (title, row[0],))
print('Index %d updated\n' % row[0])
if interrupted:
logger.warning('^C pressed. Aborting DB refresh...')
break
self.conn.commit()
def searchdb(self, keywords, all_keywords=False, delete=False, deep=False):
"""Search the database for an entries with tags or URL
or title info matching keywords and list those.
2016-05-23 14:16:21 -05:00
:param keywords: keywords to search
:param all_keywords: search any or all keywords
:param delete: delete search results
:param deep: search for matching substrings
"""
arguments = []
placeholder = "'%' || ? || '%'"
query = 'SELECT id, url, metadata, tags, desc FROM bookmarks WHERE'
2016-05-31 12:39:34 -05:00
if all_keywords: # Match all keywords in URL or Title
for token in keywords:
if not deep:
2016-09-05 03:18:21 -05:00
token = '\\b' + token + '\\b'
query = '%s (tags REGEXP ? OR URL REGEXP ? OR metadata REGEXP ? OR desc REGEXP ?) AND' % (query)
else:
query = '%s (tags LIKE (%s) OR URL LIKE (%s) OR metadata LIKE (%s) OR desc LIKE (%s)) AND' % (query, placeholder, placeholder, placeholder, placeholder)
arguments += (token, token, token, token)
query = query[:-4]
2016-05-31 12:39:34 -05:00
else: # Match any keyword in URL or Title
for token in keywords:
if not deep:
2016-09-05 03:18:21 -05:00
token = '\\b' + token + '\\b'
query = '%s tags REGEXP ? OR URL REGEXP ? OR metadata REGEXP ? OR desc REGEXP ? OR' % (query)
else:
query = '%s tags LIKE (%s) OR URL LIKE (%s) OR metadata LIKE (%s) OR desc LIKE (%s) OR' % (query, placeholder, placeholder, placeholder, placeholder)
arguments += (token, token, token, token)
query = query[:-3]
query = '%s ORDER BY id ASC' % query
logger.debug('query: "%s", args: %s', query, arguments)
self.cur.execute(query, arguments)
results = self.cur.fetchall()
if len(results) == 0:
return
2016-06-29 13:06:33 -05:00
if not self.json:
2016-07-10 10:45:43 -05:00
prompt(results, self.noninteractive, delete)
else:
2016-06-29 13:22:07 -05:00
print(format_json(results, showOpt=self.showOpt))
def search_by_tag(self, tag, delete=False):
"""Search and list bookmarks with a tag
2016-05-23 14:27:57 -05:00
:param tag: tag to search
"""
query = "SELECT id, url, metadata, tags, desc FROM bookmarks WHERE tags LIKE '%' || ? || '%' ORDER BY id ASC"
logger.debug('query: "%s", args: %s', query, tag)
self.cur.execute(query, (tag,))
results = self.cur.fetchall()
if len(results) == 0:
return
2016-06-29 13:06:33 -05:00
if not self.json:
2016-07-10 10:45:43 -05:00
prompt(results, self.noninteractive, delete)
else:
2016-06-29 13:22:07 -05:00
print(format_json(results, showOpt=self.showOpt))
def compactdb(self, index):
"""When an entry at index is deleted, move the last
entry in DB to index, if index is lesser.
Params: index of deleted entry
"""
self.cur.execute('SELECT MAX(id) from bookmarks')
results = self.cur.fetchall()
2016-05-31 12:39:34 -05:00
if len(results) == 1 and results[0][0] is None: # Return if the last index was just deleted
return
for row in results:
if row[0] > index:
self.cur.execute('SELECT id, URL, metadata, tags, desc FROM bookmarks WHERE id = ?', (row[0],))
results = self.cur.fetchall()
for row in results:
self.cur.execute('DELETE FROM bookmarks WHERE id = ?', (row[0],))
self.conn.commit()
self.cur.execute('INSERT INTO bookmarks(id, URL, metadata, tags, desc) VALUES (?, ?, ?, ?, ?)', (index, row[1], row[2], row[3], row[4],))
self.conn.commit()
2016-05-24 12:51:38 -05:00
print('Index %d moved to %d' % (row[0], index))
def delete_bookmark(self, index, low=0, high=0, is_range=False):
"""Delete a single record or remove the table if index is None
Params: index to delete
"""
2016-06-16 16:08:38 -05:00
if is_range:
try:
self.cur.execute('DELETE from bookmarks where id BETWEEN ? AND ?', (low, high))
self.conn.commit()
print('Bookmarks from index %s to %s deleted' % (low, high))
except IndexError:
logger.error('Index out of bound')
2016-06-16 16:08:38 -05:00
2016-06-16 17:06:51 -05:00
for index in range(low, high + 1):
2016-06-16 16:08:38 -05:00
self.compactdb(index)
elif index == 0: # Remove the table
resp = input('ALL bookmarks will be removed. Enter \x1b[1my\x1b[21m to confirm: ')
if resp != 'y':
print('No bookmarks deleted')
return
2016-08-21 18:09:07 -05:00
self.delete_all_bookmarks()
2016-05-24 12:51:38 -05:00
print('All bookmarks deleted')
2016-05-31 12:39:34 -05:00
else: # Remove a single entry
try:
self.cur.execute('DELETE FROM bookmarks WHERE id = ?', (index,))
self.conn.commit()
if self.cur.rowcount == 1:
2016-05-24 12:51:38 -05:00
print('Removed index %d' % index)
self.compactdb(index)
else:
logger.error('No matching index')
except IndexError:
logger.error('Index out of bound')
return True
def delete_all_bookmarks(self):
2016-08-21 18:09:07 -05:00
"""Should only be called inside of delete_bookmark
Drops the bookmark table if it exists
"""
self.cur.execute('DROP TABLE if exists bookmarks')
self.conn.commit()
def print_bookmark(self, index, empty=False):
2016-06-24 16:56:18 -05:00
"""Print bookmark details at index or all bookmarks if index is 0
Print only bookmarks with blank title or tag if empty is True
Note: URL is printed on top because title may be blank
2016-06-12 05:30:54 -05:00
Params: index to print (0 for all)
empty flag to show only bookmarks with no title or tags
"""
2016-05-31 12:39:34 -05:00
if index == 0: # Show all entries
if not empty:
self.cur.execute('SELECT * FROM bookmarks')
resultset = self.cur.fetchall()
else:
2016-05-29 08:33:58 -05:00
self.cur.execute("SELECT * FROM bookmarks WHERE metadata = '' OR tags = ?", (DELIMITER,))
resultset = self.cur.fetchall()
print('\x1b[1m%s records found\x1b[21m\n' % len(resultset))
2016-06-29 13:06:33 -05:00
if not self.json:
2016-06-29 13:22:07 -05:00
if self.showOpt == 0:
for row in resultset:
print_record(row)
2016-06-29 13:22:07 -05:00
elif self.showOpt == 1:
for row in resultset:
2016-06-02 15:46:05 -05:00
print('%s\t%s' % (row[0], row[1]))
2016-06-29 13:22:07 -05:00
elif self.showOpt == 2:
for row in resultset:
2016-06-02 15:46:05 -05:00
print('%s\t%s\t%s' % (row[0], row[1], row[3][1:-1]))
else:
2016-06-29 13:22:07 -05:00
print(format_json(resultset, showOpt=self.showOpt))
else: # Show record at index
try:
2016-05-24 12:51:38 -05:00
self.cur.execute('SELECT * FROM bookmarks WHERE id = ?', (index,))
results = self.cur.fetchall()
if len(results) == 0:
logger.error('No matching index')
return
except IndexError:
logger.error('Index out of bound')
return
2016-06-29 13:06:33 -05:00
if not self.json:
for row in results:
2016-06-29 13:22:07 -05:00
if self.showOpt == 0:
print_record(row)
2016-06-29 13:22:07 -05:00
elif self.showOpt == 1:
2016-06-02 15:46:05 -05:00
print('%s\t%s' % (row[0], row[1]))
2016-06-29 13:22:07 -05:00
elif self.showOpt == 2:
2016-06-02 15:46:05 -05:00
print('%s\t%s\t%s' % (row[0], row[1], row[3][1:-1]))
else:
2016-06-29 13:22:07 -05:00
print(format_json(results, True, self.showOpt))
def list_tags(self):
"""Print all unique tags ordered alphabetically"""
count = 1
Tags = []
uniqueTags = []
for row in self.cur.execute('SELECT DISTINCT tags FROM bookmarks ORDER BY tags'):
tagset = row[0].strip(DELIMITER).split(DELIMITER)
for tag in tagset:
if tag not in Tags:
Tags += (tag,)
if Tags[0] == '':
uniqueTags = sorted(Tags[1:], key=str.lower)
else:
uniqueTags = sorted(Tags, key=str.lower)
for tag in uniqueTags:
2016-05-24 12:51:38 -05:00
print('%6d. %s' % (count, tag))
count += 1
def replace_tag(self, orig, new=None):
"""Replace orig tags with new tags in DB for all records.
2016-06-07 10:56:22 -05:00
Remove orig tag if new tag is empty.
Params: original and new tags
"""
update = False
delete = False
2016-05-29 08:33:58 -05:00
newtags = DELIMITER
orig = '%s%s%s' % (DELIMITER, orig, DELIMITER)
if new is None:
delete = True
else:
newtags = parse_tags(new)
2016-05-29 08:33:58 -05:00
if newtags == DELIMITER:
delete = True
if orig == newtags:
2016-05-24 12:51:38 -05:00
print('Tags are same.')
return
self.cur.execute('SELECT id, tags FROM bookmarks WHERE tags LIKE ?', ('%' + orig + '%',))
results = self.cur.fetchall()
for row in results:
2016-05-31 12:39:34 -05:00
if not delete:
# Check if tag newtags is already added
if row[1].find(newtags) >= 0:
2016-05-29 08:33:58 -05:00
newtags = DELIMITER
2016-05-24 13:28:14 -05:00
tags = row[1].replace(orig, newtags)
2016-06-07 10:56:22 -05:00
tags = parse_tags([tags])
2016-05-24 13:28:14 -05:00
self.cur.execute('UPDATE bookmarks SET tags = ? WHERE id = ?', (tags, row[0],))
2016-05-24 12:51:38 -05:00
print('Index %d updated' % row[0])
update = True
if update:
self.conn.commit()
def browse_by_index(self, index):
"""Open URL at index in browser
Params: index
"""
try:
2016-05-24 12:51:38 -05:00
for row in self.cur.execute('SELECT URL FROM bookmarks WHERE id = ?', (index,)):
url = unquote(row[0])
browser_open(url)
return
logger.error('No matching index')
except IndexError:
logger.error('Index out of bound')
2016-05-29 01:09:51 -05:00
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:
logger.error(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):
2016-05-22 16:03:47 -05:00
"""Import bookmarks from a html file.
Supports Firefox, Google Chrome and IE imports
Params: Path to file to import
2016-05-22 16:03:47 -05:00
"""
try:
import bs4
with open(fp, mode='r', encoding='utf-8') as f:
soup = bs4.BeautifulSoup(f, 'html.parser')
except ImportError:
logger.error('Beautiful Soup not found')
2016-06-02 13:22:36 -05:00
return
except Exception as e:
logger.error(e)
2016-06-02 13:22:36 -05:00
return
2016-05-22 16:03:47 -05:00
html_tags = soup.findAll('a')
for tag in html_tags:
# Extract comment from <dd> tag
desc = None
comment_tag = tag.findNextSibling('dd')
if comment_tag:
desc = comment_tag.text[0:comment_tag.text.find('\n')]
self.add_bookmark(tag['href'],
tag.string,
('%s%s%s' % (DELIMITER, tag['tags'], DELIMITER)) if tag.has_attr('tags') else None,
desc,
True)
self.conn.commit()
f.close()
2016-05-29 01:09:51 -05:00
def mergedb(self, fp):
"""Merge bookmarks from another Buku database file
Params: Path to file to merge
"""
try:
# Connect to input DB
if sys.version_info >= (3,4,4):
# Python 3.4.4 and above
connfp = sqlite3.connect('file:%s?mode=ro' % fp, uri=True)
else:
connfp = sqlite3.connect(fp)
2016-05-29 01:09:51 -05:00
curfp = connfp.cursor()
except Exception as e:
logger.error(e)
return
2016-05-29 01:09:51 -05:00
curfp.execute('SELECT * FROM bookmarks')
resultset = curfp.fetchall()
for row in resultset:
self.add_bookmark(row[1], row[2], row[3], row[4], True)
self.conn.commit()
2016-05-29 01:09:51 -05:00
try:
curfp.close()
connfp.close()
2016-05-31 12:39:34 -05:00
except Exception:
2016-05-29 01:09:51 -05:00
pass
2016-05-22 16:03:47 -05:00
def close_quit(self, exitval=0):
"""Close a DB connection and exit"""
if self.conn is not None:
try:
self.cur.close()
self.conn.close()
except Exception: # we don't really care about errors, we're closing down anyway
pass
sys.exit(exitval)
2016-05-22 16:03:47 -05:00
# Generic functions
def connect_server(url):
2016-04-05 06:25:40 -05:00
"""Connect to a server and fetch the requested page data.
Supports gzip compression.
2016-04-10 07:41:00 -05:00
Params: URL to fetch
2016-04-05 06:25:40 -05:00
Returns: connection, HTTP(S) GET response
"""
2016-05-24 12:51:38 -05:00
if url.find('%20') != -1:
url = unquote(url).replace(' ', '%20')
else:
url = unquote(url)
logger.debug('unquoted: %s', url)
2016-05-31 12:39:34 -05:00
if url.find('https://') >= 0: # Secure connection
server = url[8:]
2016-05-24 12:51:38 -05:00
marker = server.find('/')
if marker > 0:
url = server[marker:]
server = server[:marker]
else: # Handle domain name without trailing /
url = '/'
urlconn = HTTPSConnection(server, timeout=30)
2016-05-31 12:39:34 -05:00
elif url.find('http://') >= 0: # Insecure connection
server = url[7:]
2016-05-24 12:51:38 -05:00
marker = server.find('/')
if marker > 0:
url = server[marker:]
server = server[:marker]
else:
url = '/'
urlconn = HTTPConnection(server, timeout=30)
else:
logger.warning('Not a valid HTTP(S) url')
2016-05-24 12:51:38 -05:00
if url.find(':') == -1:
logger.warning("Doesn't appear to be a valid url either")
return (None, None)
logger.debug('server [%s] rel [%s]', server, url)
# Handle URLs passed with %xx escape
try:
url.encode('ascii')
2016-05-31 12:39:34 -05:00
except Exception:
url = quote(url)
2016-05-24 12:51:38 -05:00
urlconn.request('GET', url, None, {
'Accept-encoding': 'gzip',
2016-06-12 05:30:54 -05:00
'DNT': '1',
})
return (urlconn, urlconn.getresponse())
def get_page_title(resp):
2016-04-05 06:25:40 -05:00
"""Invoke HTML parser and extract title from HTTP response
2016-04-10 07:41:00 -05:00
Params: GET response and invoke HTML parser
2016-04-05 06:25:40 -05:00
"""
data = None
2016-04-05 06:25:40 -05:00
charset = resp.headers.get_content_charset()
if resp.headers.get('Content-Encoding') == 'gzip':
payload = resp.read()
logger.debug('gzip response')
data = gzip.decompress(payload)
else:
data = resp.read()
2016-05-31 12:39:34 -05:00
if charset is None:
logger.warning('Charset missing in response')
2016-04-05 06:25:40 -05:00
charset = 'utf-8'
2016-04-14 09:04:02 -05:00
logger.debug('charset: %s', charset)
2016-04-05 06:25:40 -05:00
parser = BMHTMLParser()
try:
if charset == 'utf-8':
2016-05-24 12:51:38 -05:00
parser.feed(data.decode(charset, 'replace'))
2016-04-05 06:25:40 -05:00
else:
parser.feed(data.decode(charset))
2016-04-05 06:25:40 -05:00
except Exception as e:
# Suppress Exception due to intentional self.reset() in HTMLParser
if logger.isEnabledFor(logging.DEBUG) and str(e) != 'we should not get here!':
_, _, linenumber, func, _, _ = inspect.stack()[0]
logger.error('%s(), ln %d: %s', func, linenumber, e)
2016-04-05 06:25:40 -05:00
def network_handler(url):
2016-04-05 06:25:40 -05:00
"""Handle server connection and redirections
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: URL to fetch
Returns: page title or empty string, if not found
"""
global titleData
titleData = None
urlconn = None
retry = False
try:
urlconn, resp = connect_server(url)
2016-05-31 12:39:34 -05:00
while True:
if resp is None:
break
elif resp.status == 200:
get_page_title(resp)
break
elif resp.status in [301, 302]:
redirurl = urljoin(url, resp.getheader('location', ''))
logger.debug('REDIRECTION: %s', redirurl)
retry = False # Reset retry, start fresh on redirection
2016-05-24 12:51:38 -05:00
if redirurl.find('sorry/IndexRedirect?') >= 0: # gracefully handle Google blocks
logger.error('Connection blocked due to unusual activity')
break
2016-05-24 12:51:38 -05:00
marker = redirurl.find('redirectUrl=')
if marker != -1:
redirurl = redirurl[marker + 12:]
# break same URL redirection loop
if url == redirurl:
logger.error('Detected repeated redirection to same URL')
break
url = redirurl
urlconn.close()
2016-04-09 09:10:30 -05:00
# Try with complete URL on redirection
urlconn, resp = connect_server(url)
2016-05-31 12:39:34 -05:00
elif resp.status == 403 and not retry:
"""Handle URLs of the form https://www.domain.com or
https://www.domain.com/ which fails when trying to fetch
resource '/', retry with full path.
"""
urlconn.close()
logger.debug('Received status 403: retrying...')
# Remove trailing /
if url[-1] == '/':
url = url[:-1]
urlconn, resp = connect_server(url)
retry = True
#elif resp.status == 500 and not retry:
#"""Retry on status 500 (Internal Server Error) with truncated
#URL. Some servers support truncated request URL on redirection.
#"""
#urlconn.close()
#logger.debug('Received status 500: retrying...')
#urlconn, resp = connect_server(url)
#retry = True
else:
logger.error('[%s] %s', resp.status, resp.reason)
break
except Exception as e:
_, _, linenumber, func, _, _ = inspect.stack()[0]
logger.error('%s(), ln %d: %s', func, linenumber, e)
finally:
if urlconn is not None:
urlconn.close()
if titleData is None:
return ''
2016-05-31 12:39:34 -05:00
return titleData.strip().replace('\n', '')
2016-05-31 12:39:34 -05:00
def parse_tags(keywords=None):
2016-05-18 22:24:46 -05:00
"""Format and get tag string from tokens"""
2016-05-31 12:39:34 -05:00
if keywords is None:
2016-05-31 16:40:51 -05:00
return None
2016-05-31 12:39:34 -05:00
2016-05-29 08:33:58 -05:00
tags = DELIMITER
origTags = []
uniqueTags = []
2016-05-18 22:24:46 -05:00
# Cleanse and get the tags
tagstr = ' '.join(keywords)
marker = tagstr.find(DELIMITER)
while marker >= 0:
token = tagstr[0:marker]
2016-05-31 12:39:34 -05:00
tagstr = tagstr[marker + 1:]
marker = tagstr.find(DELIMITER)
token = token.strip()
if token == '':
continue
tags = '%s%s%s' % (tags, token, DELIMITER)
tagstr = tagstr.strip()
if tagstr != '':
tags = '%s%s%s' % (tags, tagstr, DELIMITER)
2016-05-18 22:24:46 -05:00
logger.debug('keywords: %s', keywords)
logger.debug('parsed tags: [%s]', tags)
2016-05-18 22:24:46 -05:00
2016-05-29 08:33:58 -05:00
if tags == DELIMITER:
return tags
2016-06-03 19:21:52 -05:00
origTags += tags.strip(DELIMITER).split(DELIMITER)
for tag in origTags:
if tag not in uniqueTags:
uniqueTags += (tag, ) # Select unique tags
# Sort the tags
sortedTags = sorted(uniqueTags, key=str.lower)
# Wrap with delimiter
return '%s%s%s' % (DELIMITER, DELIMITER.join(sortedTags), DELIMITER)
2016-05-18 22:24:46 -05:00
def prompt(results, noninteractive=False, delete=False):
"""Show each matching result from a search and prompt"""
2016-05-18 10:46:08 -05:00
count = 0
for row in results:
count += 1
print_record(row, count)
2016-05-18 10:46:08 -05:00
if delete:
resp = input('Delete the search results? Enter \x1b[1my\x1b[21m to confirm: ')
if resp != 'y'and resp != 'Y':
2016-05-18 10:46:08 -05:00
return
# delete records in reverse order
pos = len(results)-1;
while(pos >= 0):
idx = results[pos][0]
bdb.delete_bookmark(idx)
pos = pos -1
# prompt and open
else:
if noninteractive:
return
2016-05-18 10:46:08 -05:00
while True:
2016-05-18 10:46:08 -05:00
try:
2016-09-05 11:12:50 -05:00
nav = input('Results, ranges (x-y,(a)ll) to open: ')
if not nav:
2016-09-05 11:12:50 -05:00
nav = input('Results, ranges (x-y,(a)ll) to open: ')
if not nav:
# Quit on double enter
break
except EOFError:
return
# open all results and re-prompt if 'a' is pressed
if nav == 'a':
for index in range(0, count):
try:
browser_open(unquote(results[index][1]))
except Exception as e:
_, _, linenumber, func, _, _ = inspect.stack()[0]
logger.error('%s(), ln %d: %s', func, linenumber, e)
continue
2016-09-04 21:08:26 -05:00
# iterate over whitespace separated indices
for nav in (' '.join(nav.split())).split():
if is_int(nav):
index = int(nav) - 1
if index < 0 or index >= count:
logger.error('Index out of bound')
continue
try:
browser_open(unquote(results[index][1]))
except Exception as e:
_, _, linenumber, func, _, _ = inspect.stack()[0]
logger.error('%s(), ln %d: %s', func, linenumber, e)
2016-09-04 21:08:26 -05:00
elif '-' in nav and is_int(nav.split('-')[0]) and is_int(nav.split('-')[1]):
lower = int(nav.split('-')[0])
upper = int(nav.split('-')[1])
for index in range(lower-1,upper):
try:
browser_open(unquote(results[index][1]))
except Exception as e:
_, _, linenumber, func, _, _ = inspect.stack()[0]
logger.error('%s(), ln %d: %s', func, linenumber, e)
else:
break
2016-05-18 10:46:08 -05:00
def print_record(row, count=0):
2016-05-17 15:11:31 -05:00
"""Print a single DB record
Handles differently for search and print (count = 0)
"""
# Print index and URL
if count != 0:
2016-06-18 10:21:34 -05:00
printstr = '\x1B[1m\x1B[93m%d. \x1B[0m\x1B[92m%s\x1B[0m \x1B[1m[%s]\x1B[0m\n' % (count, row[1], row[0])
2016-05-17 15:11:31 -05:00
else:
2016-06-02 12:26:37 -05:00
printstr = '\x1B[1m\x1B[93m%d. \x1B[0m\x1B[92m%s\x1B[0m\n' % (row[0], row[1])
# Print title
if row[2] != '':
printstr = '%s \x1B[91m>\x1B[0m %s\n' % (printstr, row[2])
# Print description
if row[4] != '':
printstr = '%s \x1B[91m+\x1B[0m %s\n' % (printstr, row[4])
# Print tags IF not default (DELIMITER)
if row[3] != DELIMITER:
printstr = '%s \x1B[91m#\x1B[0m %s\n' % (printstr, row[3][1:-1])
print(printstr)
2016-05-17 15:11:31 -05:00
2016-06-29 13:22:07 -05:00
def format_json(resultset, single=False, showOpt=0):
2016-05-16 09:39:01 -05:00
"""Return results in Json format"""
2016-05-31 12:39:34 -05:00
if not single:
2016-03-22 18:29:45 -05:00
marks = []
for row in resultset:
if showOpt == 1:
2016-05-31 12:39:34 -05:00
record = {'uri': row[1]}
2016-03-22 18:29:45 -05:00
elif showOpt == 2:
2016-05-31 12:39:34 -05:00
record = {'uri': row[1], 'tags': row[3][1:-1]}
2016-03-22 18:29:45 -05:00
else:
2016-05-31 12:39:34 -05:00
record = {'uri': row[1], 'title': row[2], 'description': row[4], 'tags': row[3][1:-1]}
2016-03-22 18:29:45 -05:00
marks.append(record)
else:
marks = {}
for row in resultset:
if showOpt == 1:
marks['uri'] = row[1]
2016-03-22 18:29:45 -05:00
elif showOpt == 2:
marks['uri'] = row[1]
2016-05-31 12:39:34 -05:00
marks['tags'] = row[3][1:-1]
2016-03-22 18:29:45 -05:00
else:
2016-05-31 12:39:34 -05:00
marks['uri'] = row[1]
2016-03-22 18:29:45 -05:00
marks['title'] = row[2]
marks['description'] = row[4]
2016-05-31 12:39:34 -05:00
marks['tags'] = row[3][1:-1]
2016-03-22 18:29:45 -05:00
return json.dumps(marks, sort_keys=True, indent=4)
2016-05-16 09:39:01 -05:00
def is_int(string):
2016-04-05 06:25:40 -05:00
"""Check if a string is a digit
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: string
"""
try:
int(string)
return True
2016-05-31 12:39:34 -05:00
except Exception:
return False
def browser_open(url):
"""Duplicate stdin, stdout (to suppress showing errors
on the terminal) and open URL in default browser
Params: url to open
"""
url = url.replace('%22', '\"')
_stderr = os.dup(2)
os.close(2)
_stdout = os.dup(1)
os.close(1)
fd = os.open(os.devnull, os.O_RDWR)
os.dup2(fd, 2)
os.dup2(fd, 1)
try:
webbrowser.open(url)
except Exception as e:
_, _, linenumber, func, _, _ = inspect.stack()[0]
logger.error('%s(), ln %d: %s', func, linenumber, e)
finally:
os.close(fd)
os.dup2(_stderr, 2)
os.dup2(_stdout, 1)
def sigint_handler(signum, frame):
"""Custom SIGINT handler"""
global interrupted
interrupted = True
print('\nInterrupted.', file=sys.stderr)
sys.exit(1)
signal.signal(signal.SIGINT, sigint_handler)
2016-09-05 03:18:21 -05:00
def regexp(expr, item):
return re.search(expr, item, re.IGNORECASE) is not None
2016-05-24 11:00:54 -05:00
# Custom Action classes for argparse
class CustomUpdateAction(argparse.Action):
2016-05-18 12:23:08 -05:00
"""Class to capture if optional param 'update'
is actually used, even if sans arguments
"""
2016-04-25 11:23:03 -05:00
def __call__(self, parser, args, values, option_string=None):
global update
update = True
# NOTE: the following converts a None argument to an empty array []
setattr(args, self.dest, values)
2016-04-26 12:23:48 -05:00
class CustomTagAction(argparse.Action):
2016-05-18 22:24:46 -05:00
"""Class to capture if optional param 'tag'
2016-05-18 12:23:08 -05:00
is actually used, even if sans arguments
"""
def __call__(self, parser, args, values, option_string=None):
2016-05-18 22:24:46 -05:00
global tagManual
2016-05-18 12:23:08 -05:00
2016-05-31 12:39:34 -05:00
tagManual = [DELIMITER, ]
2016-05-18 12:23:08 -05:00
setattr(args, self.dest, values)
class CustomTitleAction(argparse.Action):
2016-05-18 12:23:08 -05:00
"""Class to capture if optional param 'title'
is actually used, even if sans arguments
"""
def __call__(self, parser, args, values, option_string=None):
global titleManual
titleManual = ''
setattr(args, self.dest, values)
class CustomDescAction(argparse.Action):
2016-05-18 12:23:08 -05:00
"""Class to capture if optional param 'comment'
2016-05-17 15:11:31 -05:00
is actually used, even if sans arguments
"""
def __call__(self, parser, args, values, option_string=None):
global description
description = ''
setattr(args, self.dest, values)
class CustomTagSearchAction(argparse.Action):
2016-05-18 22:24:46 -05:00
"""Class to capture if optional param 'stag'
is actually used, even if sans arguments
"""
def __call__(self, parser, args, values, option_string=None):
global tagsearch
tagsearch = True
setattr(args, self.dest, values)
2016-04-24 14:19:32 -05:00
class ExtendedArgumentParser(argparse.ArgumentParser):
"""Extend classic argument parser"""
2016-04-24 14:19:32 -05:00
2016-04-25 15:45:10 -05:00
# Print additional help and info
@staticmethod
def print_extended_help(file=None):
2016-05-14 10:33:21 -05:00
file.write('''
prompt keys:
1-N open the Nth search result in web browser
2016-09-05 03:18:21 -05:00
ranges, space-separated result indices work
2016-05-24 11:09:59 -05:00
double Enter exit buku
2016-05-14 10:33:21 -05:00
2016-07-03 05:06:50 -05:00
symbols:
> title
+ comment
# tags
2016-05-22 12:50:19 -05:00
Version %s
2016-05-14 10:33:21 -05:00
Copyright (C) 2015-2016 Arun Prakash Jana <engineerarun@gmail.com>
License: GPLv3
2016-08-13 14:31:35 -05:00
Webpage: https://github.com/jarun/Buku
2016-05-14 10:33:21 -05:00
''' % _VERSION_)
2016-04-25 15:45:10 -05:00
# Help
2016-04-24 14:19:32 -05:00
def print_help(self, file=None):
super(ExtendedArgumentParser, self).print_help(file)
2016-04-25 15:45:10 -05:00
self.print_extended_help(file)
2016-04-05 23:55:25 -05:00
"""main starts here"""
2016-04-26 12:23:48 -05:00
2016-05-31 12:39:34 -05:00
2016-04-26 12:23:48 -05:00
# Handle piped input
2016-06-30 09:19:57 -05:00
def main(argv, pipeargs = None):
if not sys.stdin.isatty():
2016-05-31 12:39:34 -05:00
pipeargs.extend(argv)
for s in sys.stdin.readlines():
pipeargs.extend(s.split())
2016-05-24 12:51:38 -05:00
if __name__ == '__main__':
2016-06-30 09:19:57 -05:00
pipeargs = []
atexit.register(logging.shutdown)
2016-06-30 09:19:57 -05:00
try:
2016-06-30 09:19:57 -05:00
main(sys.argv, pipeargs)
except KeyboardInterrupt:
pass
# If piped input, set argument vector
if len(pipeargs) > 0:
sys.argv = pipeargs
# Setup custom argument parser
argparser = ExtendedArgumentParser(
2016-06-22 09:48:45 -05:00
description='A powerful command-line bookmark manager. Your mini web!',
formatter_class=argparse.RawTextHelpFormatter,
2016-06-08 11:57:50 -05:00
usage='''buku [OPTIONS] [KEYWORD [KEYWORD ...]]''',
add_help=False
)
# General options
2016-05-31 12:39:34 -05:00
general_group = argparser.add_argument_group(
title='general options',
description='''-a, --add URL [tags ...]
2016-05-25 12:15:52 -05:00
bookmark URL with comma-separated tags
-u, --update [N] update fields of bookmark at DB index N
refresh all titles, if no arguments
refresh title of bookmark at N, if only
N is specified without any edit options
2016-06-16 16:08:38 -05:00
-d, --delete [...] delete bookmarks. Valid inputs: either
a hyphenated single range (100-200),
OR space-separated indices (100 15 200)
2016-07-10 10:45:43 -05:00
delete search results with search options
2016-05-25 12:15:52 -05:00
delete all bookmarks, if no arguments
-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)
2016-06-16 16:08:38 -05:00
general_group.add_argument('-d', '--delete', nargs='*', dest='delete', metavar='N', help=argparse.SUPPRESS)
general_group.add_argument('-h', '--help', dest='help', action='store_true', help=argparse.SUPPRESS)
# Edit options
2016-05-31 12:39:34 -05:00
edit_group = argparser.add_argument_group(
title='edit options',
description='''--url keyword specify url, works with -u only
2016-07-03 16:50:44 -05:00
--tag [+|-] [...] set comma-separated tags, works with -a, -u
2016-07-11 12:18:40 -05:00
clear tags, if no arguments
append specified tags, if preceded by '+'
remove specified tags, if preceded by '-'
2016-05-25 12:15:52 -05:00
-t, --title [...] manually set title, works with -a, -u
if no arguments:
-a: do not set title, -u: clear title
-c, --comment [...] description of the bookmark, works with
-a, -u; clears comment, if no arguments''')
edit_group.add_argument('--url', nargs=1, dest='url', metavar='url', help=argparse.SUPPRESS)
edit_group.add_argument('--tag', nargs='*', dest='tag', action=CustomTagAction, metavar='tag', help=argparse.SUPPRESS)
edit_group.add_argument('-t', '--title', nargs='*', dest='title', action=CustomTitleAction, metavar='title', help=argparse.SUPPRESS)
edit_group.add_argument('-c', '--comment', nargs='*', dest='desc', type=str, action=CustomDescAction, metavar='desc', help=argparse.SUPPRESS)
# Search options
2016-05-31 12:39:34 -05:00
search_group = argparser.add_argument_group(
title='search options',
description='''-s, --sany keyword [...]
2016-05-25 12:15:52 -05:00
search bookmarks for ANY matching keyword
-S, --sall keyword [...]
search bookmarks with ALL keywords
special keyword -
"blank": list entries with empty title/tag
--deep match substrings ('pen' matches 'opened')
2016-05-25 12:15:52 -05:00
--st, --stag [...] search bookmarks by tag
2016-06-08 11:57:50 -05:00
list tags alphabetically, if no arguments''')
search_group.add_argument('-s', '--sany', nargs='+', metavar='keyword', help=argparse.SUPPRESS)
search_group.add_argument('-S', '--sall', nargs='+', metavar='keyword', help=argparse.SUPPRESS)
search_group.add_argument('--deep', dest='deep', action='store_true', help=argparse.SUPPRESS)
search_group.add_argument('--st', '--stag', nargs='*', dest='stag', action=CustomTagSearchAction, metavar='keyword', help=argparse.SUPPRESS)
# Encryption options
2016-05-31 12:39:34 -05:00
crypto_group = argparser.add_argument_group(
title='encryption options',
description='''-l, --lock [N] encrypt DB file with N (> 0, default 8)
2016-05-25 12:15:52 -05:00
hash iterations to generate key
-k, --unlock [N] decrypt DB file with N (> 0, default 8)
hash iterations to generate key''')
crypto_group.add_argument('-k', '--unlock', nargs='?', dest='decrypt', type=int, const=8, metavar='N', help=argparse.SUPPRESS)
crypto_group.add_argument('-l', '--lock', nargs='?', dest='encrypt', type=int, const=8, metavar='N', help=argparse.SUPPRESS)
# Power toys
2016-05-31 12:39:34 -05:00
power_group = argparser.add_argument_group(
title='power toys',
description='''-p, --print [N] show details of bookmark at DB index N
2016-05-25 12:15:52 -05:00
show all bookmarks, if no arguments
-f, --format N modify -p output
N=1: show only URL, N=2: show URL and tag
-r, --replace oldtag [newtag ...]
replace oldtag with newtag everywhere
delete oldtag, if no newtag
-j, --json Json formatted output for -p, -s, -S, --st
-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
2016-05-28 08:15:03 -05:00
--noprompt do not show the prompt, run and exit
2016-05-25 12:15:52 -05:00
-o, --open N open bookmark at DB index N in web browser
-z, --debug show debug information and additional logs''')
power_group.add_argument('-p', '--print', nargs='?', dest='printindex', type=int, const=0, metavar='N', help=argparse.SUPPRESS)
2016-06-29 13:22:07 -05:00
power_group.add_argument('-f', '--format', dest='showOpt', type=int, default=0, 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('-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)
2016-05-28 08:15:03 -05:00
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)
# Show help and exit if no arguments
if len(sys.argv) < 2:
argparser.print_help(sys.stderr)
sys.exit(1)
# Parse the arguments
args = argparser.parse_args()
# Show help and exit if help requested
2016-05-31 12:39:34 -05:00
if args.help:
argparser.print_help(sys.stderr)
sys.exit(0)
# Assign the values to globals
if tagManual is not None and len(args.tag) > 0:
tagManual = args.tag
if titleManual is not None and len(args.title) > 0:
titleManual = ' '.join(args.title)
if description is not None and len(args.desc) > 0:
description = ' '.join(args.desc)
if args.jsonOutput:
import json
if args.debug:
logger.setLevel(logging.DEBUG)
logger.debug('Version %s', _VERSION_)
# Move pre-1.9 database to new location
2016-06-25 11:15:15 -05:00
#BukuDb.move_legacy_dbfile()
# Handle encrypt/decrypt options at top priority
if args.encrypt is not None:
BukuCrypt.encrypt_file(args.encrypt)
if args.decrypt is not None:
BukuCrypt.decrypt_file(args.decrypt)
# Initialize the database and get handles
2016-06-29 13:22:07 -05:00
bdb = BukuDb(args.noninteractive, args.jsonOutput, args.showOpt)
# Add a record
if args.addurl is not None:
# Parse tags into a comma-separated string
2016-05-29 08:33:58 -05:00
tags = DELIMITER
keywords = args.addurl
2016-05-31 16:40:51 -05:00
if tagManual is not None:
2016-06-12 05:30:54 -05:00
if tagManual[0] == '+' and len(tagManual) == 1:
pass
elif tagManual[0] == '+':
tagManual = tagManual[1:]
# In case of add, args.addurl may have URL followed by tags
# Add DELIMITER as url+tags may not end with comma
keywords = args.addurl + [DELIMITER] + tagManual
else:
keywords = args.addurl + [DELIMITER] + tagManual
if len(keywords) > 1:
tags = parse_tags(keywords[1:])
2016-05-29 01:09:51 -05:00
bdb.add_bookmark(args.addurl[0], titleManual, tags, description)
# Update record
2016-05-31 12:39:34 -05:00
if update:
if len(args.update) == 0:
update_index = 0
elif not args.update[0].isdigit():
logger.error('Index must be a number >= 0')
bdb.close_quit(1)
else:
update_index = int(args.update[0])
if args.url is not None:
new_url = args.url[0]
else:
new_url = ''
2016-06-12 05:30:54 -05:00
append = False
2016-07-03 16:50:44 -05:00
delete = False
2016-06-12 05:30:54 -05:00
if tagManual is not None:
2016-07-03 16:50:44 -05:00
if (tagManual[0] == '+' or tagManual[0] == '-') and len(tagManual) == 1:
logger.error('Please specify a tag')
bdb.close_quit(1)
2016-06-12 05:30:54 -05:00
elif tagManual[0] == '+':
tagManual = tagManual[1:]
append = True
2016-07-03 16:50:44 -05:00
elif tagManual[0] == '-':
tagManual = tagManual[1:]
delete = True
2016-06-12 05:30:54 -05:00
# Parse tags into a comma-separated string
tags = parse_tags(tagManual)
2016-07-03 16:50:44 -05:00
bdb.update_bookmark(update_index, new_url, titleManual, tags, description, append, delete)
# Search URLs, titles, tags for any keyword and delete if wanted
if args.sany is not None:
bdb.searchdb(args.sany, False, (args.delete is not None), args.deep)
# Search URLs, titles, tags with all keywords and delete if wanted
elif args.sall is not None:
if args.sall[0] == 'blank' and len(args.sall) == 1:
bdb.print_bookmark(0, True)
else:
bdb.searchdb(args.sall, True, (args.delete is not None), args.deep)
# Search bookmarks by tag and delete if wanted
elif tagsearch:
if len(args.stag) > 0:
tag = '%s%s%s' % (DELIMITER, ' '.join(args.stag).strip(DELIMITER), DELIMITER)
bdb.search_by_tag(tag, (args.delete is not None))
else:
bdb.list_tags()
# Just delete record(s)
elif (args.delete is not None):
2016-06-16 16:08:38 -05:00
if len(args.delete) == 0:
bdb.delete_bookmark(0)
elif len(args.delete) == 1 and '-' in args.delete[0]:
vals = str(args.delete[0]).split('-')
if len(vals) == 2 and is_int(vals[0]) and is_int(vals[1]):
if int(vals[0]) == int(vals[1]):
bdb.delete_bookmark(int(vals[0]))
elif int(vals[0]) < int(vals[1]):
bdb.delete_bookmark(0, int(vals[0]), int(vals[1]), True)
else:
bdb.delete_bookmark(0, int(vals[1]), int(vals[0]), True)
else:
logger.error('Incorrect index or range')
2016-06-16 16:08:38 -05:00
bdb.close_quit(1)
else:
ids = []
# Select the unique indices
2016-06-16 16:08:38 -05:00
for idx in args.delete:
if idx not in ids:
ids += (idx,)
try:
# Index delete order - highest to lowest
ids.sort(key=lambda x: int(x), reverse=True)
for idx in ids:
2016-06-16 16:08:38 -05:00
bdb.delete_bookmark(int(idx))
except ValueError as e:
logger.error('Index should be numerical (>= 0)')
# Print all records
if args.printindex is not None:
if args.printindex < 0:
logger.error('Index must be >= 0')
bdb.close_quit(1)
bdb.print_bookmark(args.printindex)
# Replace a tag in DB
if args.replace is not None:
if len(args.replace) == 1:
bdb.replace_tag(args.replace[0])
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:
logger.error('Index must be >= 1')
bdb.close_quit(1)
bdb.browse_by_index(args.openurl)
# Close DB connection and quit
bdb.close_quit(0)