Skip to content

Commit

Permalink
sources: add HTML loading/parsing to Kemomo (fix #2977)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Jul 23, 2023
1 parent 5699a24 commit 77357ae
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion src/sites/Kemono/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const source: ISource = {
auth: [],
maxLimit: 50,
search: {
url: (query: ISearchQuery, opts: IUrlOptions, previous: IPreviousSearch | undefined): string | IError => {
url: (query: ISearchQuery, opts: IUrlOptions): string | IError => {
const offset = (query.page - 1) * opts.limit;
if (query.search) {
return {error: "The JSON API does not support arbitrary search."};
Expand Down Expand Up @@ -114,5 +114,57 @@ export const source: ISource = {
},
},
},
html: {
name: "Regex",
auth: [],
forcedLimit: 50,
search: {
url: (query: ISearchQuery, opts: IUrlOptions): string | IError => {
const offset = (query.page - 1) * opts.limit;
return "/posts?o=" + offset + "&q=" + encodeURIComponent(query.search);
},
parse: (src: string): IParsedSearch | IError => {
const html = Grabber.parseHTML(src);
const articles = html.find("article.post-card");

const images: IImage[] = [];
for (const article of articles) {
// Basic attributes
const identity = {
service: article.attr("data-service"),
user: article.attr("data-user"),
id: article.attr("data-id"),
};
const image: IImage = {
identity,
id: identity["id"],
author_id: identity["user"],
name: article.find("header")[0].innerText().trim(),
created_at: article.find("time")[0].attr("datetime"),
};

// Not all posts have an image
const img = article.find("img");
if (img.length > 0) {
image.preview_url = img[0].attr("src");
}

// Detect galleries with multiple files
const attachmentCount = parseInt(Grabber.regexToConst("count", "(?<count>\\d+) attachments?", article.innerHTML()), 10)
if (attachmentCount > 1) {
image.type = "gallery";
image.gallery_count = attachmentCount;
}

images.push(image);
}

return {
images,
imageCount: Grabber.regexToConst("count", "Showing \\d+ - \\d+ of (?<count>\\d+)", src),
};
},
},
},
},
};

0 comments on commit 77357ae

Please sign in to comment.