aboutsummaryrefslogtreecommitdiff
path: root/lib/anilist/aniAdvanceSearch.js
blob: 7e684afe66bdc5ad9e0b2354a7e6938121f57d5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { advanceSearchQuery } from "../graphql/query";

export async function aniAdvanceSearch({
  search,
  type,
  genres,
  page,
  sort,
  format,
  season,
  seasonYear,
  perPage,
}) {
  const categorizedGenres = genres?.reduce((result, item) => {
    const existingEntry = result[item.type];

    if (existingEntry) {
      existingEntry.push(item.value);
    } else {
      result[item.type] = [item.value];
    }

    return result;
  }, {});

  if (type === "MANGA") {
    const response = await fetch("https://api.anify.tv/search-advanced", {
      method: "POST",
      body: JSON.stringify({
        sort: "averageRating",
        sortDirection: "DESC",
        ...(categorizedGenres && { ...categorizedGenres }),
        ...(search && { query: search }),
        ...(page && { page: page }),
        ...(perPage && { perPage: perPage }),
        ...(format && { format: [format] }),
        ...(seasonYear && { year: Number(seasonYear) }),
        ...(type && { type: format === "NOVEL" ? "novel" : type }),
      }),
    });

    const data = await response.json();
    return {
      pageInfo: {
        hasNextPage: data.length >= (perPage ?? 20),
        currentPage: page,
        lastPage: Math.ceil(data.length / (perPage ?? 20)),
        perPage: perPage ?? 20,
        total: data.length,
      },
      media: data.map((item) => ({
        averageScore: item.averageRating,
        bannerImage: item.bannerImage,
        chapters: item.totalChapters,
        coverImage: {
          color: item.color,
          extraLarge: item.coverImage,
          large: item.coverImage,
        },
        description: item.description,
        duration: item.duration ?? null,
        endDate: {
          day: null,
          month: null,
          year: null,
        },
        mappings: item.mappings,
        format: item.format,
        genres: item.genres,
        id: item.id,
        isAdult: false,
        mediaListEntry: null,
        nextAiringEpisode: null,
        popularity: item.averagePopularity,
        season: null,
        seasonYear: item.year,
        startDate: {
          day: null,
          month: null,
          year: item.year,
        },
        status: item.status,
        studios: { edges: [] },
        title: {
          userPreferred:
            item.title.english ?? item.title.romaji ?? item.title.native,
        },
        type: item.type,
        volumes: item.totalVolumes ?? null,
      })),
    };
  } else {
    const response = await fetch("https://graphql.anilist.co/", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        query: advanceSearchQuery,
        variables: {
          ...(search && {
            search: search,
            ...(!sort && { sort: "SEARCH_MATCH" }),
          }),
          ...(type && { type: type }),
          ...(seasonYear && { seasonYear: seasonYear }),
          ...(season && {
            season: season,
            ...(!seasonYear && { seasonYear: new Date().getFullYear() }),
          }),
          ...(categorizedGenres && { ...categorizedGenres }),
          ...(format && { format: format }),
          // ...(genres && { genres: genres }),
          // ...(tags && { tags: tags }),
          ...(perPage && { perPage: perPage }),
          ...(sort && { sort: sort }),
          ...(page && { page: page }),
        },
      }),
    });

    const datas = await response.json();
    // console.log(datas);
    const data = datas.data.Page;
    return data;
  }
}