Skip to content

Commit

Permalink
Implement directly open in browser feature.
Browse files Browse the repository at this point in the history
Signed-off-by: Arun Prakash Jana <[email protected]>
  • Loading branch information
jarun committed Nov 10, 2015
1 parent d492136 commit 98c1872
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 38 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ If you find `markit` useful, please consider donating via PayPal.
# Features
- Add, update or remove a bookmark
- Add tags to bookmarks
- Optionally fetch page title data from the web (default: disabled, use `-w`)
- Optionally fetch page title data from the web (default: disabled)
- Use (partial) tags or keywords to search bookmarks
- Unique URLs to avoid duplicates, show index if URL already exists
- Open bookmark in browser using index
- Open search results in browser
- Browser (Chromium and Firefox based) errors and warnings suppression
- Show single bookmark by ID or all bookmarks in a go
- Delete all bookmarks
- Add a bookmark at N<sup>th</sup> index, to fill deleted bookmark indexes
- Add a bookmark at N<sup>th</sup> index, to fill deleted bookmark indices
- Secure SQLite3 queries to access database
- Handle first level of redirections (reports IP blocking)
- Unicode in URL works
Expand Down Expand Up @@ -62,6 +63,7 @@ $ ./markit ...</pre>
- Substrings match (`match` matches `rematched`) for URL, tags and title data.
- All the keywords are treated together as a `single` tag in the same order. Bookmarks with partial or complete tag matches are shown in results.
- The same keywords are treated `separately` as unique tokens. Hence, entries with matching URL or title data are also shown in the results. Order is irrelevant in this case.
- Search results are indexed serially. The index is different from actual database index of a bookmark record. Use `-P` option to get DB index.

<b>Cmdline help:</b>

Expand All @@ -70,13 +72,14 @@ Bookmark manager. Your private Google.

Options
-a URL tag 1, tag 2, ... add URL as bookmark with comma separated tags
-d N delete entry at index N
-d N delete entry at DB index N (from -P output)
-D delete ALL bookmarks
-i N insert entry at index N, useful to fill deleted index
-p N show details of bookmark record at index N
-P show all bookmarks along with real index from database
-i N insert entry at DB index N, useful to fill deleted index
-o N open URL at DB index N in browser
-p N show details of bookmark record at DB index N
-P show all bookmarks along with index from DB
-s keyword(s) search all bookmarks for a (partial) tag or each keyword
-u N update entry at index N (from output of -P)
-u N update entry at DB index N
-w fetch title info from web, works with -a, -i, -u
-z show debug information
you can either add or update or delete in one instance
Expand All @@ -90,7 +93,7 @@ Keys
<pre>$ markit -a http://tuxdiary.com linux news, open source
Added at index 15012014</pre>
The assigned automatic index 15012014 is unique, one greater than highest index already in use in database.
2. Add a bookmark, fetch page Title information from web:
2. Add a bookmark, fetch page title information from web:
<pre>$ markit -a -w http://tuxdiary.com linux news, open source
Title: [TuxDiary | Linux, open source and a pinch of leisure.]
Added at index 15012014</pre>
Expand All @@ -111,13 +114,15 @@ This option is useful in filling deleted indices from database manually.
<pre>$ markit -p 15012014</pre>
8. Show all bookmarks with real index from database:
<pre>$ markit -P</pre>
9. Search bookmarks:
9. Open URL at index 15012014 in browser:
<pre>$ markit -o 15012014</pre>
10. Search bookmarks:
<pre>$ markit -s kernel debugging</pre>
10. Show debug info:
11. Show debug info:
<pre>$ markit -z</pre>
11. Show help:
12. Show help:
<pre>$ markit</pre>
12. Show manpage:
or,
<pre>$ man markit</pre>

