-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add 3hentai to the supported sites
- Loading branch information
Showing
7 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { scrapeContent } from "../../scraper/3hentai/3hentaiGetController"; | ||
import c from "../../utils/options"; | ||
import { logger } from "../../utils/logger"; | ||
import { isNumeric } from "../../utils/modifier"; | ||
import { Request, Response, NextFunction } from "express"; | ||
|
||
export async function get3hentai(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const book = req.query.book as string; | ||
if (!book) throw Error("Parameter book is required"); | ||
if (!isNumeric(book)) throw Error("Value must be number"); | ||
|
||
const url = `${c.THREEHENTAI}/d/${book}`; | ||
const data = await scrapeContent(url); | ||
logger.info({ | ||
path: req.path, | ||
query: req.query, | ||
method: req.method, | ||
ip: req.ip, | ||
useragent: req.get("User-Agent") | ||
}); | ||
return res.json(data); | ||
} catch (err: any) { | ||
next(Error(err.message)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { scrapeContent } from "../../scraper/3hentai/3hentaiGetController"; | ||
import c from "../../utils/options"; | ||
import { logger } from "../../utils/logger"; | ||
import { Request, Response, NextFunction } from "express"; | ||
|
||
export async function random3hentai(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const url = `${c.THREEHENTAI}/random`; | ||
const data = await scrapeContent(url); | ||
logger.info({ | ||
path: req.path, | ||
query: req.query, | ||
method: req.method, | ||
ip: req.ip, | ||
useragent: req.get("User-Agent") | ||
}); | ||
return res.json(data); | ||
} catch (err: any) { | ||
next(Error(err.message)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { scrapeContent } from "../../scraper/3hentai/3hentaiSearchController"; | ||
import c from "../../utils/options"; | ||
import { logger } from "../../utils/logger"; | ||
import { Request, Response, NextFunction } from "express"; | ||
const sorting = ["recent", "popular-24h", "popular-7d", "popular"]; | ||
|
||
export async function search3hentai(req: Request, res: Response, next: NextFunction) { | ||
try { | ||
const key = req.query.key || ""; | ||
const page = req.query.page || 1; | ||
const sort = req.query.sort as string || sorting[0] as string; | ||
if (!key) throw Error("Parameter key is required"); | ||
if (!sorting.includes(sort)) throw Error("Invalid sort: " + sorting.join(", ")); | ||
|
||
const url = `${c.THREEHENTAI}/search?q=${key}&page=${page}&sort=${sort}`; | ||
const data = await scrapeContent(url); | ||
logger.info({ | ||
path: req.path, | ||
query: req.query, | ||
method: req.method, | ||
ip: req.ip, | ||
useragent: req.get("User-Agent") | ||
}); | ||
return res.json(data); | ||
} catch (err: any) { | ||
next(Error(err.message)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { load } from "cheerio"; | ||
import p from "phin"; | ||
import c from "../../utils/options"; | ||
|
||
|
||
interface IGet3hentai { | ||
title: string; | ||
id: number; | ||
tags: string[]; | ||
total: number; | ||
image: string[]; | ||
upload_date: string; | ||
|
||
} | ||
|
||
interface IData{ | ||
data: object; | ||
source: string; | ||
} | ||
|
||
export async function scrapeContent(url: string) { | ||
try { | ||
const res = await p({ url: url, followRedirects: true }); | ||
const $ = load(res.body); | ||
|
||
//get href in <div id="main-cover"> first | ||
const actualId = $("#main-cover").find("a").attr("href"); | ||
//get after last second '/' in asu | ||
const book = actualId?.split("/")[4]; | ||
|
||
const title = $("h1").text(); | ||
const id = parseInt(url.split("/").pop() as string) || parseInt(book as string); | ||
const tags = $("span.filter-elem")?.map((i, el) => $(el).text()).get(); | ||
const tagsClean = tags.map((tag: string) => tag.replace(/<[^>]*>/g, "").replace(/\n/g, "").trim()); | ||
const image = $("div.single-thumb-col")?.map((i, el) => $(el).find("img").attr("data-src")).get(); | ||
const imageClean = image.map((img: string) => img.replace("t.", ".")); | ||
const upload_date = $("time").text(); | ||
|
||
const objectData: IGet3hentai = { | ||
title: title, | ||
id: id, | ||
tags: tagsClean.slice(0, tagsClean.length - 1), | ||
total: image.length, | ||
image: imageClean, | ||
upload_date: upload_date, | ||
}; | ||
|
||
const data: IData = { | ||
data: objectData, | ||
source: `${c.THREEHENTAI}/d/${id ? id : book}`, | ||
}; | ||
return data; | ||
} catch (err) { | ||
const error = err as string; | ||
throw new Error(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { load } from "cheerio"; | ||
import p from "phin"; | ||
import { removeNonNumeric } from "../../utils/modifier"; | ||
|
||
interface I3HentaiSearch { | ||
title: string; | ||
id: number; | ||
} | ||
|
||
export async function scrapeContent(url: string) { | ||
try { | ||
const res = await p({ url: url, followRedirects: true }); | ||
const $ = load(res.body as Buffer); | ||
|
||
//in <div class="listing-container bg-container container-xl"> there are <div class="doujin-col"> | ||
const doujinCol = $("div.listing-container.bg-container.container-xl"); | ||
//in <div class="doujin-col"> there are <div class="doujin"> | ||
const doujin = doujinCol.find("div.doujin"); | ||
|
||
//map all href in <div class="doujin"> | ||
const href = doujin.map((i, el) => $(el).find("a").attr("href")).get(); | ||
// const book = href.map((id: string) => id.split("/").pop()); | ||
|
||
//There is two <div class="title flag flag-eng"> and <div class="title flag flag-jpn">, get all text | ||
const title = doujin.map((i, el) => $(el).find("div.title").text()).get(); | ||
const titleClean = title.map((title: string) => title.replace(/<[^>]*>/g, "").replace(/\n/g, "").trim()); | ||
|
||
|
||
const content = []; | ||
for (const [i, val] of href.entries()) { | ||
const id = removeNonNumeric(val); | ||
const objectData: I3HentaiSearch = { | ||
title: titleClean[i], | ||
id: parseInt(id), | ||
}; | ||
content.push(objectData); | ||
} | ||
|
||
const data = { | ||
data: content, | ||
page: parseInt(url.split("&page=")[1]), | ||
sort: url.split("sort=")[1], | ||
source: url | ||
}; | ||
return data; | ||
|
||
} catch (err) { | ||
const error = err as string; | ||
throw new Error(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters