Cuu.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.1 KiB

  1. /* eslint-disable import/prefer-default-export */
  2. /* eslint-disable @typescript-eslint/no-unsafe-call */
  3. /* eslint-disable @typescript-eslint/no-unsafe-assignment */
  4. /* eslint-disable @typescript-eslint/no-unsafe-member-access */
  5. /* eslint-disable @typescript-eslint/no-unsafe-return */
  6. import fetchPonyfill from 'fetch-ponyfill';
  7. import * as config from '../config';
  8. export const fetchBooru = async (q: string, page = 0) => {
  9. if (q.trim().length <= 0) {
  10. throw new Error('Specify search terms');
  11. }
  12. const url = new URL(config.gelbooru.apiUrl);
  13. const search = new URLSearchParams({
  14. page: 'dapi',
  15. s: 'post',
  16. api_key: config.gelbooru.apiKey,
  17. user_id: config.gelbooru.userId,
  18. q: 'index',
  19. json: '1',
  20. tags: encodeURIComponent(q),
  21. pid: page.toString(),
  22. });
  23. url.search = search.toString();
  24. const { fetch } = fetchPonyfill();
  25. const response = await fetch(url.toString());
  26. if (response.ok) {
  27. const data = await response.json();
  28. console.log('OK', data);
  29. return data;
  30. }
  31. const data = await response.text();
  32. console.log('NOT OK', data);
  33. throw new Error('Gelbooru API error');
  34. };
  35. export const fetchImage = async (id: string) => {
  36. const url = new URL(config.gelbooru.apiUrl);
  37. const search = new URLSearchParams({
  38. page: 'dapi',
  39. s: 'post',
  40. api_key: config.gelbooru.apiKey,
  41. user_id: config.gelbooru.userId,
  42. q: 'index',
  43. json: '1',
  44. id,
  45. });
  46. url.search = search.toString();
  47. const { fetch } = fetchPonyfill();
  48. const response = await fetch(url.toString());
  49. if (response.ok) {
  50. const data = await response.json();
  51. return data.post[0];
  52. }
  53. throw new Error('Gelbooru API error');
  54. };
  55. export const reverseSearch = async (u: string) => {
  56. const url = new URL(config.saucenao.apiUrl);
  57. const search = new URLSearchParams({
  58. db: '25',
  59. output_type: '2',
  60. testmode: '1',
  61. numres: '16',
  62. api_key: config.saucenao.apiKey,
  63. url: u,
  64. });
  65. url.search = search.toString();
  66. const { fetch } = fetchPonyfill();
  67. const response = await fetch(url.toString());
  68. if (response.ok) {
  69. return response.json();
  70. }
  71. throw new Error('SauceNAO API error');
  72. };