@uwu-codes/furaffinity-api
Version:
FurAffinity wrapper for Node.js
178 lines (177 loc) • 8.98 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.FetchMyWatchingList = exports.FetchWatchingList = exports.FetchAuthor = exports.FaveSubmission = exports.FetchSubmissions = exports.FetchSubmission = exports.FetchScraps = exports.FetchGallery = exports.FetchBrowse = exports.FetchSearch = exports.FetchHome = exports.SetProxy = exports.Login = exports.COOKIES = exports.ENDPOINT = void 0;
const Enums_1 = require("./Enums");
const cloudscraper_1 = __importDefault(require("cloudscraper"));
const lodash_1 = __importDefault(require("lodash"));
exports.ENDPOINT = 'https://www.furaffinity.net';
exports.COOKIES = { loggedIn: false, a: '', b: '' };
function Login(cookieA, cookieB) {
exports.COOKIES.loggedIn = true;
exports.COOKIES.a = cookieA;
exports.COOKIES.b = cookieB;
}
exports.Login = Login;
function SetProxy(config) {
cloudscraper_1.default.defaults({ proxy: config });
}
exports.SetProxy = SetProxy;
;
;
/**
* util to request
* @param options options
*/
function request(options) {
return new Promise((resolve, reject) => {
options = lodash_1.default.merge({
headers: exports.COOKIES.loggedIn ? {
Cookie: `a=${exports.COOKIES.a}; b=${exports.COOKIES.b}`,
Connection: 'Keep-Alive'
} : {
Connection: 'Keep-Alive'
},
agentOptions: {
keepAlive: true,
maxSockets: Infinity
}
}, options);
cloudscraper_1.default(options, (error, response, body) => {
if (error) {
console.error(error);
reject();
}
if (response.statusCode !== 200) {
console.error(`${response.statusCode}: `, response.statusMessage);
reject();
}
resolve(body);
});
});
}
function FetchHome() {
return __awaiter(this, void 0, void 0, function* () {
return yield request({ url: `${exports.ENDPOINT}/me` });
});
}
exports.FetchHome = FetchHome;
function FetchSearch(query, options) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${exports.ENDPOINT}/search`;
const page = (options === null || options === void 0 ? void 0 : options.prev) ? ((options === null || options === void 0 ? void 0 : options.page) || 1) + 1
: ((options === null || options === void 0 ? void 0 : options.page) || 1) - 1;
return yield request({
url,
method: 'post',
formData: {
'rating-general': ((options === null || options === void 0 ? void 0 : options.rating) || Enums_1.Rating.Any) & Enums_1.Rating.General ? 'on' : 'off',
'rating-mature': ((options === null || options === void 0 ? void 0 : options.rating) || Enums_1.Rating.Any) & Enums_1.Rating.Mature ? 'on' : 'off',
'rating-adult': ((options === null || options === void 0 ? void 0 : options.rating) || Enums_1.Rating.Any) & Enums_1.Rating.Adult ? 'on' : 'off',
'type-art': ((options === null || options === void 0 ? void 0 : options.type) || Enums_1.SearchType.All) & Enums_1.SearchType.Art ? 'on' : 'off',
'type-flash': ((options === null || options === void 0 ? void 0 : options.type) || Enums_1.SearchType.All) & Enums_1.SearchType.Flash ? 'on' : 'off',
'type-photo': ((options === null || options === void 0 ? void 0 : options.type) || Enums_1.SearchType.All) & Enums_1.SearchType.Photos ? 'on' : 'off',
'type-music': ((options === null || options === void 0 ? void 0 : options.type) || Enums_1.SearchType.All) & Enums_1.SearchType.Music ? 'on' : 'off',
'type-story': ((options === null || options === void 0 ? void 0 : options.type) || Enums_1.SearchType.All) & Enums_1.SearchType.Story ? 'on' : 'off',
'type-poetry': ((options === null || options === void 0 ? void 0 : options.type) || Enums_1.SearchType.All) & Enums_1.SearchType.Poetry ? 'on' : 'off',
page,
prev_page: (options === null || options === void 0 ? void 0 : options.prev) ? 'Back' : undefined,
next_page: (options === null || options === void 0 ? void 0 : options.prev) ? undefined : 'Next',
mode: 'extended',
q: query
}
});
});
}
exports.FetchSearch = FetchSearch;
function FetchBrowse(options) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${exports.ENDPOINT}/browse`;
return yield request({
url,
method: 'post',
formData: {
'rating_general': ((options === null || options === void 0 ? void 0 : options.rating) || 0x7) & Enums_1.Rating.General ? 'on' : 'off',
'rating_mature': ((options === null || options === void 0 ? void 0 : options.rating) || 0x7) & Enums_1.Rating.Mature ? 'on' : 'off',
'rating_adult': ((options === null || options === void 0 ? void 0 : options.rating) || 0x7) & Enums_1.Rating.Adult ? 'on' : 'off',
'cat': (options === null || options === void 0 ? void 0 : options.category) || 1,
'atype': (options === null || options === void 0 ? void 0 : options.tag) || 1,
'species': (options === null || options === void 0 ? void 0 : options.species) || 1,
'gender': (options === null || options === void 0 ? void 0 : options.gender) || 0,
perpage: (options === null || options === void 0 ? void 0 : options.perpage) || 72,
go: 'Apply',
page: (options === null || options === void 0 ? void 0 : options.page) || 1,
}
});
});
}
exports.FetchBrowse = FetchBrowse;
function FetchGallery(id, page = 1, perpage) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${exports.ENDPOINT}/gallery/${id}/${page}?perpage=${perpage}`;
return yield request({ url });
});
}
exports.FetchGallery = FetchGallery;
function FetchScraps(id, page = 1, perpage) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${exports.ENDPOINT}/scraps/${id}/${page}?perpage=${perpage}`;
return yield request({ url });
});
}
exports.FetchScraps = FetchScraps;
function FetchSubmission(id) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${exports.ENDPOINT}/view/${id}`;
return yield request({ url });
});
}
exports.FetchSubmission = FetchSubmission;
function FetchSubmissions(options) {
return __awaiter(this, void 0, void 0, function* () {
const startAt = typeof (options === null || options === void 0 ? void 0 : options.startAt) === "string" ? "~" + options.startAt : "";
const sort = (options === null || options === void 0 ? void 0 : options.sort) || "new";
const perpage = (options === null || options === void 0 ? void 0 : options.perpage) || 72;
const url = `${exports.ENDPOINT}/msg/submissions/${sort}${startAt}@${perpage}`;
return yield request({ url });
});
}
exports.FetchSubmissions = FetchSubmissions;
function FaveSubmission(favLink) {
return __awaiter(this, void 0, void 0, function* () {
yield request({ url: favLink });
});
}
exports.FaveSubmission = FaveSubmission;
function FetchAuthor(id) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${exports.ENDPOINT}/user/${id}`;
return yield request({ url });
});
}
exports.FetchAuthor = FetchAuthor;
function FetchWatchingList(id, page = 1) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${exports.ENDPOINT}/watchlist/by/${id}/${page}`;
return yield request({ url });
});
}
exports.FetchWatchingList = FetchWatchingList;
function FetchMyWatchingList(page = 1) {
return __awaiter(this, void 0, void 0, function* () {
const url = `${exports.ENDPOINT}/controls/buddylist/${page}`;
return yield request({ url });
});
}
exports.FetchMyWatchingList = FetchMyWatchingList;