#License
Expand Down
65 changes: 46 additions & 19 deletions markit
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ addurl = False
addindex = None
online = False
delete = False
openurl = None
show = False
showindex = None
search = False
Expand All @@ -48,13 +49,14 @@ def usage():
print("Bookmark manager. Your private Google.\n")
print("Options")
print(" -a URL tag 1, tag 2, ... add URL as bookmark with comma separated tags")
print(" -d N delete entry at index N")
print(" -d N delete entry at DB index N (from -P output)")
print(" -D delete ALL bookmarks")
print(" -i N insert entry at index N, useful to fill deleted index")
print(" -p N show details of bookmark record at index N")
print(" -P show all bookmarks along with real index from database")
print(" -i N insert entry at DB index N, useful to fill deleted index")
print(" -o N open URL at DB index N in browser")
print(" -p N show details of bookmark record at DB index N")
print(" -P show all bookmarks along with index from DB")
print(" -s keyword(s) search all bookmarks for a (partial) tag or each keyword")
print(" -u N update entry at index N (from output of -P)")
print(" -u N update entry at DB index N")
print(" -w fetch title info from web, works with -a, -i, -u")
print(" -z show debug information")
print(" you can either add or update or delete in one instance")
Expand Down Expand Up @@ -87,8 +89,8 @@ def initdb():



# Add a new bookmark or update an existing record
def AddUpdateEntry(conn, cur, keywords, entry):
# Add a new bookmark or update an existing record at index
def AddUpdateEntry(conn, cur, keywords, index):
global online

tags = ','
Expand Down Expand Up @@ -209,9 +211,9 @@ def AddUpdateEntry(conn, cur, keywords, entry):
meta = meta.strip().replace("\n","")
print("Title: [%s]" % meta)

if entry == None: # Add a new entry
if index == None: # Insert a new entry
try:
if addindex == None:
if addindex == None: # addindex is index number to insert record at
cur.execute('INSERT INTO bookmarks(URL, metadata, tags) VALUES (?, ?, ?)', (url, meta, tags,))
else:
cur.execute('INSERT INTO bookmarks(id, URL, metadata, tags) VALUES (?, ?, ?, ?)', (int(addindex), url, meta, tags,))
Expand All @@ -225,7 +227,7 @@ def AddUpdateEntry(conn, cur, keywords, entry):
print("Index %s exists" % addindex)
else: # Update an existing entry
try:
cur.execute("UPDATE bookmarks SET URL = ?, metadata = ?, tags = ? WHERE id = ?", (url, meta, tags, int(entry),))
cur.execute("UPDATE bookmarks SET URL = ?, metadata = ?, tags = ? WHERE id = ?", (url, meta, tags, int(index),))
conn.commit()
if cur.rowcount == 1:
print("Updated")
Expand Down Expand Up @@ -276,7 +278,6 @@ def searchdb(cur, keywords):

try:
openurl = unquote(results[int(nav) - 1])
openurl = openurl.replace("%22", "\"")
browser_open(openurl)
except IndexError:
print("Index out of bound")
Expand All @@ -286,13 +287,13 @@ def searchdb(cur, keywords):


# Delete a single record or remove the table
def cleardb(conn, cur, entry):
if entry == None: # Remove the table
def cleardb(conn, cur, index):
if index == None: # Remove the table
cur.execute('DROP TABLE if exists bookmarks')
conn.commit()
else: # Remove a single entry
try:
cur.execute("DELETE FROM bookmarks WHERE id = ?", (int(entry),))
cur.execute("DELETE FROM bookmarks WHERE id = ?", (int(index),))
conn.commit()
if cur.rowcount == 1:
print("Removed")
Expand All @@ -304,13 +305,13 @@ def cleardb(conn, cur, entry):


# Print all records in the table
def printdb(cur, showindex):
if showindex == None: # Show all entries
def printdb(cur, index):
if index == None: # Show all entries
for row in cur.execute('SELECT * FROM bookmarks'):
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]))
else: # Show record at index showindex
else: # Show record at index
try:
for row in cur.execute("SELECT * FROM bookmarks WHERE id = ?", (int(showindex),)):
for row in cur.execute("SELECT * FROM bookmarks WHERE id = ?", (int(index),)):
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]))
return
print("No matching index")
Expand All @@ -319,6 +320,19 @@ def printdb(cur, showindex):



