anime-wallpapers
Version:
Free wallpaper generator
107 lines • 5.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnimeWallpaper = void 0;
const node_superfetch_1 = __importDefault(require("node-superfetch"));
const cheerio_1 = __importDefault(require("cheerio"));
const Error_1 = __importDefault(require("./utils/Error"));
class AnimeWallpaper {
constructor() { }
getAnimeWall1(param) {
if (!param || !param.search)
throw new Error_1.default("param must be specified");
else if (!param.page)
param.page = 0;
if (typeof param.page === "string")
console.warn("Use number instead of a string on `page` options, this is will not be affected");
return new Promise((resolve, reject) => {
this._request("https://wall.alphacoders.com/search.php", {
search: encodeURIComponent(param.search),
page: param.page
})
.then(x => {
const $ = cheerio_1.default.load(x);
const arr = [];
$("#page_container [class=\"center\"] [class=\"thumb-container\"]").each((i, elm) => {
const title = $(elm).find("a").attr("title");
const thumbnail = $(elm).find("[class=\"boxgrid\"] a source").attr("srcset");
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const image = `https://${thumbnail === null || thumbnail === void 0 ? void 0 : thumbnail.split("/")[2]}/${thumbnail === null || thumbnail === void 0 ? void 0 : thumbnail.split("/")[3]}/${thumbnail === null || thumbnail === void 0 ? void 0 : thumbnail.split("/")[4].split("-")[2]}`;
arr.push({ title, thumbnail, image });
});
if (!arr.length)
throw new Error_1.default("No result found");
resolve(arr);
})
.catch(er => reject(er));
});
}
getAnimeWall2(param) {
if (!param)
throw new Error_1.default("param must be specified");
const baseUrl = "https://wallpapercave.com";
return new Promise((resolve, reject) => {
this._request(`${baseUrl}/search`, { q: param.split(" ").join("+") })
.then(x => {
const $ = cheerio_1.default.load(x);
const arr = [];
const results = [];
$("#content #popular a").each((i, elm) => {
const title = $(elm).attr("href");
results.push(title);
});
const filteredRes = results.filter(x => !x.startsWith("/w/") && !x.startsWith("/latest-upload"));
if (!filteredRes.length)
throw new Error_1.default("No result found");
const random = filteredRes[Math.floor(Math.random() * filteredRes.length)];
this._request(`${baseUrl}${random}`, {})
.then(res => {
const $$ = cheerio_1.default.load(res);
$$("#albumwp .wallpaper").each((i, elm) => {
const title = $(elm).find("a.wpinkw img").attr("alt");
const image = `${baseUrl}${$(elm).find("a.wpinkw img").attr("src")}`;
arr.push({ title, image });
});
resolve(arr);
})
.catch(er => reject(er));
})
.catch(er => reject(er));
});
}
getAnimeWall3() {
const baseUrl = "https://free4kwallpapers.com";
const random = Math.floor(Math.random() * 20) + 1;
return new Promise((resolve, reject) => {
this._request(`${baseUrl}/anime-wallpapers`, { page: random })
.then(x => {
const $ = cheerio_1.default.load(x);
const results = [];
$("#contents .container .row .cbody a img").each((i, elm) => {
const title = $(elm).attr("title");
const image = `${baseUrl}/${$(elm).attr("data-src")}`;
results.push({ title, image });
});
if (!results)
throw new Error_1.default("Images not found");
resolve(results);
})
.catch(er => reject(er));
});
}
_request(uri, options) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require("../package.json");
return new Promise((resolve, reject) => {
void node_superfetch_1.default.get(uri)
.query(options).set({
"user-agent": `${pkg.name}/${pkg.version} (${pkg.repository})`
})
.then(x => resolve(x.text))
.catch(er => reject(er));
});
}
}
exports.AnimeWallpaper = AnimeWallpaper;