Skip to content

Commit

Permalink
Refactor all code to something more legible
Browse files Browse the repository at this point in the history
Merge pull request #59 from ErickLimaS/refactor-code
  • Loading branch information
ErickLimaS authored May 28, 2024
2 parents 1070c33 + 4b316dc commit 06b6651
Show file tree
Hide file tree
Showing 130 changed files with 6,589 additions and 5,825 deletions.
50 changes: 31 additions & 19 deletions app/api/anilist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import {
defaultApiQueryRequest, getCurrentSeason,
mediaAiringApiQueryRequest, mediaByIdQueryRequest,
mediaTrendingApiQueryRequest
} from './anilistQueryFunctions'
} from './anilistQueryConstants'
import { cache } from 'react'
import axiosRetry from 'axios-retry'

const BASE_URL: string = 'https://graphql.anilist.co/'
const BASE_URL = 'https://graphql.anilist.co/'

const headers = {
'Content-Type': 'application/json',
}

// returns medias with adult content
function filterAdultContent(data: ApiDefaultResult[] | ApiAiringMidiaResults[], reponseType?: "mediaByFormat") {
function filterMediasWithAdultContent(mediasList: ApiDefaultResult[] | ApiAiringMidiaResults[], reponseType?: "mediaByFormat") {

if (reponseType == "mediaByFormat") {
const filteredData = (data as ApiDefaultResult[]).filter((item) => item.isAdult == false)
const mediasFiltered = (mediasList as ApiDefaultResult[]).filter((item) => item.isAdult == false)

return filteredData
return mediasFiltered
}
else {
const filteredData = (data as ApiAiringMidiaResults[]).filter((item) => item.media.isAdult == false)
const mediasFiltered = (mediasList as ApiAiringMidiaResults[]).filter((item) => item.media.isAdult == false)

return filteredData
return mediasFiltered
}

}
Expand All @@ -43,17 +43,17 @@ axiosRetry(Axios, {
export default {

// HOME PAGE
getNewReleases: cache(async (
getNewReleases: cache(async ({ type, format, sort, showAdultContent, status, page, perPage }: {
type: string,
format?: string,
sort?: string,
showAdultContent?: boolean,
status?: "FINISHED" | "RELEASING" | "NOT_YET_RELEASED" | "CANCELLED" | "HIATUS",
page?: number,
perPage?: number
) => {
}) => {

const season: string = getCurrentSeason()
const season = getCurrentSeason()

try {

Expand Down Expand Up @@ -92,7 +92,7 @@ export default {
}),

//SEARCH
getSeachResults: cache(async (query: string, showAdultContent?: boolean) => {
getSeachResults: cache(async ({ query, showAdultContent }: { query: string, showAdultContent?: boolean }) => {

try {

Expand All @@ -115,7 +115,7 @@ export default {
})

return showAdultContent ?
data.data.Page.media as ApiDefaultResult[] : filterAdultContent(data.data.Page.media, "mediaByFormat")
data.data.Page.media as ApiDefaultResult[] : filterMediasWithAdultContent(data.data.Page.media, "mediaByFormat")

}
catch (error: any) {
Expand All @@ -128,7 +128,7 @@ export default {
}),

// RELEASING THIS WEEK
getReleasingThisWeek: cache(async (type: string, format?: string, page?: number, showAdultContent?: boolean) => {
getReleasingThisWeek: cache(async ({ type, format, page, showAdultContent }: { type: string, format?: string, page?: number, showAdultContent?: boolean }) => {

try {

Expand Down Expand Up @@ -168,7 +168,13 @@ export default {
}),

// RELEASING BY DAYS RANGE
getReleasingByDaysRange: cache(async (type: string, days: 1 | 7 | 30, pageNumber?: number, perPage?: number, showAdultContent?: boolean) => {
getReleasingByDaysRange: cache(async ({ type, days, pageNumber, perPage, showAdultContent }: {
type: string,
days: 1 | 7 | 30,
pageNumber?: number,
perPage?: number,
showAdultContent?: boolean
}) => {

try {

Expand Down Expand Up @@ -198,7 +204,7 @@ export default {
})

return showAdultContent ?
data.data.Page.airingSchedules as ApiAiringMidiaResults[] : filterAdultContent(data.data.Page.airingSchedules) as ApiAiringMidiaResults[]
data.data.Page.airingSchedules as ApiAiringMidiaResults[] : filterMediasWithAdultContent(data.data.Page.airingSchedules) as ApiAiringMidiaResults[]

}
catch (error: any) {
Expand All @@ -212,7 +218,7 @@ export default {
}),

// TRENDING
getTrendingMedia: cache(async (sort?: string, showAdultContent?: boolean) => {
getTrendingMedia: cache(async ({ sort, showAdultContent }: { sort?: string, showAdultContent?: boolean }) => {

try {

Expand Down Expand Up @@ -251,7 +257,13 @@ export default {
}),

// MEDIAS WITH INDICATED FORMAT
getMediaForThisFormat: cache(async (type: string, sort?: string, pageNumber?: number, perPage?: number, showAdultContent?: boolean) => {
getMediaForThisFormat: cache(async ({ type, sort, pageNumber, perPage, showAdultContent }: {
type: string,
sort?: string,
pageNumber?: number,
perPage?: number,
showAdultContent?: boolean
}) => {

try {

Expand All @@ -274,7 +286,7 @@ export default {
})

return showAdultContent ?
data.data.Page.media as ApiDefaultResult[] : filterAdultContent(data.data.Page.media, "mediaByFormat") as ApiDefaultResult[]
data.data.Page.media as ApiDefaultResult[] : filterMediasWithAdultContent(data.data.Page.media, "mediaByFormat") as ApiDefaultResult[]

}
catch (error: any) {
Expand All @@ -288,7 +300,7 @@ export default {
}),

// GET MEDIA INFO BY ID
getMediaInfo: cache(async (id: number, showAdultContent?: boolean) => {
getMediaInfo: cache(async ({ id, showAdultContent }: { id: number, showAdultContent?: boolean }) => {

try {

Expand Down
File renamed without changes.
113 changes: 65 additions & 48 deletions app/api/animes-database/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,96 @@ import { MediaDbOffline } from "@/app/ts/interfaces/dbOffilineInterface";

export async function GET(request: NextRequest) {

const params = request.nextUrl.searchParams
const searchParams = request.nextUrl.searchParams

const resultLimit = 12
const resultsLimit = 12

let dataSort = (AnimeDataOffline as { data: MediaDbOffline[] }).data
let dataToBeSorted = (AnimeDataOffline as { data: MediaDbOffline[] }).data

if (params.get("type")) dataSort = dataSort.filter((item: { type: string }) => item.type == params.get("type")!.toUpperCase())
if (searchParams.get("type")) {
dataToBeSorted = dataToBeSorted.filter((media) => media.type == searchParams.get("type")!.toUpperCase())
}

if (params.get("genre")) dataSort = dataSort.filter((item: { tags: string[] }) => item.tags.some(a => (params.get("genre")!.includes(a))))
if (searchParams.get("year")) {
dataToBeSorted = dataToBeSorted.filter((media) => media.animeSeason.year == Number(searchParams.get("year")))
}

if (params.get("status")) dataSort = dataSort.filter((item: { status: string }) => item.status == params.get("status")!.toUpperCase())
if (searchParams.get("genre")) {
dataToBeSorted = dataToBeSorted.filter((media) => media.tags.some(genreName => (searchParams.get("genre")!.includes(genreName))))
}

if (params.get("year")) dataSort = dataSort.filter((item: { animeSeason: { year: number } }) => item.animeSeason.year == Number(params.get("year")))
if (searchParams.get("status")) {
dataToBeSorted = dataToBeSorted.filter((media) => media.status == searchParams.get("status")!.toUpperCase())
}

if (params.get("title")) dataSort = dataSort.filter((item: { title: string }) => item.title.toLowerCase().includes(params.get("title")!.toLowerCase()))
if (searchParams.get("title")) {
dataToBeSorted = dataToBeSorted.filter((media) => media.title.toLowerCase().includes(searchParams.get("title")!.toLowerCase()))
}

if (params.get("season")) dataSort = dataSort.filter((item: { animeSeason: { season: string } }) =>
item.animeSeason.season.toLocaleLowerCase() == params.get("season")?.toLocaleLowerCase()
)
if (searchParams.get("season")) {
dataToBeSorted = dataToBeSorted.filter((media) =>
media.animeSeason.season.toLocaleLowerCase() == searchParams.get("season")?.toLocaleLowerCase()
)
}

if (params.get("sort")) {
if (params.get("sort") == "releases_desc") {
dataSort = dataSort.sort(
(a: { animeSeason: { year: number } }, b: { animeSeason: { year: number } }) => a.animeSeason.year - b.animeSeason.year
).reverse()
}
else if (params.get("sort") == "releases_asc") {
dataSort = dataSort.sort(
(a: { animeSeason: { year: number } }, b: { animeSeason: { year: number } }) => a.animeSeason.year - b.animeSeason.year
)
}
switch (searchParams.get("sort")) {

if (params.get("sort") == "title_desc") {
dataSort = dataSort.sort(
(a: { title: string }, b: { title: string }) => a.title > b.title ? -1 : 1
)
}
else if (params.get("sort") == "title_asc") {
dataSort = dataSort.sort(
(a: { title: string }, b: { title: string }) => a.title > b.title ? -1 : 1
).reverse()
}
}
case "releases_desc":

dataToBeSorted = dataToBeSorted.sort((a, b) => a.animeSeason.year - b.animeSeason.year).reverse()

break

case "releases_asc":

const totalLength = dataSort.length
dataToBeSorted = dataToBeSorted.sort((a, b) => a.animeSeason.year - b.animeSeason.year)

dataSort = dataSort.slice(0, resultLimit * Number(params.get("page") || 1))
break

case "title_desc":

dataToBeSorted = dataToBeSorted.sort((a, b) => a.title > b.title ? -1 : 1)

break

case "title_asc":

dataToBeSorted = dataToBeSorted.sort((a, b) => a.title > b.title ? -1 : 1).reverse()

break

default:
break

}

const totalResultsLength = dataToBeSorted.length

// GET ANILIST ID FOR EACH MEDIA
if (dataSort) {
if (dataToBeSorted.length > 0) {

let sortHasAnilistId
const mediasWithAnilistId = dataToBeSorted.filter((media) => media.sources.map(source => {

sortHasAnilistId = dataSort.filter((item) => item.sources.map(a => {
if (source.includes("https://anilist.co/anime")) {

if (a.includes("https://anilist.co/anime")) {
const foundUrl: string | null = a.slice(a.search(/\banime\b/))
item.anilistId = foundUrl!.slice(6)
}
const urlWithAnilistId = source.slice(source.search(/\banime\b/))

}))
media.anilistId = urlWithAnilistId!.slice(6)

// filter only itens which has the anilist ID
sortHasAnilistId = sortHasAnilistId.filter((item) => item.anilistId)
}

})).filter((item) => item.anilistId)

dataSort = sortHasAnilistId
dataToBeSorted = mediasWithAnilistId

}

const resultsLimitedByPage = dataToBeSorted.slice(0, resultsLimit * Number(searchParams.get("page") || 1))

return NextResponse.json(
{
data: dataSort,
allResultsLength: totalLength,
data: resultsLimitedByPage,
allResultsLength: totalResultsLength,
lastUpdate: (AnimeDataOffline as { lastUpdate: string }).lastUpdate
}
)
Expand Down
6 changes: 3 additions & 3 deletions app/api/aniwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ axiosRetry(Axios, {
export default {

// SEARCH MEDIA
searchMedia: cache(async (query: string, page?: number) => {
searchMedia: cache(async ({ query, page }: { query: string, page?: number }) => {

try {

Expand All @@ -38,7 +38,7 @@ export default {
}),

// GET EPISODES, NO LINKS INCLUDED
getEpisodes: cache(async (episodeId: string) => {
getEpisodes: cache(async ({ episodeId }: { episodeId: string }) => {

try {

Expand All @@ -59,7 +59,7 @@ export default {
}),

// GET EPISODES, NO LINKS INCLUDED
episodesLinks: cache(async (episodeId: string, server?: string, category?: "dub" | "sub") => {
episodesLinks: cache(async ({ episodeId, server, category }: { episodeId: string, server?: string, category?: "dub" | "sub" }) => {

try {

Expand Down
8 changes: 4 additions & 4 deletions app/api/consumetGoGoAnime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ axiosRetry(Axios, {
export default {

// SEARCH ANIME BY QUERY
searchMedia: cache(async (query: string, page?: number) => {
searchMedia: cache(async ({ query, page }: { query: string, page?: number }) => {

try {

Expand All @@ -43,7 +43,7 @@ export default {
}),

// GET ANIME INFO
getInfoFromThisMedia: cache(async (id: string | number) => {
getInfoFromThisMedia: cache(async ({ id }: { id: string | number }) => {

try {

Expand All @@ -65,7 +65,7 @@ export default {
}),

// GET EPISODES LINKS FOR ANIMES AND MOVIES
getEpisodeStreamingLinks: cache(async (episodeId: string | number, serverName?: string) => {
getEpisodeStreamingLinks: cache(async ({ episodeId, serverName }: { episodeId: string | number, serverName?: string }) => {

try {
const { data } = await Axios({
Expand All @@ -86,7 +86,7 @@ export default {
}),

// ALTERNATIVE: GET EPISODES LINKS FOR ANIMES AND MOVIES
getEpisodeStreamingLinks2: cache(async (episodeId: string) => {
getEpisodeStreamingLinks2: cache(async ({ episodeId }: { episodeId: string }) => {

try {
const { data } = await Axios({
Expand Down
14 changes: 10 additions & 4 deletions app/api/consumetImdb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import stringToOnlyAlphabetic from "@/app/lib/convertStringsTo"
import stringToOnlyAlphabetic from "@/app/lib/convertStrings"
import { ImdbMediaInfo, ImdbSearchItem } from "@/app/ts/interfaces/apiImdbInterface"
import Axios from "axios"
import axiosRetry from "axios-retry"
Expand All @@ -15,7 +15,7 @@ axiosRetry(Axios, {
})

// SEARCH BY MEDIA TITLE
export const searchMedia = cache(async (mediaTitle: string) => {
export const searchMedia = cache(async ({ mediaTitle }: { mediaTitle: string }) => {

try {

Expand All @@ -37,7 +37,13 @@ export const searchMedia = cache(async (mediaTitle: string) => {
})

// GET INFO FOR THIS MEDIA
export const getMediaInfo = cache(async (search: boolean, mediaId?: string, type?: "TV Series", seachTitle?: string, releaseYear?: number) => {
export const getMediaInfo = cache(async ({ search, mediaId, type, seachTitle, releaseYear }: {
search: boolean,
mediaId?: string,
type?: "TV Series",
seachTitle?: string,
releaseYear?: number
}) => {

try {

Expand All @@ -46,7 +52,7 @@ export const getMediaInfo = cache(async (search: boolean, mediaId?: string, type

if (search && seachTitle) {

const searchResults: ImdbSearchItem[] = await searchMedia(stringToOnlyAlphabetic(seachTitle)).then(res => res.results)
const searchResults: ImdbSearchItem[] = await searchMedia({ mediaTitle: stringToOnlyAlphabetic(seachTitle) }).then(res => res.results)

const filteredRes = searchResults.find((item) => Number(item.releaseDate) == releaseYear)

Expand Down
Loading

0 comments on commit 06b6651

Please sign in to comment.