Skip to content

Commit

Permalink
Add support for source booru.io
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed May 5, 2022
1 parent 3130acd commit 47e00cf
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/sites/Booru.io/booru.io/defaults.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[General]
name=Booru.io
ssl=true
Binary file added src/sites/Booru.io/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions src/sites/Booru.io/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const map = {
"id": "key",
"width": "attributes.width",
"height": "attributes.height",
"related": "related_tags",
};

const buildImage = (data: any): IImage => {
const img: IImage = Grabber.mapFields(data, map);
img.tags = Object.keys(data["tags"]);

const versions = [];
for (const key in data["transforms"]) {
const match = key.match(/width=(\d+):/);
if (match) {
versions.push({
size: parseInt(match[1], 10),
url: "/api/legacy/data/" + data["transforms"][key],
});
}
}
Grabber.setImageLinks(img, versions);

return img;
};

export const source: ISource = {
name: "Booru.io",
modifiers: [],
tagFormat: {
case: "lower",
wordSeparator: "_",
},
searchFormat: {
and: " ",
},
apis: {
json: {
name: "JSON",
auth: [],
forcedLimit: 50,
search: {
url: (query: ISearchQuery): string | IError => {
const cursor = (query.page - 1) * 50;
return "/api/legacy/query/entity?cursor=" + cursor + "&query=" + encodeURIComponent(query.search);
},
parse: (src: string): IParsedSearch => {
const raw = JSON.parse(src);
const images = raw.data.map(buildImage);
return { images };
},
},
details: {
fullResults: true,
url: (id: string): string | IError => {
return "/api/legacy/entity/" + id;
},
parse: (src: string): IImage => {
const raw = JSON.parse(src);
return buildImage(raw);
},
},
},
},
};
1 change: 1 addition & 0 deletions src/sites/Booru.io/sites.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
booru.io
1 change: 1 addition & 0 deletions src/sites/Booru.io/supported.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
booru.io
28 changes: 28 additions & 0 deletions src/sites/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,31 @@ addHelper("parseSearchQuery", (query: string, metas: Record<string, MetaField>):
ret.query = tags.join(" ");
return ret;
});

addHelper("setImageLinks", (image: IImage, versions: { size: number, url: string }[]): void => {
let previewWidth = 0;
let sampleWidth = 0;
let fullWidth = 0;

for (const version of versions) {
const size = version.size;

// preview_url gets the biggest size between 150 and 300
if (!previewWidth || (size <= 300 && size > previewWidth) || (size >= 150 && previewWidth > 300)) {
image.preview_url = version.url;
previewWidth = size;
}

// sample_url is optional and takes the biggest size between 500 and 1500
if (size >= 500 && size <= 1500 && size > sampleWidth) {
image.sample_url = version.url;
sampleWidth = size;
}

// file_url just takes the biggest size available
if (size > fullWidth) {
image.file_url = version.url;
fullWidth = size;
}
}
})

0 comments on commit 47e00cf

Please sign in to comment.