|
- /* eslint-disable import/prefer-default-export */
- /* eslint-disable @typescript-eslint/no-unsafe-call */
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
- /* eslint-disable @typescript-eslint/no-unsafe-return */
-
- import fetchPonyfill from 'fetch-ponyfill';
- import * as config from '../config';
-
- export const fetchBooru = async (q: string, page = 0) => {
- if (q.trim().length <= 0) {
- throw new Error('Specify search terms');
- }
-
- const url = new URL(config.gelbooru.apiUrl);
- const search = new URLSearchParams({
- page: 'dapi',
- s: 'post',
- api_key: config.gelbooru.apiKey,
- user_id: config.gelbooru.userId,
- q: 'index',
- json: '1',
- tags: encodeURIComponent(q),
- pid: page.toString(),
- });
- url.search = search.toString();
- const { fetch } = fetchPonyfill();
- const response = await fetch(url.toString());
- if (response.ok) {
- const data = await response.json();
- console.log('OK', data);
- return data;
- }
-
- const data = await response.text();
- console.log('NOT OK', data);
- throw new Error('Gelbooru API error');
- };
-
- export const fetchImage = async (id: string) => {
- const url = new URL(config.gelbooru.apiUrl);
- const search = new URLSearchParams({
- page: 'dapi',
- s: 'post',
- api_key: config.gelbooru.apiKey,
- user_id: config.gelbooru.userId,
- q: 'index',
- json: '1',
- id,
- });
- url.search = search.toString();
- const { fetch } = fetchPonyfill();
- const response = await fetch(url.toString());
- if (response.ok) {
- const data = await response.json();
- return data.post[0];
- }
- throw new Error('Gelbooru API error');
- };
-
- export const reverseSearch = async (u: string) => {
- const url = new URL(config.saucenao.apiUrl);
- const search = new URLSearchParams({
- db: '25',
- output_type: '2',
- testmode: '1',
- numres: '16',
- api_key: config.saucenao.apiKey,
- url: u,
- });
- url.search = search.toString();
- const { fetch } = fetchPonyfill();
- const response = await fetch(url.toString());
- if (response.ok) {
- return response.json();
- }
- throw new Error('SauceNAO API error');
- };
|