2015-11-01 14:04:41 -06:00
|
|
|
#!/usr/bin/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
|
|
|
|
# along with markit. 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-01 14:04:41 -06:00
|
|
|
import sqlite3
|
2015-11-04 08:11:16 -06:00
|
|
|
from getopt import getopt, GetoptError
|
2015-11-06 13:26:20 -06:00
|
|
|
import readline
|
2015-11-04 08:11:16 -06:00
|
|
|
|
2015-11-04 13:07:10 -06:00
|
|
|
# Globals
|
2015-11-05 09:18:51 -06:00
|
|
|
addurl = False
|
|
|
|
deletedb = False
|
2015-11-05 21:40:44 -06:00
|
|
|
showdb = False
|
2015-11-05 12:26:02 -06:00
|
|
|
search = False
|
2015-11-05 21:40:44 -06:00
|
|
|
entry = "0"
|
|
|
|
updatedb = False
|
2015-11-04 13:07:10 -06:00
|
|
|
|
2015-11-04 08:11:16 -06:00
|
|
|
def usage():
|
|
|
|
print("Usage: markit [OPTIONS] KEYWORDS...")
|
|
|
|
print("Bookmark manager.\n")
|
|
|
|
print("Options")
|
2015-11-05 09:18:51 -06:00
|
|
|
print(" -a URL tag entry1, tag entry2, ... print all bookmarks")
|
2015-11-05 09:25:11 -06:00
|
|
|
print(" -d delete ALL bookamrks")
|
2015-11-05 09:18:51 -06:00
|
|
|
print(" -p print all bookmarks")
|
2015-11-05 12:26:02 -06:00
|
|
|
print(" -s string(s) search all bookmarks for a tag or string")
|
2015-11-05 21:40:44 -06:00
|
|
|
print(" -u N update index number (from output of -p)")
|
2015-11-05 09:18:51 -06:00
|
|
|
print(" any other input exits markit\n")
|
2015-11-04 08:11:16 -06:00
|
|
|
print("Version 0.1")
|
|
|
|
print("Copyright (C) 2015 Arun Prakash Jana <engineerarun@gmail.com>")
|
|
|
|
print("Webpage: https://github.com/jarun/markit")
|
|
|
|
sys.exit(1)
|
2015-11-01 14:04:41 -06:00
|
|
|
|
2015-11-05 09:18:51 -06:00
|
|
|
def initdb():
|
|
|
|
# Create a connection
|
|
|
|
conn = sqlite3.connect('bookmarks.db')
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
|
|
|
# Create table if it doesn't exist
|
|
|
|
cur.execute('''CREATE TABLE if not exists bookmarks
|
|
|
|
(id integer PRIMARY KEY AUTOINCREMENT, URL text NOT NULL UNIQUE, tags text, metadata text)''')
|
|
|
|
conn.commit()
|
|
|
|
return (conn, cur)
|
|
|
|
|
|
|
|
def addentry(conn, cur, keywords):
|
|
|
|
tags = ','
|
|
|
|
url = keywords[0]
|
|
|
|
for tag in keywords[1:]:
|
|
|
|
if tags[-1] == ",":
|
|
|
|
tags += tag
|
|
|
|
else:
|
|
|
|
tags += " " + tag
|
|
|
|
|
|
|
|
if tags[-1] != ",":
|
|
|
|
tags += ","
|
|
|
|
|
|
|
|
try:
|
|
|
|
cur.execute('INSERT INTO bookmarks(URL, tags, metadata) VALUES (?, ?, ?)', (url, tags, ''))
|
|
|
|
conn.commit()
|
|
|
|
except sqlite3.IntegrityError:
|
|
|
|
print("URL already exists")
|
|
|
|
|
2015-11-05 12:26:02 -06:00
|
|
|
def searchdb(cur, keywords):
|
|
|
|
searchtag = ''
|
|
|
|
searchkey = keywords[0]
|
|
|
|
for token in keywords:
|
|
|
|
searchtag += token + " "
|
|
|
|
|
|
|
|
searchtag = ',' + searchtag[0:-1] + ','
|
|
|
|
|
2015-11-05 12:33:00 -06:00
|
|
|
count = 0
|
2015-11-05 22:28:44 -06:00
|
|
|
for row in cur.execute("SELECT * FROM bookmarks WHERE tags LIKE ('%' || ? || '%') \
|
|
|
|
OR tags LIKE ('%' || ? || '%') \
|
|
|
|
OR URL LIKE ('%' || ? || '%')", (searchtag, searchkey, searchkey)):
|
2015-11-05 12:33:00 -06:00
|
|
|
count += 1
|
|
|
|
print("%d. %s" % (count, row[1]))
|
2015-11-05 12:26:02 -06:00
|
|
|
|
2015-11-05 21:40:44 -06:00
|
|
|
def updateentry(conn, cur, entry, keywords):
|
|
|
|
tags = ','
|
|
|
|
url = keywords[0]
|
|
|
|
for tag in keywords[1:]:
|
|
|
|
if tags[-1] == ",":
|
|
|
|
tags += tag
|
|
|
|
else:
|
|
|
|
tags += " " + tag
|
|
|
|
|
|
|
|
if tags[-1] != ",":
|
|
|
|
tags += ","
|
|
|
|
|
2015-11-05 22:28:44 -06:00
|
|
|
cur.execute("UPDATE bookmarks SET URL = ?, tags = ?, metadata = '' WHERE id = ?", (url, tags, int(entry)))
|
|
|
|
conn.commit()
|
|
|
|
if cur.rowcount == 1:
|
|
|
|
print("Updated")
|
|
|
|
else:
|
|
|
|
print("No matching index")
|
2015-11-05 21:40:44 -06:00
|
|
|
|
2015-11-05 09:18:51 -06:00
|
|
|
def cleardb(conn, cur):
|
|
|
|
cur.execute('DROP TABLE if exists bookmarks')
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
def printtable(cur):
|
|
|
|
for row in cur.execute('SELECT * FROM bookmarks'):
|
2015-11-06 13:26:20 -06:00
|
|
|
print("%s. %s\n\t[TAGS] %s\n\t[META] %s" % (row[0], row[1], row[2][1:-1], row[3]))
|
2015-11-05 09:18:51 -06:00
|
|
|
|
2015-11-01 14:04:41 -06:00
|
|
|
# Main starts here
|
|
|
|
# ----------------
|
2015-11-04 08:11:16 -06:00
|
|
|
optlist = None
|
|
|
|
keywords = None
|
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
try:
|
2015-11-05 21:40:44 -06:00
|
|
|
optlist, keywords = getopt(sys.argv[1:], "u:adps")
|
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-05 21:40:44 -06:00
|
|
|
if updatedb == True:
|
|
|
|
print("You can either add or update in one instance\n")
|
|
|
|
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-05 09:18:51 -06:00
|
|
|
deletedb = True
|
|
|
|
elif opt[0] == "-p":
|
2015-11-05 21:40:44 -06:00
|
|
|
showdb = True
|
2015-11-05 12:26:02 -06:00
|
|
|
elif opt[0] == "-s":
|
|
|
|
search = True
|
2015-11-05 21:40:44 -06:00
|
|
|
elif opt[0] == "-u":
|
|
|
|
if addurl == True:
|
|
|
|
print("You can either add or update in one instance\n")
|
|
|
|
usage()
|
|
|
|
if not opt[1].isdigit():
|
|
|
|
usage()
|
|
|
|
|
|
|
|
entry = opt[1]
|
|
|
|
if int(entry) <= 0:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
updatedb = True
|
2015-11-04 08:11:16 -06:00
|
|
|
except GetoptError as e:
|
|
|
|
print("markit:", e)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-11-05 09:18:51 -06:00
|
|
|
conn, cur = initdb()
|
|
|
|
|
|
|
|
if addurl == True:
|
|
|
|
if len(keywords) < 1:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
addentry(conn, cur, keywords)
|
|
|
|
|
2015-11-05 12:26:02 -06:00
|
|
|
if search == True:
|
|
|
|
if len(keywords) < 1:
|
|
|
|
usage()
|
|
|
|
|
|
|
|
searchdb(cur, keywords)
|
|
|
|
|
2015-11-05 21:40:44 -06:00
|
|
|
if updatedb == True:
|
|
|
|
updateentry(conn, cur, entry, keywords)
|
2015-11-05 09:18:51 -06:00
|
|
|
|
2015-11-05 21:40:44 -06:00
|
|
|
if showdb == True:
|
2015-11-05 09:18:51 -06:00
|
|
|
printtable(cur)
|
2015-11-04 21:54:10 -06:00
|
|
|
|
2015-11-05 21:40:44 -06:00
|
|
|
if deletedb == True:
|
|
|
|
cleardb(conn, cur)
|
|
|
|
|
2015-11-01 14:04:41 -06:00
|
|
|
conn.close()
|