-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmapper.js
More file actions
216 lines (179 loc) Β· 6.01 KB
/
mapper.js
File metadata and controls
216 lines (179 loc) Β· 6.01 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const axios = require('axios');
const stringSimilarity = require('string-similarity');
const PROXY_PRO = process.env.PROXY_PRO || "";
const PROXY_PRO2 = process.env.PROXY_PRO2;
// Common headers for API requests
const getCommonHeaders = () => ({
'Accept': 'application/json, text/plain, */*',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36',
'x-site': 'anicrush',
'Referer': 'https://anicrush.to/',
'Origin': 'https://anicrush.to',
'sec-fetch-site': 'same-site',
'sec-fetch-mode': 'cors',
'sec-fetch-dest': 'empty'
});
// Minimal GraphQL query for AniList
const ANILIST_QUERY = `
query ($id: Int) {
Media(id: $id, type: ANIME) {
id
title {
romaji
english
native
}
synonyms
format
seasonYear
}
}`;
// Normalize title
function normalizeTitle(title) {
if (!title) return '';
return title.toLowerCase()
.replace(/[^a-z0-9\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\u4e00-\u9faf\uff00-\uff9f]/g, ' ')
.replace(/\s+/g, ' ')
.trim();
}
// π₯ UPDATED FUNCTION WITH PROPER LOGGING
async function getAniListDetails(anilistId) {
const targetUrl = 'https://graphql.anilist.co';
try {
console.log("β‘ Trying DIRECT AniList request...");
const response = await axios({
url: targetUrl,
method: 'POST',
data: {
query: ANILIST_QUERY,
variables: { id: parseInt(anilistId) }
},
timeout: 5000,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0'
}
});
if (!response.data?.data?.Media) {
throw new Error('Anime not found Direct');
}
console.log("β
DIRECT REQUEST SUCCESS");
return response.data.data.Media;
} catch (error) {
console.error("β DIRECT REQUEST FAILED");
console.error("Status:", error.response?.status || "No response");
console.error("Reason:", error.response?.data || error.message);
// π Fallback to Proxy
try {
const proxyBase = PROXY_PRO2 || PROXY_PRO;
const sep = proxyBase.includes('?') ? '&' : '?';
const proxyUrl = `${proxyBase}${sep}url=${encodeURIComponent(targetUrl)}`;
console.log("π Trying PROXY request...");
console.log("Proxy URL:", proxyUrl);
const response = await axios.post(proxyUrl, {
query: ANILIST_QUERY,
variables: { id: parseInt(anilistId) }
}, {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0'
},
timeout: 10000
});
if (!response.data?.data?.Media) {
throw new Error('Anime not found via Proxy');
}
console.log("β
PROXY REQUEST SUCCESS");
return response.data.data.Media;
} catch (err2) {
console.error("β PROXY REQUEST ALSO FAILED");
console.error("Status:", err2.response?.status || "No response");
console.error("Reason:", err2.response?.data || err2.message);
throw new Error('Failed to fetch anime details from AniList');
}
}
}
// Search anime on anicrush
async function searchAnicrush(title) {
const headers = getCommonHeaders();
const response = await axios({
method: 'GET',
url: 'https://api.anicrush.to/shared/v2/movie/list',
params: {
keyword: title,
page: 1,
limit: 24
},
headers
});
return response.data;
}
// Similarity
function calculateTitleSimilarity(title1, title2) {
if (!title1 || !title2) return 0;
return stringSimilarity.compareTwoStrings(
title1.toLowerCase(),
title2.toLowerCase()
) * 100;
}
function extractSeasonNumber(title) {
if (!title) return null;
const lower = title.toLowerCase();
let match = lower.match(/(?:season|cour|part)\s*(\d{1,2})/i);
if (match && match[1]) return parseInt(match[1], 10);
match = lower.match(/(\d{1,2})\s*$/);
if (match && match[1] && parseInt(match[1], 10) < 10)
return parseInt(match[1], 10);
return null;
}
function findBestMatch(anilistData, anicrushResults) {
if (!anicrushResults?.result?.movies?.length) return null;
const anilistTitles = [
anilistData.title.romaji,
anilistData.title.english,
anilistData.title.native,
...(anilistData.synonyms || [])
].filter(Boolean).map(normalizeTitle);
let bestMatch = null;
let highestScore = 0;
for (const result of anicrushResults.result.movies) {
for (const aTitle of anilistTitles) {
const similarity = calculateTitleSimilarity(aTitle, result.name);
if (similarity > highestScore) {
highestScore = similarity;
bestMatch = result;
}
}
}
return highestScore >= 60 ? bestMatch : null;
}
// Main mapper
async function mapAniListToAnicrush(anilistId) {
const anilistData = await getAniListDetails(anilistId);
const titlesToTry = [
anilistData.title.romaji,
anilistData.title.english,
anilistData.title.native
].filter(Boolean);
let bestMatch = null;
for (const title of titlesToTry) {
const searchResults = await searchAnicrush(title);
bestMatch = findBestMatch(anilistData, searchResults);
if (bestMatch) break;
}
if (!bestMatch)
throw new Error('No matching anime found on anicrush');
return {
anilist_id: anilistId,
anicrush_id: bestMatch.id,
title: bestMatch.name,
type: bestMatch.type,
year: anilistData.seasonYear
};
}
module.exports = {
mapAniListToAnicrush,
getCommonHeaders
};