Merge remote-tracking branch 'origin/json-output' into json-output
This commit is contained in:
commit
d60d9c4249
128
buku
128
buku
@ -276,6 +276,8 @@ def AddUpdateEntry(conn, cur, keywords, index):
|
|||||||
|
|
||||||
# Search the database for a tag or matching URL or Title info
|
# Search the database for a tag or matching URL or Title info
|
||||||
def searchdb(cur, keywords):
|
def searchdb(cur, keywords):
|
||||||
|
global jsonOutput
|
||||||
|
|
||||||
searchtag = ''
|
searchtag = ''
|
||||||
for token in keywords:
|
for token in keywords:
|
||||||
searchtag += token + " "
|
searchtag += token + " "
|
||||||
@ -310,35 +312,43 @@ def searchdb(cur, keywords):
|
|||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
results = []
|
results = []
|
||||||
for row in cur.execute(query, arguments):
|
resultset = cur.execute(query, arguments)
|
||||||
results.append(row[1])
|
|
||||||
count += 1
|
|
||||||
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" % (count, row[1], row[0], row[2], row[3][1:-1]))
|
|
||||||
|
|
||||||
if count == 0:
|
if jsonOutput == True:
|
||||||
return
|
results = cur.fetchall();
|
||||||
|
if len(results) > 0:
|
||||||
|
print(formatJson(results))
|
||||||
|
|
||||||
print("")
|
else:
|
||||||
|
for row in resultset:
|
||||||
|
results.append(row[1])
|
||||||
|
count += 1
|
||||||
|
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" % (count, row[1], row[0], row[2], row[3][1:-1]))
|
||||||
|
|
||||||
while True:
|
if count == 0:
|
||||||
try:
|
|
||||||
nav = input("Result number to open: ")
|
|
||||||
except EOFError:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
if is_int(nav):
|
print("")
|
||||||
index = int(nav) - 1
|
|
||||||
if index < 0:
|
|
||||||
print("Index out of bound")
|
|
||||||
continue
|
|
||||||
|
|
||||||
|
while True:
|
||||||
try:
|
try:
|
||||||
openurl = unquote(results[int(nav) - 1])
|
nav = input("Result number to open: ")
|
||||||
browser_open(openurl)
|
except EOFError:
|
||||||
except IndexError:
|
return
|
||||||
print("Index out of bound")
|
|
||||||
else:
|
if is_int(nav):
|
||||||
break
|
index = int(nav) - 1
|
||||||
|
if index < 0:
|
||||||
|
print("Index out of bound")
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
openurl = unquote(results[int(nav) - 1])
|
||||||
|
browser_open(openurl)
|
||||||
|
except IndexError:
|
||||||
|
print("Index out of bound")
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -366,47 +376,59 @@ def printdb(cur, index):
|
|||||||
global jsonOutput
|
global jsonOutput
|
||||||
|
|
||||||
if index == None: # Show all entries
|
if index == None: # Show all entries
|
||||||
resultSet = cur.execute('SELECT * FROM bookmarks')
|
resultset = cur.execute('SELECT * FROM bookmarks')
|
||||||
for row in resultSet:
|
if jsonOutput == False:
|
||||||
if jsonOutput == True:
|
for row in resultset:
|
||||||
data = formatJson(resultSet)
|
if showOpt == 1:
|
||||||
else:
|
print("%s %s" % (row[0], row[1]))
|
||||||
data = formatRaw(resultSet)
|
elif showOpt == 2:
|
||||||
|
print("%s %s %s" % (row[0], row[1], row[3][1:-1]))
|
||||||
|
else:
|
||||||
|
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:
|
||||||
|
print(formatJson(resultset))
|
||||||
|
|
||||||
else: # Show record at index
|
else: # Show record at index
|
||||||
try:
|
try:
|
||||||
for row in cur.execute("SELECT * FROM bookmarks WHERE id = ?", (int(index),)):
|
resultset = 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")
|
|
||||||
except IndexError:
|
except IndexError:
|
||||||
print("Index out of bound")
|
print("Index out of bound")
|
||||||
|
|
||||||
print(data)
|
if jsonOutput == False:
|
||||||
|
for row in resultset:
|
||||||
def formatRaw(resultSet):
|
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]))
|
||||||
marks = []
|
return
|
||||||
for row in resultSet:
|
print("No matching index")
|
||||||
if showOpt == 1:
|
|
||||||
marks.append(str(row[0]) + " " + row[1]);
|
|
||||||
elif showOpt == 2:
|
|
||||||
marks.append(str(row[0]) + " " + row[1] + " " + row[3][1:-1]);
|
|
||||||
else:
|
else:
|
||||||
marks.append(str(row[0]) + "." + row[1] + "\n\t" + row[2] + "\n\t" + row[3][1:-1]);
|
print(formatJson(resultset, True))
|
||||||
|
|
||||||
return "\n".join(marks)
|
def formatJson(resultset, single=False):
|
||||||
|
global showOpt
|
||||||
|
|
||||||
def formatJson(resultSet):
|
if single == False:
|
||||||
marks = []
|
marks = []
|
||||||
for row in resultSet:
|
for row in resultset:
|
||||||
if showOpt == 1:
|
if showOpt == 1:
|
||||||
record = { 'url': row[1] }
|
record = { 'url': row[1] }
|
||||||
elif showOpt == 2:
|
elif showOpt == 2:
|
||||||
record = { 'url': row[1], 'tags': row[3][1:-1] }
|
record = { 'url': row[1], 'tags': row[3][1:-1] }
|
||||||
else:
|
else:
|
||||||
record = { 'url': row[1], 'title': row[2], 'tags': row[3][1:-1]}
|
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]
|
||||||
|
|
||||||
marks.append(record)
|
|
||||||
|
|
||||||
return json.dumps(marks, sort_keys=True, indent=4)
|
return json.dumps(marks, sort_keys=True, indent=4)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user