From 349894d4f97b2700521ab90c181463c6434a017f Mon Sep 17 00:00:00 2001 From: neobooru <50623835+neobooru@users.noreply.github.com> Date: Sat, 20 Nov 2021 01:39:18 +0100 Subject: [PATCH] Add basic note import support for Danbooru and Gelbooru There UI currently doesn't display which notes will be imported, nor is there an option to not import notes. This will be added in a future version. --- package.json | 4 +-- src/SzuruTypes.ts | 5 ++++ src/SzuruWrapper.ts | 24 ++++++++++++------ src/ViewModels.ts | 49 ++++++++++++++++++++++++++++++++++++ src/popup/App.vue | 53 +++------------------------------------ src/scripts/background.ts | 8 +++--- yarn.lock | 22 +++++++++++++--- 7 files changed, 99 insertions(+), 66 deletions(-) create mode 100644 src/ViewModels.ts diff --git a/package.json b/package.json index 828e426..460364e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "szuru-chrome", - "version": "0.7.6", + "version": "0.7.7", "description": "Import media from various sites into szurubooru.", "author": "neobooru", "license": "MIT", @@ -16,7 +16,7 @@ }, "dependencies": { "axios": "^0.23.0", - "neo-scraper": "^0.4.7", + "neo-scraper": "^0.5.0", "vue": "^2.6.14", "webextension-polyfill": "^0.8.0" }, diff --git a/src/SzuruTypes.ts b/src/SzuruTypes.ts index 27479cc..cfc915e 100644 --- a/src/SzuruTypes.ts +++ b/src/SzuruTypes.ts @@ -97,6 +97,11 @@ export interface Post { comments: any[]; // Comment resource } +export interface SzuruNote { + polygon: number[][]; + text: string; +} + export interface SimilarPost { distance: number; post: Post; diff --git a/src/SzuruWrapper.ts b/src/SzuruWrapper.ts index 00046fc..cbb650a 100644 --- a/src/SzuruWrapper.ts +++ b/src/SzuruWrapper.ts @@ -1,7 +1,16 @@ import axios, { AxiosRequestConfig, CancelToken } from "axios"; -import { ScrapedPost } from "neo-scraper"; import { SzuruSiteConfig } from "./Config"; -import { TagsResult, TagCategoriesResult, Post, Tag, SzuruError, ImageSearchResult, TagFields } from "./SzuruTypes"; +import { + TagsResult, + TagCategoriesResult, + Post, + Tag, + SzuruError, + ImageSearchResult, + TagFields, + SzuruNote +} from "./SzuruTypes"; +import { PostViewModel } from "./ViewModels"; /** * A 1:1 wrapper around the szurubooru API. @@ -61,11 +70,12 @@ export default class SzuruWrapper { return (await this.apiGet("tag-categories")).data; } - async createPost(post: ScrapedPost): Promise { + async createPost(post: PostViewModel): Promise { var obj = { tags: post.tags.map(x => x.name), safety: post.rating, source: post.source, + notes: post.notes.map(note => { polygon: note.polygons, text: note.text }), contentUrl: post.contentUrl }; @@ -83,6 +93,10 @@ export default class SzuruWrapper { return (await this.apiPost("posts/reverse-search", obj)).data; } + static async createFromConfig(siteConfig: SzuruSiteConfig): Promise { + return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken); + } + private async apiGet( url: string, additionalHeaders: any = {}, @@ -137,8 +151,4 @@ export default class SzuruWrapper { throw error ? error : ex; } } - - static async createFromConfig(siteConfig: SzuruSiteConfig): Promise { - return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken); - } } diff --git a/src/ViewModels.ts b/src/ViewModels.ts new file mode 100644 index 0000000..ac9e634 --- /dev/null +++ b/src/ViewModels.ts @@ -0,0 +1,49 @@ +import { BooruTypes, ContentType, ScrapedNote, ScrapedPost, ScrapedTag } from "neo-scraper"; +import { MicroTag, Tag } from "./SzuruTypes"; + +export class TagViewModel { + public implications: TagViewModel[] = []; + + constructor(public name: string, public category?: BooruTypes.TagCategory, public usages?: number | undefined) {} + + static fromTag(tag: Tag) { + let x = new TagViewModel(tag.names[0], tag.category, tag.usages); + + if (tag.implications) { + x.implications = tag.implications.map(y => TagViewModel.fromMicroTag(y)); + } + + return x; + } + + static fromMicroTag(tag: MicroTag) { + return new TagViewModel(tag.names[0], tag.category, tag.usages); + } + + static fromScapedTag(tag: ScrapedTag): TagViewModel { + return new TagViewModel(tag.name, tag.category); + } +} + +export class PostViewModel { + name: string = ""; + tags: TagViewModel[] = []; + notes: ScrapedNote[]; + contentUrl: string; + pageUrl: string; + contentType: ContentType; + rating: BooruTypes.SafetyRating; + source: string | undefined; + referrer: string | undefined; + + constructor(post: ScrapedPost) { + this.contentUrl = post.contentUrl; + this.pageUrl = post.pageUrl; + this.contentType = post.contentType; + this.rating = post.rating; + this.source = post.source; + this.referrer = post.referrer; + this.tags = post.tags.map(x => TagViewModel.fromScapedTag(x)); + this.notes = post.notes; + } +} diff --git a/src/popup/App.vue b/src/popup/App.vue index bd89f65..ae4cc2c 100644 --- a/src/popup/App.vue +++ b/src/popup/App.vue @@ -127,58 +127,14 @@