From d9e603ac31016f1bad11bb5f8daa2580c0bbe9f0 Mon Sep 17 00:00:00 2001 From: cynic Date: Sun, 8 May 2022 00:44:05 -0400 Subject: [PATCH] first commit --- danbooru.nim | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ example.nim | 8 ++++++ readme.txt | 4 +++ 3 files changed, 81 insertions(+) create mode 100644 danbooru.nim create mode 100644 example.nim create mode 100644 readme.txt diff --git a/danbooru.nim b/danbooru.nim new file mode 100644 index 0000000..d6ec31f --- /dev/null +++ b/danbooru.nim @@ -0,0 +1,69 @@ +import std/httpclient +import std/strformat +import std/json +import std/options +import std/sequtils +import std/strutils +import std/sugar + +type + danbooru* = object + c: HttpClient + endpoint*: string + dbPost* = object + id*: int + uploader_id*: int + approver_id*: Option[int] + tag_string*: string + tag_string_general*: string + tag_string_artist*: string + tag_string_copyright*: string + tag_string_character*: string + tag_string_meta*: string + rating*: Option[string] + parent_id*: Option[int] + source*: string + md5*: string + file_url*: string + large_file_url*: string + preview_file_url*: string + file_ext*: string + file_size*: int + image_width*: int + score*: int + fav_count*: int + tag_count_general*: int + tag_count_artist*: int + tag_count_copyright*: int + tag_count_character*: int + tag_count_meta*: int + last_comment_bumped_at*: Option[string] + last_noted_at*: Option[string] + has_children*: bool + # is_note_locked*: bool + # is_rating_locked*: bool + # is_status_locked*: bool + image_height*: int + created_at*: string + updated_at*: string + + +proc newDanbooru*(): danbooru = + result.endpoint = "https://danbooru.donmai.us" + result.c = newHttpClient() + +proc post*(d: danbooru, id: int): dbPost = + to(d.c.getContent(d.endpoint&fmt"/posts/{id}.json").parseJson(), dbPost) + +proc searchPosts*(d: danbooru, tags: string, random: bool = false): seq[dbPost] = + to(d.c.getContent(d.endpoint&fmt"/posts.json?tags="&tags&"&random=" & $random).parseJson(), seq[dbPost]) + + +proc filterTags*(p: var seq[dbPost], tags: string): seq[dbPost] = + let stags: seq[string] = tags.split(" ") + p.filter((dbPost) => ( + for tag in stags: + if not ((tag in dbPost.tag_string.split(" ")) or (tag[0] == '-' and tag[1 .. ^1] notin dbPost.tag_string.split(" "))): + return false + return true + )) diff --git a/example.nim b/example.nim new file mode 100644 index 0000000..51a3982 --- /dev/null +++ b/example.nim @@ -0,0 +1,8 @@ +import danbooru + +var db = newDanbooru() +echo(db.endpoint) +var mai_waifu = db.searchPosts("makise_kurisu", random = true) +mai_waifu = mai_waifu.filterTags("1girl -1boy -2girl -futanari -sex_toy -cosplay -tattoo") +for post in mai_waifu: + echo(post.file_url) diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..0ed4fbe --- /dev/null +++ b/readme.txt @@ -0,0 +1,4 @@ +danbooru lib for nim +implements 'post's as objects and searches as post seqs +enough functions to search for posts and filter by tag (clientside, so you don't need an API key), which is all i've ever needed +cheers