From 3b8dd537714218be4e97f359dd36f429975014d8 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Tue, 18 Oct 2022 20:47:51 +0100 Subject: [PATCH] added burrkmarks --- .../{bookmurrks.nimble => burrkmarks.nimble} | 1 + burrkmarks/nim.cfg | 1 + burrkmarks/src/burrkkmarks.nim | 5 - burrkmarks/src/burrkmarks.nim | 111 ++++++++++++++++++ globurrl.nim | 6 + 5 files changed, 119 insertions(+), 5 deletions(-) rename burrkmarks/{bookmurrks.nimble => burrkmarks.nimble} (89%) create mode 100644 burrkmarks/nim.cfg delete mode 100644 burrkmarks/src/burrkkmarks.nim create mode 100644 burrkmarks/src/burrkmarks.nim diff --git a/burrkmarks/bookmurrks.nimble b/burrkmarks/burrkmarks.nimble similarity index 89% rename from burrkmarks/bookmurrks.nimble rename to burrkmarks/burrkmarks.nimble index 0043260..818abf8 100644 --- a/burrkmarks/bookmurrks.nimble +++ b/burrkmarks/burrkmarks.nimble @@ -11,3 +11,4 @@ bin = @["burrkmarks"] # Dependencies requires "nim >= 1.6.6" +requires "parsetoml >= 0.6.0" diff --git a/burrkmarks/nim.cfg b/burrkmarks/nim.cfg new file mode 100644 index 0000000..521e21d --- /dev/null +++ b/burrkmarks/nim.cfg @@ -0,0 +1 @@ +-d:ssl diff --git a/burrkmarks/src/burrkkmarks.nim b/burrkmarks/src/burrkkmarks.nim deleted file mode 100644 index 862d40c..0000000 --- a/burrkmarks/src/burrkkmarks.nim +++ /dev/null @@ -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!") diff --git a/burrkmarks/src/burrkmarks.nim b/burrkmarks/src/burrkmarks.nim new file mode 100644 index 0000000..c136f7e --- /dev/null +++ b/burrkmarks/src/burrkmarks.nim @@ -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>", {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() diff --git a/globurrl.nim b/globurrl.nim index 811c9c0..c2d3f3c 100644 --- a/globurrl.nim +++ b/globurrl.nim @@ -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):