2016-03-15 08:51:06 -05:00
#!/usr/bin/env python3
2015-11-01 14:08:45 -06:00
#
# Bookmark management utility
#
# Copyright (C) 2015 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
2015-12-19 14:16:24 -06:00
# along with buku. If not, see <http://www.gnu.org/licenses/>.
2015-11-01 14:04:41 -06:00
2015-11-04 08:11:16 -06:00
import sys
2015-11-06 14:26:08 -06:00
import os
2015-11-01 14:04:41 -06:00
import sqlite3
2016-04-24 14:19:32 -05:00
import argparse
2015-11-06 13:26:20 -06:00
import readline
2015-11-06 13:59:57 -06:00
import webbrowser
2015-11-06 16:32:08 -06:00
import html.parser as HTMLParser
2016-04-04 04:50:23 -05:00
from http.client import HTTPConnection, HTTPSConnection
2016-04-09 04:22:31 -05:00
from urllib.parse import urljoin, quote, unquote
2016-04-09 06:52:47 -05:00
import gzip
import io
2016-03-16 10:10:55 -05:00
import signal
2016-03-21 07:37:28 -05:00
import json
2016-04-08 07:38:40 -05:00
import shutil
2015-11-04 08:11:16 -06:00
2015-12-19 10:23:29 -06:00
# Import libraries needed for encryption
2015-12-19 09:02:55 -06:00
try:
2015-12-19 10:23:29 -06:00
import getpass
2015-12-19 09:02:55 -06:00
import hashlib
from Crypto.Cipher import AES
from Crypto import Random
2015-12-19 10:23:29 -06:00
import struct
2015-12-19 09:02:55 -06:00
no_crypto = False
2015-12-19 13:55:05 -06:00
BLOCKSIZE = 65536
2015-12-20 13:00:34 -06:00
SALT_SIZE = 32
CHUNKSIZE = 0x80000 # Read/write 512 KB chunks
2015-12-19 09:02:55 -06:00
except ImportError:
no_crypto = True
2015-12-19 10:23:29 -06:00
2015-11-04 13:07:10 -06:00
# Globals
2016-04-05 23:55:25 -05:00
addindex = None # DB index to insert URL into
showindex = None # Index of bookmark to show
showOpt = 0 # Modify show. 1: show only URL, 2: show URL and tag
entry = None # DB index to update or delete
update = False # Update a bookmark in DB
titleData = None # Title fetched from a page
titleManual = None # Manually add a title offline
2016-04-22 13:41:41 -05:00
iterations = 8 # Number of hash iterations to generate key
2016-04-17 11:07:26 -05:00
jsonOutput = False # Output json formatted result
2016-04-24 16:18:56 -05:00
debug = False # Enable debug logs
2016-04-05 23:55:25 -05:00
pipeargs = [] # Holds arguments piped to the program
2016-04-08 07:43:05 -05:00
_VERSION_ = 1.9 # Program version
2016-04-05 23:55:25 -05:00
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
self.data = ""
self.lasttag = None
def handle_starttag(self, tag, attrs):
self.inTitle = False
if tag == "title":
self.inTitle = True
self.lasttag = tag
def handle_endtag(self, tag):
global titleData
if tag == "title":
self.inTitle = False
if self.data != "":
titleData = self.data
self.reset() # We have received title data, exit parsing
def handle_data(self, data):
if self.lasttag == "title" and self.inTitle == True:
self.data += data
2016-04-10 02:09:51 -05:00
def error(self, message):
pass
2016-04-05 06:25:40 -05:00
2016-04-07 16:34:05 -05:00
def getDataPath():
2016-04-10 07:28:49 -05:00
"""Determine the DB file path:
if $XDG_DATA_HOME is defined, use it
else if $HOME exists, use it
else use the current directory
"""
2016-04-07 16:34:05 -05:00
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')
2016-04-08 07:38:40 -05:00
def moveOldDatabase():
2016-04-10 07:28:49 -05:00
"""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.
"""
2016-04-08 07:38:40 -05:00
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 = getDataPath()
newdbfile = os.path.join(newdbpath, 'bookmarks.db')
if os.path.exists(newdbfile):
print("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)
shutil.move(olddbfile, newdbfile)
2016-04-08 09:15:47 -05:00
print("Database was moved from old (%s) to new (%s) location.\n" % (olddbfile, newdbfile))
2016-04-08 07:38:40 -05:00
os.rmdir(olddbpath)
2015-11-01 14:04:41 -06:00
2015-11-08 12:56:52 -06:00
2015-11-05 09:18:51 -06:00
def initdb():
2016-04-05 06:25:40 -05:00
"""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
"""
2016-04-07 16:34:05 -05:00
dbpath = getDataPath()
2015-11-09 23:04:34 -06:00
if not os.path.exists(dbpath):
os.makedirs(dbpath)
2016-03-24 13:47:57 -05:00
dbfile = os.path.join(dbpath, 'bookmarks.db')
2015-12-19 12:49:44 -06:00
encpath = os.path.join(dbpath, 'bookmarks.db.enc')
2016-03-24 13:47:57 -05:00
# Notify if DB file needs to be decrypted first
if os.path.exists(encpath) and not os.path.exists(dbfile):
2015-12-19 12:49:44 -06:00
print("Unlock database first")
sys.exit(1)
2016-03-24 13:47:57 -05:00
# Show info on first creation
if no_crypto == False and not os.path.exists(dbfile):
print("DB file is being created. You may want to encrypt it later.")
2015-11-05 09:18:51 -06:00
# Create a connection
2016-03-24 13:47:57 -05:00
conn = sqlite3.connect(dbfile)
2015-11-05 09:18:51 -06:00
cur = conn.cursor()
# Create table if it doesn't exist
2015-11-07 11:47:24 -06:00
cur.execute('''CREATE TABLE if not exists bookmarks \
2015-11-09 04:18:44 -06:00
(id integer PRIMARY KEY, URL text NOT NULL UNIQUE, metadata text, tags text)''')
2015-11-05 09:18:51 -06:00
conn.commit()
return (conn, cur)
2015-11-08 12:56:52 -06:00
2016-04-09 09:10:30 -05:00
def getPageResp(url, fullurl=False):
2016-04-05 06:25:40 -05:00
"""Connect to a server and fetch the requested page data.
2016-04-09 06:52:47 -05:00
Supports gzip compression.
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: URL to fetch, redirection status
Returns: connection, HTTP(S) GET response
"""
2016-03-25 01:17:20 -05:00
if url.find("%20") != -1:
2016-04-19 12:02:15 -05:00
url = unquote(url).replace(" ", "%20")
2016-03-25 01:17:20 -05:00
else:
url = unquote(url)
2016-03-25 03:02:43 -05:00
if debug:
print("unquoted: %s" % url)
2016-03-25 01:17:20 -05:00
2016-03-23 09:58:21 -05:00
if url.find("https://") >= 0: # Secure connection
2016-03-22 15:23:46 -05:00
server = url[8:]
2016-03-23 09:58:21 -05:00
marker = server.find("/")
if marker > 0:
2016-04-09 09:10:30 -05:00
if fullurl == False:
2016-03-23 09:58:21 -05:00
url = server[marker:]
server = server[:marker]
urlconn = HTTPSConnection(server, timeout=30)
elif url.find("http://") >= 0: # Insecure connection
2016-03-22 15:23:46 -05:00
server = url[7:]
2016-03-23 09:58:21 -05:00
marker = server.find("/")
if marker > 0:
2016-04-09 09:10:30 -05:00
if fullurl == False:
2016-03-23 09:58:21 -05:00
url = server[marker:]
server = server[:marker]
urlconn = HTTPConnection(server, timeout=30)
2016-03-22 15:23:46 -05:00
else:
2016-04-01 09:56:00 -05:00
printmsg("Not a valid HTTP(S) url", "WARNING")
2016-03-23 09:58:21 -05:00
return (None, None)
2016-03-22 15:23:46 -05:00
2016-03-23 09:58:21 -05:00
if debug:
2016-04-14 09:04:02 -05:00
print("server [%s] url [%s]" % (server, url))
2016-03-23 09:58:21 -05:00
2016-04-09 04:22:31 -05:00
# Handle URLs passed with %xx escape
try:
url.encode('ascii')
except:
url = quote(url)
2016-04-09 06:52:47 -05:00
urlconn.request("GET", url, None, {
"Accept-encoding": "gzip",
})
2016-04-09 08:11:46 -05:00
return (urlconn, urlconn.getresponse())
2016-03-23 09:58:21 -05:00
2016-04-05 06:25:40 -05:00
def getTitleData(resp):
"""Invoke HTML parser and extract title from HTTP response
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: GET response
"""
2016-04-09 06:52:47 -05:00
data = None
2016-04-05 06:25:40 -05:00
charset = resp.headers.get_content_charset()
2016-04-09 06:52:47 -05:00
if resp.headers.get('Content-Encoding') == 'gzip':
2016-04-10 02:09:51 -05:00
if debug:
print("gzip response")
2016-04-09 06:52:47 -05:00
data = gzip.GzipFile(fileobj=io.BytesIO(resp.read())).read()
else:
data = resp.read()
2016-04-05 06:25:40 -05:00
if charset == None:
charset = 'utf-8'
2016-04-14 09:04:02 -05:00
if debug:
printmsg("Charset missing in response", "WARNING")
2016-04-05 06:25:40 -05:00
if debug:
print("charset: %s" % charset)
parser = BMHTMLParser()
try:
if charset == 'utf-8':
2016-04-09 06:52:47 -05:00
parser.feed(data.decode(charset, "replace"))
2016-04-05 06:25:40 -05:00
else:
2016-04-09 06:52:47 -05:00
parser.feed(data.decode(charset))
2016-04-05 06:25:40 -05:00
except Exception as e:
2016-04-10 00:07:16 -05:00
if debug and str(e) != "we should not get here!":
# Suppress Exception due to intentional self.reset() in HTMLParser
2016-04-09 12:39:00 -05:00
print("\x1b[1mEXCEPTION\x1b[21m [getTitleData]: (%s) %s" % (type(e).__name__, e))
2016-04-05 06:25:40 -05:00
2016-03-23 09:58:21 -05:00
def fetchTitle(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
"""
2016-03-23 09:58:21 -05:00
global titleData
2016-03-24 13:02:39 -05:00
titleData = None
2016-03-23 09:58:21 -05:00
urlconn = None
2016-04-09 08:43:12 -05:00
retry = False
2016-03-22 15:23:46 -05:00
try:
2016-04-05 07:40:05 -05:00
urlconn, resp = getPageResp(url, False)
2016-03-23 09:58:21 -05:00
2016-03-24 15:38:38 -05:00
while 1:
if resp is None:
break
2016-04-09 23:47:09 -05:00
elif resp.status == 200:
2016-03-24 15:38:38 -05:00
getTitleData(resp)
break
2016-04-09 23:47:09 -05:00
elif resp.status in [301, 302]:
2016-03-25 01:17:20 -05:00
redirurl = urljoin(url, resp.getheader('location', ''))
2016-04-01 09:56:00 -05:00
printmsg(redirurl, "REDIRECTION")
2016-03-22 15:23:46 -05:00
2016-04-10 08:01:11 -05:00
if redirurl.find("sorry/IndexRedirect?") >= 0: # gracefully handle Google blocks
2016-04-01 09:56:00 -05:00
printmsg("Connection blocked due to unusual activity", "ERROR")
2016-03-24 15:38:38 -05:00
break
2016-03-25 01:17:20 -05:00
marker = redirurl.find("redirectUrl=")
if marker != -1:
redirurl = redirurl[marker + 12:]
# break same URL redirection loop
if url == redirurl:
2016-04-01 09:56:00 -05:00
printmsg("Detected repeated redirection to same URL", "ERROR")
2016-03-25 01:17:20 -05:00
break
url = redirurl
urlconn.close()
2016-04-09 09:10:30 -05:00
# Try with complete URL on redirection
2016-04-05 07:40:05 -05:00
urlconn, resp = getPageResp(url, True)
2016-04-09 23:47:09 -05:00
elif resp.status == 500 and retry == False:
2016-04-09 08:43:12 -05:00
"""Retry on status 500 (Internal Server Error) with truncated
URL. Some servers support truncated request URL on redirection.
"""
urlconn.close()
if debug:
print("Received status 500: retrying.")
urlconn, resp = getPageResp(url, False)
retry = True
2016-03-24 15:38:38 -05:00
else:
2016-04-01 09:56:00 -05:00
printmsg(("[" + str(resp.status) + "] " + resp.reason), "ERROR")
2016-03-24 15:38:38 -05:00
break
2016-03-22 15:23:46 -05:00
except Exception as e:
2016-04-09 12:39:00 -05:00
print("\x1b[1mEXCEPTION\x1b[21m [fetchTitle]: (%s) %s" % (type(e).__name__, e))
2016-03-22 15:23:46 -05:00
finally:
2016-03-23 09:58:21 -05:00
if urlconn is not None:
urlconn.close()
if titleData is None:
return ''
2016-03-22 15:38:35 -05:00
return titleData.strip().replace("\n","")
2016-03-22 15:23:46 -05:00
2016-04-10 06:45:11 -05:00
def isBookmarkAdded(cur, url):
"""Check if URL already exists in DB
Params: cursor, URL to search
Returns: DB index if URL found, else -1
"""
cur.execute("SELECT id FROM bookmarks WHERE URL = ?", (url,))
resultset = cur.fetchall()
if len(resultset) == 0:
return -1
return resultset[0][0]
2015-11-10 05:20:30 -06:00
def AddUpdateEntry(conn, cur, keywords, index):
2016-04-05 06:25:40 -05:00
"""Add a new bookmark or update an existing record at index
or insert a new record at addindex (if empty)
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: connection, cursor, index to update
"""
2016-03-03 11:49:43 -06:00
global titleManual
2015-11-05 09:18:51 -06:00
tags = ','
2016-03-07 07:37:33 -06:00
meta = ''
2015-11-05 09:18:51 -06:00
url = keywords[0]
2016-03-07 07:37:33 -06:00
2016-04-10 06:45:11 -05:00
"""In case of an add or insert operation ensure
that the URL does not exist in DB already
"""
if index is None:
id = isBookmarkAdded(cur, url)
if id != -1:
print("URL already exists at index %d" % id)
return
2016-03-22 15:23:46 -05:00
# Cleanse and get the tags
2015-11-09 04:18:44 -06:00
if len(keywords) > 1:
for tag in keywords[1:]:
2016-03-16 10:49:35 -05:00
if tag[-1] == ',':
2016-04-19 12:02:15 -05:00
tag = tag.strip(',') + ',' # if delimiter is present, maintain it
2016-03-16 10:49:35 -05:00
else:
2016-03-24 13:02:39 -05:00
tag = tag.strip(',') # a token in a multi-word tag
2016-03-16 10:49:35 -05:00
2016-03-16 11:35:34 -05:00
if tag == ',':
continue
2016-03-16 10:49:35 -05:00
if tags[-1] == ',':
2015-11-09 04:18:44 -06:00
tags += tag
else:
2016-03-16 10:49:35 -05:00
tags += ' ' + tag
2015-11-05 09:18:51 -06:00
2016-03-16 10:49:35 -05:00
if tags[-1] != ',':
tags += ','
2015-11-05 09:18:51 -06:00
2016-03-03 11:49:43 -06:00
if titleManual != None:
2016-04-21 09:24:06 -05:00
if titleManual == "none":
meta = ''
else:
meta = titleManual
else:
2016-03-22 15:23:46 -05:00
meta = fetchTitle(url)
if meta == '':
print("\x1B[91mTitle: []\x1B[0m")
2015-11-07 01:19:13 -06:00
else:
2016-03-22 15:23:46 -05:00
print("Title: [%s]" % meta)
2015-11-06 16:32:08 -06:00
2015-11-10 05:20:30 -06:00
if index == None: # Insert a new entry
2015-11-07 07:29:38 -06:00
try:
2015-11-10 05:20:30 -06:00
if addindex == None: # addindex is index number to insert record at
2015-11-09 04:18:44 -06:00
cur.execute('INSERT INTO bookmarks(URL, metadata, tags) VALUES (?, ?, ?)', (url, meta, tags,))
2015-11-07 12:10:21 -06:00
else:
2015-11-09 04:18:44 -06:00
cur.execute('INSERT INTO bookmarks(id, URL, metadata, tags) VALUES (?, ?, ?, ?)', (int(addindex), url, meta, tags,))
2015-11-07 07:29:38 -06:00
conn.commit()
2016-03-24 15:48:36 -05:00
print("Added at index %d\n" % cur.lastrowid)
printdb(cur, str(cur.lastrowid))
2015-11-07 07:29:38 -06:00
except sqlite3.IntegrityError:
2015-11-08 17:29:06 -06:00
for row in cur.execute("SELECT id from bookmarks where URL LIKE ?", (url,)):
print("URL already exists at index %s" % row[0])
return
print("Index %s exists" % addindex)
2015-11-07 07:29:38 -06:00
else: # Update an existing entry
try:
2015-11-10 05:20:30 -06:00
cur.execute("UPDATE bookmarks SET URL = ?, metadata = ?, tags = ? WHERE id = ?", (url, meta, tags, int(index),))
2015-11-07 07:29:38 -06:00
conn.commit()
if cur.rowcount == 1:
2016-03-24 15:48:36 -05:00
print("Updated index %d\n" % int(index))
printdb(cur, int(index))
2015-11-07 07:29:38 -06:00
else:
print("No matching index")
except sqlite3.IntegrityError:
print("URL already exists")
2015-11-05 09:18:51 -06:00
2015-11-08 12:56:52 -06:00
2016-04-21 22:12:17 -05:00
def dbRefresh(conn, cur, index):
2016-04-05 06:25:40 -05:00
"""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-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: connection, cursor
"""
2016-04-21 23:53:38 -05:00
global titleManual
2016-04-21 22:12:17 -05:00
if index == 0:
cur.execute("SELECT id, url FROM bookmarks ORDER BY id ASC")
else:
cur.execute("SELECT id, url FROM bookmarks WHERE id = ?", (index,))
2016-04-21 23:53:38 -05:00
2016-03-24 13:47:57 -05:00
resultset = cur.fetchall()
2016-04-22 09:40:58 -05:00
if titleManual is None:
for row in resultset:
2016-04-21 23:53:38 -05:00
title = fetchTitle(row[1])
if title == '':
print("\x1B[91mTitle: []")
print("\x1b[1mNOT updating index %d\x1b[21m\x1B[0m\n" % row[0])
continue
else:
print("Title: [%s]" % title)
2016-04-22 09:40:58 -05:00
cur.execute("UPDATE bookmarks SET metadata = ? WHERE id = ?", (title, row[0],))
conn.commit()
print("Updated index %d\n" % row[0])
else:
if titleManual == "none":
title = ''
2016-03-24 13:47:57 -05:00
else:
2016-04-22 09:40:58 -05:00
title = titleManual
2016-03-24 13:47:57 -05:00
2016-04-22 09:40:58 -05:00
for row in resultset:
cur.execute("UPDATE bookmarks SET metadata = ? WHERE id = ?", (title, row[0],))
conn.commit()
print("Updated index %d\n" % row[0])
2016-03-24 13:47:57 -05:00
2016-04-24 15:33:59 -05:00
def searchdb(cur, keywords, all_keywords=False):
2016-04-05 06:25:40 -05:00
"""Search the database for an entries with tags or URL
or title info matching keywords and list those.
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: cursor, keywords to search
"""
2016-03-22 18:33:13 -05:00
global jsonOutput
2015-11-08 11:44:35 -06:00
arguments = []
placeholder = "'%' || ? || '%'"
2016-04-19 13:32:28 -05:00
query = "SELECT id, url, metadata, tags FROM bookmarks WHERE"
2016-04-24 15:33:59 -05:00
if all_keywords == True: # Match all keywords in URL or Title
2015-11-11 05:41:53 -06:00
for token in keywords:
2016-04-19 13:32:28 -05:00
query += " (tags LIKE (%s) OR URL LIKE (%s) OR metadata LIKE (%s)) AND" % (placeholder, placeholder, placeholder)
2015-11-11 05:41:53 -06:00
arguments.append(token)
arguments.append(token)
2016-04-19 13:32:28 -05:00
arguments.append(token)
query = query[:-4]
2015-11-11 05:41:53 -06:00
else: # Match any keyword in URL or Title
for token in keywords:
2016-04-19 13:32:28 -05:00
query += " tags LIKE (%s) OR URL LIKE (%s) OR metadata LIKE (%s) OR" % (placeholder, placeholder, placeholder)
arguments.append(token)
2015-11-11 05:41:53 -06:00
arguments.append(token)
arguments.append(token)
2016-04-19 13:32:28 -05:00
query = query[:-3]
2015-11-11 05:41:53 -06:00
2015-11-08 12:19:12 -06:00
if debug:
2015-11-11 05:41:53 -06:00
print("\"%s\", (%s)" % (query, arguments))
2015-11-05 12:26:02 -06:00
2015-11-05 12:33:00 -06:00
count = 0
2015-11-06 13:59:57 -06:00
results = []
2016-03-22 18:33:13 -05:00
resultset = cur.execute(query, arguments)
2015-11-05 12:26:02 -06:00
2016-04-17 09:45:29 -05:00
if jsonOutput == False:
2016-03-22 18:33:13 -05:00
for row in resultset:
results.append(row[1])
count += 1
2016-04-22 16:31:38 -05:00
print("\x1B[1m\x1B[93m%d. \x1B[0m\x1B[92m%s\x1B[0m [%d]\n\t%s\n\t\x1B[91m[TAGS]\x1B[0m %s\n" % (count, row[1], row[0], row[2], row[3][1:-1]))
2015-11-07 09:08:15 -06:00
2016-03-22 18:33:13 -05:00
if count == 0:
2016-03-10 06:33:54 -06:00
return
2016-03-22 18:33:13 -05:00
while True:
2015-11-06 13:59:57 -06:00
try:
2016-03-22 18:33:13 -05:00
nav = input("Result number to open: ")
except EOFError:
return
if is_int(nav):
index = int(nav) - 1
2016-04-17 09:31:40 -05:00
if index < 0 or index >= count:
2016-03-22 18:33:13 -05:00
print("Index out of bound")
continue
try:
2016-04-19 12:02:15 -05:00
browser_open(unquote(results[int(nav) - 1]))
2016-04-17 09:31:40 -05:00
except Exception as e:
print("\x1b[1mEXCEPTION\x1b[21m [searchdb]: (%s) %s" % (type(e).__name__, e))
2016-03-22 18:33:13 -05:00
else:
break
2015-11-06 13:59:57 -06:00
2016-04-17 09:45:29 -05:00
else:
results = cur.fetchall();
if len(results) > 0:
print(formatJson(results))
else:
return
2015-11-08 12:56:52 -06:00
2016-03-20 00:39:07 -05:00
def compactDB(conn, cur, index):
2016-04-05 06:25:40 -05:00
"""When an entry at index is deleted, move the last
entry in DB to index, if index is lesser.
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: connection, cursor, index of deleted entry
"""
2016-03-20 00:39:07 -05:00
cur.execute('SELECT MAX(id) from bookmarks')
results = cur.fetchall()
for row in results:
if row[0] > index:
cur.execute('SELECT id, URL, metadata, tags FROM bookmarks WHERE id = ?', (row[0],))
results = cur.fetchall()
for row in results:
cur.execute('DELETE FROM bookmarks WHERE id = ?', (row[0],))
conn.commit()
cur.execute('INSERT INTO bookmarks(id, URL, metadata, tags) VALUES (?, ?, ?, ?)', (index, row[1], row[2], row[3],))
conn.commit()
print("Index %d moved to %d" % (row[0], index))
2015-11-10 05:20:30 -06:00
def cleardb(conn, cur, index):
2016-04-05 06:25:40 -05:00
"""Delete a single record or remove the table if index is None
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: connection, cursor, index to delete
"""
2016-04-24 15:33:59 -05:00
if index == 0: # Remove the table
2016-03-19 23:18:13 -05:00
resp = input("ALL bookmarks will be removed. Enter \x1b[1my\x1b[21m to confirm: ")
if resp != 'y':
print("No bookmarks deleted")
return
2015-11-07 07:29:38 -06:00
cur.execute('DROP TABLE if exists bookmarks')
conn.commit()
2016-03-19 23:18:13 -05:00
print("All bookmarks deleted")
2015-11-07 07:29:38 -06:00
else: # Remove a single entry
try:
2016-04-24 15:33:59 -05:00
cur.execute('DELETE FROM bookmarks WHERE id = ?', (index,))
2015-11-07 07:29:38 -06:00
conn.commit()
if cur.rowcount == 1:
2016-04-24 15:33:59 -05:00
print("Removed index %d" % index)
compactDB(conn, cur, index)
2015-11-07 07:29:38 -06:00
else:
print("No matching index")
except IndexError:
2015-11-09 12:32:10 -06:00
print("Index out of bound")
2015-11-05 09:18:51 -06:00
2015-11-08 12:56:52 -06:00
2016-03-25 02:52:52 -05:00
def printdb(cur, index, empty=False):
2016-04-05 06:25:40 -05:00
"""Print bookmark details at index or all bookmarks if index is None
2016-04-24 16:18:56 -05:00
Print only bookmarks with blank title or tags if empty is True
Note: URL is printed on top because title may be blank
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: cursor, index to print, flag to show only bookmarks with no title or tags
"""
2015-11-11 01:28:37 -06:00
global showOpt
2016-03-22 18:31:17 -05:00
global jsonOutput
2015-11-11 01:28:37 -06:00
2016-03-25 02:52:52 -05:00
resultset = None
2016-04-20 10:09:18 -05:00
if int(index) == 0: # Show all entries
2016-03-25 02:52:52 -05:00
if empty == False:
cur.execute('SELECT * FROM bookmarks')
resultset = cur.fetchall()
else:
cur.execute("SELECT * FROM bookmarks WHERE metadata = '' OR tags = ','")
resultset = cur.fetchall()
print("\x1b[1m%d records found\x1b[21m\n" % len(resultset))
2016-03-22 18:32:37 -05:00
if jsonOutput == False:
2016-04-19 12:02:15 -05:00
if showOpt == 0:
for row in resultset:
2016-04-22 16:31:38 -05:00
print("\x1B[1m\x1B[93m%s. \x1B[0m\x1B[92m%s\x1B[0m\n\t%s\n\t\x1B[91m[TAGS]\x1B[0m %s\n" % (row[0], row[1], row[2], row[3][1:-1]))
2016-04-19 12:02:15 -05:00
elif showOpt == 1:
for row in resultset:
2016-03-22 18:32:37 -05:00
print("%s %s" % (row[0], row[1]))
2016-04-19 12:02:15 -05:00
elif showOpt == 2:
for row in resultset:
2016-03-22 18:32:37 -05:00
print("%s %s %s" % (row[0], row[1], row[3][1:-1]))
2016-04-19 12:02:15 -05:00
2016-03-22 18:32:37 -05:00
else:
print(formatJson(resultset))
2015-11-10 05:20:30 -06:00
else: # Show record at index
2015-11-09 12:32:10 -06:00
try:
2016-03-22 18:32:37 -05:00
resultset = cur.execute("SELECT * FROM bookmarks WHERE id = ?", (int(index),))
except IndexError:
print("Index out of bound")
2016-03-23 07:10:26 -05:00
return
2016-03-22 18:32:37 -05:00
if jsonOutput == False:
for row in resultset:
2015-11-10 00:55:54 -06:00
print("\x1B[1m\x1B[93m%s. \x1B[0m\x1B[92m%s\x1B[0m\n\t%s\n\t\x1B[91m[TAGS]\x1B[0m %s" % (row[0], row[1], row[2], row[3][1:-1]))
2015-11-09 12:32:10 -06:00
return
print("No matching index")
2016-03-22 18:32:37 -05:00
else:
print(formatJson(resultset, True))
2016-03-23 07:10:08 -05:00
2016-03-22 18:29:45 -05:00
def formatJson(resultset, single=False):
global showOpt
if single == False:
marks = []
for row in resultset:
if showOpt == 1:
record = { 'url': row[1] }
elif showOpt == 2:
record = { 'url': row[1], 'tags': row[3][1:-1] }
else:
record = { 'url': row[1], 'title': row[2], 'tags': row[3][1:-1]}
marks.append(record)
else:
marks = {}
for row in resultset:
if showOpt == 1:
marks['url'] = row[1]
elif showOpt == 2:
marks['title'] = row[2]
marks['tags'] = row[3][1:-1]
else:
marks['url'] = row[1]
marks['title'] = row[2]
marks['tags'] = row[3][1:-1]
2015-11-05 09:18:51 -06:00
2015-11-08 12:56:52 -06:00
2016-03-22 18:29:45 -05:00
return json.dumps(marks, sort_keys=True, indent=4)
2015-11-08 12:56:52 -06:00
2016-03-16 13:03:15 -05:00
def showUniqueTags(cur):
2016-04-05 06:25:40 -05:00
"""Print all unique tags ordered alphabetically
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: cursor
"""
2016-03-16 13:03:15 -05:00
count = 1
Tags = []
uniqueTags = []
for row in cur.execute('SELECT DISTINCT tags FROM bookmarks'):
if row[0] == ',':
continue
Tags.extend(row[0].strip(',').split(','))
for tag in Tags:
if tag not in uniqueTags:
uniqueTags.append(tag)
Tags = sorted(uniqueTags, key=str.lower)
for tag in Tags:
print("%6d. %s" % (count, tag))
count += 1
2016-03-19 10:55:13 -05:00
def replaceTags(conn, cur, orig, new):
2016-04-05 06:25:40 -05:00
"""Replace orig tags with new tags in DB for all records.
Remove orig tag is new tag is empty.
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: connection, cursor, original and new tags
"""
2016-03-19 12:06:44 -05:00
update = False
delete = False
2016-03-19 10:55:13 -05:00
orig = ',' + orig + ','
new = new.strip(',')
if new == '':
new = ','
2016-03-19 12:06:44 -05:00
delete = True
2016-03-19 10:55:13 -05:00
else:
new = ',' + new + ','
2016-03-19 12:19:58 -05:00
if orig == new:
print("Tags are same.")
return
2016-03-19 10:55:13 -05:00
cur.execute("SELECT id, tags FROM bookmarks WHERE tags LIKE ?", ('%' + orig + '%',))
results = cur.fetchall()
for row in results:
2016-03-19 12:06:44 -05:00
if delete == False:
if row[1].find(new) >= 0:
new = ','
2016-03-19 10:55:13 -05:00
newtags = row[1].replace(orig, new)
cur.execute("UPDATE bookmarks SET tags = ? WHERE id = ?", (newtags, row[0],))
2016-03-19 12:06:44 -05:00
print("Updated index %d" % row[0])
update = True
2016-03-19 10:55:13 -05:00
2016-03-19 12:06:44 -05:00
if update:
conn.commit()
2016-03-19 10:55:13 -05:00
2015-11-10 05:20:30 -06:00
def fetchopen(index):
2016-04-05 06:25:40 -05:00
"""Fetch URL at index and open in browser
2016-04-10 07:41:00 -05:00
2016-04-05 06:25:40 -05:00
Params: index
"""
2015-11-10 05:20:30 -06:00
try:
2016-04-24 16:18:56 -05:00
for row in cur.execute("SELECT URL FROM bookmarks WHERE id = ?", (index,)):
2015-11-10 05:20:30 -06:00
url = unquote(row[0])
browser_open(url)
return
print("No matching index")
except IndexError:
print("Index out of bound")
2015-11-06 13:59:57 -06: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
"""
2015-11-06 13:59:57 -06:00
try:
int(string)
return True
except:
return False
2015-11-08 12:56:52 -06:00
2015-11-10 03:11:05 -06:00
def browser_open(url):
2016-04-05 07:55:29 -05:00
"""Duplicate stdin, stdout (to suppress showing errors
on the terminal) and open URL in default browser
2015-11-06 16:32:08 -06:00
2016-04-05 07:55:29 -05:00
Params: url to open
"""
2015-11-08 12:56:52 -06:00
2015-11-10 05:20:30 -06:00
url = url.replace("%22", "\"")
2015-11-10 03:11:05 -06:00
_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:
2016-04-09 12:39:00 -05:00
print("\x1b[1mEXCEPTION\x1b[21m [browser_open]: (%s) %s" % (type(e).__name__, e))
2015-11-10 03:11:05 -06:00
finally:
os.close(fd)
os.dup2(_stderr, 2)
os.dup2(_stdout, 1)
2015-12-19 13:55:05 -06:00
def get_filehash(filepath):
2016-04-05 07:55:29 -05:00
"""Get the SHA256 hash of a file
Params: path to the file
"""
2015-12-19 13:55:05 -06:00
with open(filepath, 'rb') as f:
hasher = hashlib.sha256()
buf = f.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = f.read(BLOCKSIZE)
return hasher.digest()
2015-12-19 10:23:29 -06:00
def encrypt_file():
2016-04-05 07:55:29 -05:00
"""Encrypt the bookmarks database file"""
2016-04-07 16:34:05 -05:00
dbpath = os.path.join(getDataPath(), 'bookmarks.db')
2016-03-20 03:11:52 -05:00
encpath = dbpath + '.enc'
2015-12-19 10:23:29 -06:00
if not os.path.exists(dbpath):
2015-12-19 12:49:44 -06:00
print("%s missing. Already encrypted?" % dbpath)
2015-12-19 10:23:29 -06:00
sys.exit(1)
2016-03-20 03:11:52 -05:00
# If both encrypted file and flat file exist, error out
if os.path.exists(dbpath) and os.path.exists(encpath):
2016-04-01 09:56:00 -05:00
printmsg("Both encrypted and flat DB files exist!", "ERROR")
2016-03-20 03:11:52 -05:00
sys.exit(1)
2015-12-19 10:23:29 -06:00
password = ''
password = getpass.getpass()
passconfirm = getpass.getpass()
if password == '':
print("Empty password");
sys.exit(1)
if password != passconfirm:
print("Passwords don't match");
sys.exit(1)
2015-12-19 13:55:05 -06:00
# Get SHA256 hash of DB file
dbhash = get_filehash(dbpath)
2016-04-10 08:01:11 -05:00
# Generate random 256-bit salt and key
2016-04-10 07:41:00 -05:00
salt = Random.get_random_bytes(SALT_SIZE)
2015-12-20 13:00:34 -06:00
key = (password + salt.decode('utf-8', "replace")).encode('utf-8')
2015-12-22 12:10:24 -06:00
for i in range(iterations):
2015-12-20 13:00:34 -06:00
key = hashlib.sha256(key).digest()
iv = Random.get_random_bytes(16)
2015-12-19 10:23:29 -06:00
cipher = AES.new(key, AES.MODE_CBC, iv)
filesize = os.path.getsize(dbpath)
with open(dbpath, 'rb') as infile:
with open(encpath, 'wb') as outfile:
outfile.write(struct.pack('<Q', filesize))
2015-12-20 13:00:34 -06:00
outfile.write(salt)
2015-12-19 10:23:29 -06:00
outfile.write(iv)
2015-12-19 13:55:05 -06:00
# Embed DB file hash in encrypted file
outfile.write(dbhash)
2015-12-19 10:23:29 -06:00
while True:
2015-12-20 13:00:34 -06:00
chunk = infile.read(CHUNKSIZE)
2015-12-19 10:23:29 -06:00
if len(chunk) == 0:
break
elif len(chunk) % 16 != 0:
chunk += ' ' * (16 - len(chunk) % 16)
outfile.write(cipher.encrypt(chunk))
2015-12-19 11:15:17 -06:00
os.remove(dbpath)
2015-12-19 10:23:29 -06:00
print("File encrypted")
2016-04-24 15:33:59 -05:00
sys.exit(0)
2015-12-19 10:23:29 -06:00
2015-12-19 11:15:17 -06:00
def decrypt_file():
2016-04-05 07:55:29 -05:00
"""Decrypt the bookmarks database file"""
2016-04-07 16:34:05 -05:00
dbpath = os.path.join(getDataPath(), 'bookmarks.db')
2015-12-19 11:15:17 -06:00
encpath = dbpath + '.enc'
if not os.path.exists(encpath):
2016-04-01 09:56:00 -05:00
printmsg((encpath + " missing"), "ERROR")
2015-12-19 11:15:17 -06:00
sys.exit(1)
2016-03-20 03:11:52 -05:00
# If both encrypted file and flat file exist, error out
if os.path.exists(dbpath) and os.path.exists(encpath):
2016-04-01 09:56:00 -05:00
printmsg("Both encrypted and flat DB files exist!", "ERROR")
2015-12-19 11:15:17 -06:00
sys.exit(1)
password = ''
password = getpass.getpass()
if password == '':
print("Decryption failed");
sys.exit(1)
with open(encpath, 'rb') as infile:
origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
2015-12-20 13:00:34 -06:00
# Read 256-bit salt and generate key
salt = infile.read(32)
key = (password + salt.decode('utf-8', "replace")).encode('utf-8')
2015-12-22 12:10:24 -06:00
for i in range(iterations):
2015-12-20 13:00:34 -06:00
key = hashlib.sha256(key).digest()
2015-12-19 11:15:17 -06:00
iv = infile.read(16)
cipher = AES.new(key, AES.MODE_CBC, iv)
2015-12-19 13:55:05 -06:00
# Get original DB file's SHA256 hash from encrypted file
enchash = infile.read(32)
2015-12-19 11:15:17 -06:00
with open(dbpath, 'wb') as outfile:
while True:
2015-12-20 13:00:34 -06:00
chunk = infile.read(CHUNKSIZE)
2015-12-19 11:15:17 -06:00
if len(chunk) == 0:
break;
outfile.write(cipher.decrypt(chunk))
outfile.truncate(origsize)
2015-12-19 13:55:05 -06:00
# Match hash of generated file with that of original DB file
dbhash = get_filehash(dbpath)
if dbhash != enchash:
os.remove(dbpath)
2016-03-20 02:43:41 -05:00
print("Decryption failed");
2016-04-24 16:18:56 -05:00
sys.exit(1)
2015-12-19 13:55:05 -06:00
else:
os.remove(encpath)
print("File decrypted")
2015-12-19 11:15:17 -06:00
2016-03-16 10:10:55 -05:00
def sigint_handler(signum, frame):
2016-04-05 07:55:29 -05:00
"""Custom SIGINT handler"""
2016-03-16 10:10:55 -05:00
print('\nInterrupted.', file=sys.stderr)
sys.exit(1)
signal.signal(signal.SIGINT, sigint_handler)
2016-04-01 09:56:00 -05:00
def printmsg(msg, level=None):
2016-04-05 07:55:29 -05:00
"""Print a message in 2 parts, with the level in bold
Params: msg, level
"""
2016-04-01 09:56:00 -05:00
if level is not None:
print("\x1b[1m%s:\x1b[21m %s" % (level, msg))
else:
print("%s" % msg)
2016-04-05 07:55:29 -05:00
2016-04-05 23:55:25 -05:00
def usage():
"""Show buku usage, options, general information and exit"""
2016-04-19 10:37:24 -05:00
print("Usage: OPTIONS [URL] [TAGS] [KEYWORDS ...]\n\n"
"A private cmdline bookmark manager. Your mini web!\n\n"
"General options\n"
" -a URL [tags] add URL as bookmark with comma separated tags\n"
2016-04-20 10:53:12 -05:00
" -d N delete entry at DB index N (from -p 0), N=0 deletes all\n"
2016-04-19 10:37:24 -05:00
" -g list all tags alphabetically\n"
2016-04-22 12:01:17 -05:00
" -m title manually set title, for -a, -i, -u; '-m none' clears title\n"
2016-04-19 13:32:28 -05:00
" -s keyword(s) search bookmarks for any keyword\n"
2016-04-20 10:17:47 -05:00
" -S keyword(s) search bookmarks with all keywords\n"
2016-04-22 00:05:26 -05:00
" -u N [URL] [tags] update fields of the entry at DB index N\n"
" The first keyword, if available, is treated as the URL.\n"
" If URL is omitted (and -m is not used) the title of entry at\n"
" index N is refreshed from the web, N=0 refreshes all titles.\n\n"
2016-04-19 10:37:24 -05:00
"Power toys\n"
2016-04-19 12:02:15 -05:00
" -e show bookmarks with empty titles or no tags\n"
2016-04-19 10:37:24 -05:00
" -i N insert new bookmark at free DB index N\n"
" -j show results in Json format\n"
" -k decrypt (unlock) database file\n"
" -l encrypt (lock) database file\n"
" -o N open URL at DB index N in browser\n"
2016-04-20 10:41:07 -05:00
" -p N show details of bookmark record at DB index N, N=0 shows all\n"
2016-04-19 10:37:24 -05:00
" -r oldtag [newtag] replace oldtag with newtag, delete oldtag if newtag empty\n"
" -t N use N (> 0) hash iterations to generate key, for -k, -l\n"
2016-04-22 00:08:21 -05:00
" -x N modify -p behaviour, N=1: show only URL, N=2: show URL & tag\n"
2016-04-19 10:37:24 -05:00
" -z show debug information\n\n"
2016-04-23 00:25:26 -05:00
"Prompt keys\n"
2016-04-19 10:37:24 -05:00
" 1-N open Nth search result in browser\n"
2016-04-19 10:40:52 -05:00
" Enter exit buku\n\n"
2016-04-19 10:37:24 -05:00
"Version %.1f\n"
"Copyright (C) 2015 Arun Prakash Jana <engineerarun@gmail.com>\n"
"License: GPLv3\n"
"Webpage: https://github.com/jarun/buku\n" % _VERSION_)
2016-04-05 23:55:25 -05:00
sys.exit(1)
2016-03-26 10:59:07 -05:00
2016-04-05 23:55:25 -05:00
"""main starts here"""
2016-03-26 10:59:07 -05:00
def main(argv = sys.argv):
# detects whether have pipe line parsing in
if not sys.stdin.isatty():
pipeargs.extend(sys.argv)
for s in sys.stdin.readlines():
pipeargs.extend(s.split())
if __name__ == "__main__":
try:
main(sys.argv)
except KeyboardInterrupt:
pass
2016-04-24 14:19:32 -05:00
class ExtendedArgumentParser(argparse.ArgumentParser):
def print_help(self, file=None):
super(ExtendedArgumentParser, self).print_help(file)
argparser = ExtendedArgumentParser(
add_help=False,
description='A private cmdline bookmark manager. Your mini web!'
)
addarg = argparser.add_argument
2016-04-24 15:33:59 -05:00
addarg('-a', '--add', nargs='+', dest='addurl', metavar=('URL', 'tags'), # DONE
2016-04-24 14:19:32 -05:00
help='add URL as bookmark with comma separated tags')
2016-04-24 15:33:59 -05:00
addarg('-d', '--delete', nargs='?', dest='delete', type=int, const=0, metavar='N', # DONE
2016-04-24 14:19:32 -05:00
help='delete entry at DB index N (from -p 0), N=0 deletes all')
2016-04-24 15:33:59 -05:00
addarg('-g', '--tags', dest='showTags', action='store_true', # DONE
2016-04-24 14:19:32 -05:00
help='list all tags alphabetically')
addarg('-m', '--title', dest='titleManual', metavar='title',
help="manually set title, for -a, -i, -u; '-m none' clears title")
2016-04-24 15:33:59 -05:00
addarg('-s', '--sany', nargs='+', metavar='KEYWORD', # DONE
2016-04-24 14:19:32 -05:00
help='search bookmarks for any keyword')
2016-04-24 15:33:59 -05:00
addarg('-S', '--sall', nargs='+', metavar='KEYWORD', # DONE
2016-04-24 14:19:32 -05:00
help='search bookmarks with all keywords')
addarg('-u', '--update', nargs='+', dest='update', metavar=('N', 'URL tags'),
help='update fields of the entry at DB index N. The first keyword, if available, is treated as the URL. If URL is omitted (and -m is not used) the title of entry at index N is refreshed from the web, N=0 refreshes all titles.')
2016-04-24 15:33:59 -05:00
addarg('-e', '--empty', dest='empty', action='store_true', # DONE
2016-04-24 14:19:32 -05:00
help='show bookmarks with empty titles or no tags')
addarg('-i', '--insert', dest='insert', nargs='+', metavar=('N', 'URL tags'),
help='insert new bookmark with URL and tags at free DB index N')
addarg('-j', '--json', dest='jsonOutput', action='store_true',
help='show results in Json format')
2016-04-24 15:33:59 -05:00
addarg('-k', '--decrypt', dest='decrypt', action='store_true', # DONE
2016-04-24 14:19:32 -05:00
help='decrypt (unlock) database file')
2016-04-24 15:33:59 -05:00
addarg('-l', '--encrypt', dest='encrypt', action='store_true', # DONE
2016-04-24 14:19:32 -05:00
help='encrypt (lock) database file')
2016-04-24 16:18:56 -05:00
addarg('-o', '--open', dest='openurl', type=int, metavar='N', # DONE
2016-04-24 14:19:32 -05:00
help='open URL at DB index N in browser')
addarg('-p', '--print', dest='showindex', type=int, metavar='N',
help='show details of bookmark record at DB index N, N=0 shows all')
2016-04-24 16:18:56 -05:00
addarg('-r', '--replace', nargs=2, dest='replace', metavar=('oldtag', 'newtag'), # DONE
2016-04-24 14:19:32 -05:00
help='replace oldtag with newtag, delete oldtag if newtag=none')
2016-04-24 16:18:56 -05:00
addarg('-t', '--iterate', dest='iterations', type=int, metavar='N', # DONE
2016-04-24 14:19:32 -05:00
help='use N (> 0) hash iterations to generate key, for -k, -l')
addarg('-x', '--format', dest='showOpt', metavar='N',
help='modify -p behaviour, N=1: show only URL, N=2: show URL and tag')
2016-04-24 15:33:59 -05:00
addarg('-z', '--debug', dest='debug', action='store_true', # DONE
2016-04-24 14:19:32 -05:00
help='show debug information')
2015-11-04 08:11:16 -06:00
if len(sys.argv) < 2:
2016-04-24 14:19:32 -05:00
argparser.print_help(sys.stderr)
sys.exit(1)
2015-11-04 08:11:16 -06:00
2016-04-24 14:19:32 -05:00
'''
2015-11-08 12:56:52 -06:00
# Check cmdline options
2015-11-04 08:11:16 -06:00
try:
2016-04-17 09:31:40 -05:00
2016-03-26 10:59:07 -05:00
if len(pipeargs) > 0:
2016-04-21 12:54:37 -05:00
optlist, keywords = getopt(pipeargs[1:], "d:i:m:o:p:t:u:x:aegjklrsSz")
2016-03-26 10:59:07 -05:00
else:
2016-04-21 12:54:37 -05:00
optlist, keywords = getopt(sys.argv[1:], "d:i:m:o:p:t:u:x:aegjklrsSz")
2015-11-06 13:59:57 -06:00
if len(optlist) < 1:
usage()
2015-11-04 08:11:16 -06:00
for opt in optlist:
2015-11-05 09:18:51 -06:00
if opt[0] == "-a":
2015-11-08 12:19:12 -06:00
if update == True or delete == True:
2015-11-07 07:29:38 -06:00
print("You can either add or update or delete in one instance\n")
2015-11-05 21:40:44 -06:00
usage()
2015-11-05 09:18:51 -06:00
addurl = True
2015-11-04 08:11:16 -06:00
elif opt[0] == "-d":
2015-11-08 12:19:12 -06:00
if addurl == True or update == True:
2015-11-07 07:29:38 -06:00
print("You can either add or update or delete in one instance\n")
usage()
if not opt[1].isdigit():
usage()
entry = opt[1]
2016-04-20 10:53:12 -05:00
if int(entry) < 0:
2015-11-07 07:29:38 -06:00
usage()
2015-11-08 12:19:12 -06:00
delete = True
2016-03-25 02:52:52 -05:00
elif opt[0] == "-e":
empty = True
2016-03-16 13:03:15 -05:00
elif opt[0] == "-g":
showTags = True
2015-11-07 12:10:21 -06:00
elif opt[0] == "-i":
2015-11-10 03:11:05 -06:00
if update == True or delete == True:
print("You can either add or update or delete in one instance\n")
usage()
2015-11-07 12:10:21 -06:00
if not opt[1].isdigit():
usage()
addindex = opt[1]
if int(addindex) <= 0:
usage()
2015-11-10 03:11:05 -06:00
addurl = True
2016-03-21 07:37:52 -05:00
elif opt[0] == "-j":
jsonOutput = True
2015-12-19 11:15:17 -06:00
elif opt[0] == "-k":
if no_crypto == True:
2016-04-01 09:56:00 -05:00
printmsg("PyCrypto missing", "ERROR")
2015-12-19 11:15:17 -06:00
sys.exit(0)
2015-12-22 12:10:24 -06:00
decrypt = True
2015-12-19 11:15:17 -06:00
elif opt[0] == "-l":
if no_crypto == True:
2016-04-01 09:56:00 -05:00
printmsg("PyCrypto missing", "ERROR")
2015-12-19 11:15:17 -06:00
sys.exit(0)
2015-12-22 12:10:24 -06:00
encrypt = True
2016-03-03 11:49:43 -06:00
elif opt[0] == "-m":
titleManual = opt[1]
2015-11-10 05:20:30 -06:00
elif opt[0] == "-o":
if not opt[1].isdigit():
usage()
openurl = opt[1]
if int(openurl) <= 0:
usage()
2015-11-05 09:18:51 -06:00
elif opt[0] == "-p":
2015-11-09 12:32:10 -06:00
if not opt[1].isdigit():
usage()
showindex = opt[1]
2016-04-20 10:09:18 -05:00
if int(showindex) < 0:
2015-11-09 12:32:10 -06:00
usage()
2016-03-19 10:55:13 -05:00
elif opt[0] == "-r":
replace = True
2015-11-05 12:26:02 -06:00
elif opt[0] == "-s":
search = True
2015-11-11 05:41:53 -06:00
elif opt[0] == "-S":
searchAll = True
search = True
2015-12-22 12:10:24 -06:00
elif opt[0] == "-t":
if not opt[1].isdigit():
usage()
iterations = int(opt[1])
if iterations <= 0:
usage()
2015-11-05 21:40:44 -06:00
elif opt[0] == "-u":
2015-11-08 12:19:12 -06:00
if addurl == True or delete == True:
2015-11-07 07:29:38 -06:00
print("You can either add or update or delete in one instance\n")
2015-11-05 21:40:44 -06:00
usage()
2015-11-07 07:29:38 -06:00
2015-11-05 21:40:44 -06:00
if not opt[1].isdigit():
usage()
entry = opt[1]
2016-04-21 12:48:42 -05:00
if int(entry) < 0:
2015-11-05 21:40:44 -06:00
usage()
2015-11-08 12:19:12 -06:00
update = True
2015-11-11 01:28:37 -06:00
elif opt[0] == "-x":
if not opt[1].isdigit():
usage()
showOpt = int(opt[1])
if showOpt < 1 or showOpt > 2:
usage()
2015-11-08 12:19:12 -06:00
elif opt[0] == "-z":
debug = True
2015-11-04 08:11:16 -06:00
except GetoptError as e:
2015-12-19 14:16:24 -06:00
print("buku:", e)
2015-11-04 08:11:16 -06:00
sys.exit(1)
2016-04-24 14:19:32 -05:00
'''
args = argparser.parse_args()
2015-11-04 08:11:16 -06:00
2016-04-24 15:33:59 -05:00
titleManual = args.titleManual
2016-04-24 16:18:56 -05:00
if args.iterations is not None:
iterations = args.iterations
2016-04-24 14:19:32 -05:00
jsonOutput = args.jsonOutput
debug = args.debug
keywords = []
# Show version in debug logs
2016-03-30 14:28:37 -05:00
if debug:
print("Version %.1f" % _VERSION_)
2016-04-08 07:38:40 -05:00
# Move database to new location, if needed
moveOldDatabase()
2015-12-22 12:10:24 -06:00
# Handle encrypt/decrypt options at top priority
2016-04-24 15:33:59 -05:00
if args.encrypt == True:
if no_crypto:
printmsg("PyCrypto missing", "ERROR")
sys.exit(1)
2015-12-22 12:10:24 -06:00
encrypt_file()
2016-04-24 15:33:59 -05:00
if args.decrypt == True:
if no_crypto:
printmsg("PyCrypto missing", "ERROR")
sys.exit(1)
2015-12-22 12:10:24 -06:00
decrypt_file()
2016-04-24 16:18:56 -05:00
# Initialize the database and get handles
2015-11-05 09:18:51 -06:00
conn, cur = initdb()
2016-04-24 15:33:59 -05:00
# Add a record
if args.addurl is not None:
AddUpdateEntry(conn, cur, args.addurl, None)
# Remove a single record or all records
if args.delete is not None:
cleardb(conn, cur, args.delete)
# Show all unique tags
if args.showTags == True:
showUniqueTags(cur)
# Search URLs, titles, tags for keywords
if args.sany is not None:
searchdb(cur, args.sany)
if args.sall is not None:
searchdb(cur, args.sall, True)
2016-03-19 10:55:13 -05:00
# Replace a tag in DB
2016-04-24 14:19:32 -05:00
if args.replace is not None:
keywords = args.replace
if keywords[1] == "none":
2016-03-19 10:55:13 -05:00
replaceTags(conn, cur, keywords[0], "")
else:
replaceTags(conn, cur, keywords[0], keywords[1])
2016-04-21 12:48:42 -05:00
# Update record
if update == True:
2016-04-21 22:12:17 -05:00
if len(keywords) < 1:
dbRefresh(conn, cur, int(entry))
2016-04-21 12:48:42 -05:00
else:
2016-04-21 22:12:17 -05:00
AddUpdateEntry(conn, cur, keywords, entry)
2015-11-13 05:12:12 -06:00
2015-11-08 12:56:52 -06:00
# Print all records
2016-04-20 10:09:18 -05:00
if showindex is not None:
2015-11-09 12:32:10 -06:00
printdb(cur, showindex)
2015-11-04 21:54:10 -06:00
2016-04-24 15:33:59 -05:00
if args.empty == True:
printdb(cur, 0, True)
2016-03-25 02:52:52 -05:00
2015-11-10 05:20:30 -06:00
# Open URL in browser
2016-04-24 16:18:56 -05:00
if args.openurl is not None:
fetchopen(args.openurl)
2015-11-10 05:20:30 -06:00
2015-11-08 12:56:52 -06:00
# Close the connection before exiting
2015-11-01 14:04:41 -06:00
conn.close()