added burrkmarks

This commit is contained in:
Paul Wilde 2022-10-18 20:47:51 +01:00
parent ae14fa08dd
commit 3b8dd53771
5 changed files with 119 additions and 5 deletions

View file

@ -11,3 +11,4 @@ bin = @["burrkmarks"]
# Dependencies
requires "nim >= 1.6.6"
requires "parsetoml >= 0.6.0"

1
burrkmarks/nim.cfg Normal file
View file

@ -0,0 +1 @@
-d:ssl

View file

@ -1,5 +0,0 @@
# This is just an example to get you started. A typical binary package
# uses this file as the main entry point of the application.
when isMainModule:
echo("Hello, World!")

View file

@ -0,0 +1,111 @@
import std/[json,os,marshal,osproc,re]
import std/[asyncdispatch, httpclient]
import ../../globurrl
type
Bookmark = object
name: string
shortname: string
url: string
const TITLE = "Burrkmarks"
let title_re = re("<title>(.*?)<\\/title>", {reMultiLine})
let title_rem_re = re("<\\/?title>")
let https_re = re("https?:\\/\\/")
let bookmarks_file = getSyncDir() & "bookmarks.json"
var bookmarks: seq[Bookmark] = @[]
proc `$`(bookmark: Bookmark): string =
let show_name = if bookmark.shortname != "": bookmark.shortname else: bookmark.name
let x = show_name & " - [" & bookmark.url & "]"
return x
proc getTitle(bookmark: Bookmark): Future[string] {.async.} =
var client = newAsyncHttpClient()
try:
let html = await client.getContent(bookmark.url)
let titles = html.findAll(title_re)
if len(titles) > 0:
var title = titles[0]
title = title.replace(title_rem_re,"")
return title
except:
echo getCurrentExceptionMsg()
return ""
proc save(bookmarks: seq[Bookmark]): bool {.discardable.} =
let file = open(bookmarks_file, fm_write)
defer: file.close()
file.write($$bookmarks)
proc get(bookmarks: seq[Bookmark], str: string): Bookmark =
for bookmark in bookmarks:
if str == $bookmark:
return bookmark
return Bookmark()
proc checkFile(): bool =
if not fileExists(bookmarks_file):
writeFile(bookmarks_file,"")
if fileExists(bookmarks_file):
return true
return false
proc getBookmarks(): seq[Bookmark] =
let f = open(bookmarks_file)
try:
let nodes = parseJson(f.readAll())
for node in nodes:
var bookmark = Bookmark()
bookmark.name = node.getOrDefault("name").getStr()
bookmark.shortname = node.getOrDefault("shortname").getStr()
bookmark.url = node.getOrDefault("url").getStr()
bookmarks.add(bookmark)
except:
echo getCurrentExceptionMsg()
return bookmarks
proc addBookmark(link: string) =
var url = link
var bookmark = Bookmark()
if not url.contains(https_re):
url = "https://" & url
bookmark.url = url
bookmark.name = waitFor bookmark.getTitle()
bookmarks.add(bookmark)
bookmarks.save()
proc goToBookmark(bookmark: string) =
let bm = bookmarks.get(bookmark)
discard execCmd("xdg-open " & bm.url)
proc toStrList(bookmarks: seq[Bookmark]): seq[string] =
var list: seq[string] = @[]
for bookmark in bookmarks:
list.add($bookmark)
return list
proc showBookmarks() =
let info = newInfo(TITLE)
let args = bookmarks.toStrList()
let option = outputData(info, args)
if option == "":
echo "Empty input, closing..."
return
elif option notin args:
echo "Adding bookmark: ", option
addBookmark(option)
showBookmarks()
elif option in args:
echo "Opening bookmark: ", option
goToBookmark(option)
proc start() =
if checkfile():
bookmarks = getBookmarks()
showBookmarks()
else:
echo "File : ", bookmarks_file, " does not exist."
when isMainModule:
start()

View file

@ -35,6 +35,7 @@ type
y*: int
const WM_TOOLS_DIR* = getHomeDir() & ".wm_tools/"
const WM_TOOLS_SYNC_DIR = getHomeDir() & "/Nextcloud/.wm_tools_sync/"
const background* = "#000000"
const backgroundalt* = "#bb222222"
const backgroundalt2* = "#bb333333"
@ -254,6 +255,11 @@ proc outputData*(data: Info, args: varargs[string]): string {.discardable.} =
output = runMenu(data,args)
return output
proc getSyncDir*(): string =
if existsOrCreateDir(WM_TOOLS_SYNC_DIR):
echo "Sync Dir already exists"
return WM_TOOLS_SYNC_DIR
return WM_TOOLS_SYNC_DIR
proc checkCacheDir() =
if not dirExists(WM_TOOLS_DIR):