supreme-community-api
Version:
Requests latest droplist from SupremeCommunity
80 lines • 3.29 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.scrape = exports.getItems = exports.getLatestWeek = void 0;
const cheerio_1 = __importDefault(require("cheerio"));
const request_promise_1 = __importDefault(require("request-promise"));
const latestSeason = "https://www.supremecommunity.com/season/latest/droplists/";
const options = (url) => ({
url,
transform: function (body) {
return cheerio_1.default.load(body);
},
});
/**
* Get the latest week of the current season
*
* @returns {string} The url of the most recent season
*/
const getLatestWeek = (weekNumber) => __awaiter(void 0, void 0, void 0, function* () {
const $ = yield request_promise_1.default(options(latestSeason));
const paths = $("a.block");
const weeks = [];
paths.each((i, el) => {
if (i === 1)
return;
weeks.push($(el).attr("href"));
});
const path = weekNumber ? weeks.reverse()[weekNumber - 1] : weeks[0];
return `https://www.supremecommunity.com/${path}`;
});
exports.getLatestWeek = getLatestWeek;
/**
* Get all of the latest items from the latest week
*
* @param {string} latestWeek
* @returns {array} Array of all the items in the latest week
*/
const getItems = (latestWeek) => __awaiter(void 0, void 0, void 0, function* () {
const $ = yield request_promise_1.default(options(latestWeek));
// Get all of the items on the page
const pageItems = yield $(".card.card-2");
let items = [];
// Loop through each item
pageItems.each((i, el) => {
const item = {
id: i + 1,
name: $(el).find("h2").text(),
price: $(el).find(".label-price").text().trim(),
image: `https://www.supremecommunity.com${$(el).find("img").attr("src")}`,
description: $(el).find("img").attr("alt").split(" - ")[1],
category: $(el).find(".category").text(),
};
items.push(item);
});
return items;
});
exports.getItems = getItems;
/**
* Get the latest week, then get the latest items, store them in an array and return the value
*
* @returns {array} Array of all the items in the latest week
*/
const scrape = (weekNumber) => __awaiter(void 0, void 0, void 0, function* () {
const latestWeek = yield getLatestWeek(weekNumber);
const latestItems = yield getItems(latestWeek);
return latestItems;
});
exports.scrape = scrape;
//# sourceMappingURL=index.js.map