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-06 14:26:08 -06:00
|
|
|
import os
|
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-06 13:59:57 -06:00
|
|
|
import webbrowser
|
2015-11-06 16:32:08 -06:00
|
|
|
import html.parser as HTMLParser
|
2015-11-06 17:07:15 -06:00
|
|
|
from http.client import HTTPSConnection
|
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-06 13:59:57 -06:00
|
|
|
print(" any other input shows help and 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 += ","
|
|
|
|
|
2015-11-06 16:32:08 -06:00
|
|
|
meta = ''
|
2015-11-06 17:07:15 -06:00
|
|
|
http = True
|
|
|
|
if url.find("https://") >= 0:
|
|
|
|
server = url[8:]
|
|
|
|
elif url.find("http://") >= 0:
|
|
|
|
server = url[7:]
|
|
|
|
else:
|
|
|
|
http = False
|
|
|
|
|
|
|
|
if http == True:
|
|
|
|
marker = server.find("/")
|
|
|
|
if marker > 0:
|
|
|
|
server = server[:marker]
|
|
|
|
|
|
|
|
urlconn = HTTPSConnection(server, timeout=45)
|
|
|
|
urlconn.request("GET", url)
|
|
|
|
resp = urlconn.getresponse()
|
|
|
|
parser = BMHTMLParser()
|
|
|
|
parser.feed(resp.read().decode('utf-8'))
|
|
|
|
meta = parser.data
|
|
|
|
urlconn.close()
|
2015-11-06 16:32:08 -06:00
|
|
|
|
2015-11-05 09:18:51 -06:00
|
|
|
try:
|
2015-11-06 16:32:08 -06:00
|
|
|
cur.execute('INSERT INTO bookmarks(URL, tags, metadata) VALUES (?, ?, ?)', (url, tags, meta))
|
2015-11-05 09:18:51 -06:00
|
|
|
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-06 13:59:57 -06:00
|
|
|
results = []
|
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-06 13:59:57 -06:00
|
|
|
results.append(row[1])
|
2015-11-05 12:33:00 -06:00
|
|
|
count += 1
|
2015-11-06 14:45:30 -06:00
|
|
|
print("\x1B[1m\x1B[93m%d. \x1B[0m\x1B[92m%s\x1B[0m" % (count, row[1]))
|
2015-11-05 12:26:02 -06:00
|
|
|
|
2015-11-06 13:59:57 -06:00
|
|
|
if count == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
while True:
|
2015-11-06 14:45:30 -06:00
|
|
|
nav = input("Index number to open: ")
|
2015-11-06 13:59:57 -06:00
|
|
|
if is_int(nav):
|
2015-11-06 14:26:08 -06:00
|
|
|
index = int(nav) - 1
|
|
|
|
if index < 0:
|
|
|
|
print("Index out of bound.")
|
|
|
|
continue
|
|
|
|
|
2015-11-06 13:59:57 -06:00
|
|
|
try:
|
2015-11-06 14:26:08 -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(results[int(nav) - 1])
|
|
|
|
finally:
|
|
|
|
os.close(fd)
|
|
|
|
os.dup2(_stderr, 2)
|
|
|
|
os.dup2(_stdout, 1)
|
2015-11-06 13:59:57 -06:00
|
|
|
except IndexError:
|
|
|
|
print("Index out of bound.")
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
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 14:45:30 -06:00
|
|
|
print("\x1B[1m\x1B[93m%s. \x1B[0m\x1B[92m%s\x1B[0m\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-06 13:59:57 -06:00
|
|
|
def is_int(string):
|
|
|
|
try:
|
|
|
|
int(string)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2015-11-06 16:32:08 -06:00
|
|
|
class BMHTMLParser(HTMLParser.HTMLParser):
|
|
|
|
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):
|
|
|
|
if tag == "title":
|
|
|
|
self.inTitle = False
|
|
|
|
|
|
|
|
def handle_data(self, data):
|
|
|
|
if self.lasttag == "title" and self.inTitle == True:
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
|
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-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-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()
|