# Fetch index and open URL in browser
def fetchopen(index):
try:
for row in cur.execute("SELECT URL FROM bookmarks WHERE id = ?", (int(index),)):
url = unquote(row[0])
browser_open(url)
return
print("No matching index")
except IndexError:
print("Index out of bound")



# Check if a value is a digit
def is_int(string):
try:
Expand Down Expand Up @@ -355,6 +369,8 @@ class BMHTMLParser(HTMLParser.HTMLParser):

# Open a URL in browser
def browser_open(url):
url = url.replace("%22", "\"")

_stderr = os.dup(2)
os.close(2)
_stdout = os.dup(1)
Expand Down Expand Up @@ -383,7 +399,7 @@ if len(sys.argv) < 2:

# Check cmdline options
try:
optlist, keywords = getopt(sys.argv[1:], "d:i:p:u:aDPswz")
optlist, keywords = getopt(sys.argv[1:], "d:i:o:p:u:aDPswz")
if len(optlist) < 1:
usage()

Expand Down Expand Up @@ -426,6 +442,13 @@ try:
usage()

addurl = True
elif opt[0] == "-o":
if not opt[1].isdigit():
usage()

openurl = opt[1]
if int(openurl) <= 0:
usage()
elif opt[0] == "-p":
if not opt[1].isdigit():
usage()
Expand Down Expand Up @@ -488,6 +511,10 @@ if search == True:
if show == True:
printdb(cur, showindex)

# Open URL in browser
if openurl != None:
fetchopen(openurl)

# Remove a single record of all records
if delete == True:
cleardb(conn, cur, entry)
Expand Down
20 changes: 13 additions & 7 deletions markit.1
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Search works in mysterious ways:
- Substrings match ('match' matches 'rematched') for URL, tags and title data.
- All the keywords are treated together as a 'single' tag in the same order. Bookmarks with partial or complete tag matches are shown in results.
- The same keywords are treated 'separately' as unique tokens. Hence, entries with matching URL or title data are also shown in the results. Order is irrelevant in this case.
- Search results are indexed serially. The index is different from actual database index of a bookmark record. Use '-P' option to get DB index.
.SH OPTIONS
.TP
.BI \-a " URL" " " "tag 1", " tag 2", " ..."
Expand All @@ -33,31 +34,36 @@ along with comma separated tags. A tag can have multiple words. The same URL can
.BI \-d " N"
Delete bookmark at index
.I N
(as shown in `-p` output).
in DB (from `-P` output).
.TP
.B \-D
Delete ALL bookmarks.
.TP
.BI \-i " N"
Add a new record at index
.I N
of the database. Use this option to fill blank indexes left by deleted bookmarks.
in DB. Use this option to fill blank indices left by deleted bookmarks.
.TP
.BI \-o " N"
Open URL at DB index
.I N
in browser.
.TP
.BI \-p " N"
Show details of bookmark record stored at
.I Nth
index in database.
Show details of bookmark record stored at index
.I N
in DB.
.TP
.B \-P
Show all bookmark records from the database along with actual index. Shows URL, title data and tags.
Show all bookmark records from the DB along with actual index. Shows URL, title data and tags.
.TP
.BI \-s " keywords"
Search bookmarks for a (partial) tag or keywords and show the results. Prompts to enter result number to open in browser. Note that the sequential index number may not match the real index in database. Shows URL, title data and tags.
.TP
.BI \-u " N"
Update bookmark at index
.I N
(as shown in `-P` output).
in DB.
.TP
.BI \-w
Fetch title data from the web. Works with `-a`, `-i` or `-u` options.
Expand Down

0 comments on commit 98c1872

Please sign in to comment.