better-ani-scraped 1.2.4 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-ani-scraped",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "Scrape anime data from different sources (only anime-sama.fr for the moment)",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,6 +1,7 @@
1
1
  import axios from "axios";
2
2
  import * as cheerio from "cheerio";
3
3
  import fs from "fs";
4
+ import puppeteer from'puppeteer';
4
5
 
5
6
  const BASE_URL = "https://anime-sama.fr";
6
7
  const CATALOGUE_URL = `${BASE_URL}/catalogue`;
@@ -123,40 +124,36 @@ export async function getSeasons(animeUrl, language = "vostfr") {
123
124
  return seasons;
124
125
  }
125
126
 
126
- import { chromium } from 'playwright';
127
127
 
128
128
  export async function getEpisodeTitles(animeUrl) {
129
- let browser;
130
- try {
131
- browser = await chromium.launch({ headless: true });
132
- const context = await browser.newContext({
133
- locale: 'fr-FR',
134
- userAgent: 'Mozilla/5.0'
135
- });
136
- const page = await context.newPage();
137
- await page.route('**/*', (route) => {
138
- const blocked = ['image', 'stylesheet', 'font', 'media'];
139
- if (blocked.includes(route.request().resourceType())) {
140
- route.abort();
141
- } else {
142
- route.continue();
143
- }
144
- });
145
-
146
- await page.goto(animeUrl, { waitUntil: 'domcontentloaded', timeout: 20000 });
147
- await page.waitForSelector('#selectEpisodes', { timeout: 10000 });
148
- const titres = await page.$$eval('#selectEpisodes option', options =>
149
- options.map(o => o.textContent.trim())
150
- );
151
- return titres;
152
- } catch (error) {
153
- console.error('❌ Erreur dans la récupération des titres :', error);
154
- return [];
155
- } finally {
156
- if (browser) {
157
- await browser.close();
158
- }
159
- }
129
+ let browser;
130
+ try {
131
+ browser = await puppeteer.launch({
132
+ headless: true,
133
+ args: ['--no-sandbox', '--disable-setuid-sandbox']
134
+ });
135
+ const page = await browser.newPage();
136
+ await page.setRequestInterception(true);
137
+ page.on('request', (req) => {
138
+ const blocked = ['image', 'stylesheet', 'font', 'media'];
139
+ if (blocked.includes(req.resourceType())) {
140
+ req.abort();
141
+ } else {
142
+ req.continue();
143
+ }
144
+ });
145
+ await page.goto(animeUrl, { waitUntil: 'domcontentloaded' });
146
+ await page.waitForSelector('#selectEpisodes');
147
+ const titres = await page.$$eval('#selectEpisodes option', options =>
148
+ options.map(o => o.textContent.trim())
149
+ );
150
+ return titres;
151
+ } catch (error) {
152
+ console.error('Erreur dans la récupération des titres:', error);
153
+ return [];
154
+ } finally {
155
+ if (browser) await browser.close();
156
+ }
160
157
  }
161
158
 
162
159
 
@@ -164,8 +161,8 @@ export async function getEmbed(animeUrl, hostPriority = ["vidmoly"]) {
164
161
  const res = await axios.get(animeUrl, {
165
162
  headers: getHeaders(animeUrl.split("/").slice(0, 5).join("/")),
166
163
  });
167
- const $ = cheerio.load(res.data);
168
164
 
165
+ const $ = cheerio.load(res.data);
169
166
  const scriptTag = $('script[src*="episodes.js"]').attr("src");
170
167
  if (!scriptTag) throw new Error("No episodes script found");
171
168
 
@@ -182,7 +179,6 @@ export async function getEmbed(animeUrl, hostPriority = ["vidmoly"]) {
182
179
  ];
183
180
  if (!matches.length) throw new Error("No episode arrays found");
184
181
 
185
- // Parse les arrays et crée une matrice [epsX][index]
186
182
  let episodeMatrix = [];
187
183
  for (const [, , arrayString] of matches) {
188
184
  try {
@@ -193,14 +189,10 @@ export async function getEmbed(animeUrl, hostPriority = ["vidmoly"]) {
193
189
  }
194
190
  }
195
191
 
196
- // Déterminer le nombre total d'épisodes max (plus long des arrays)
197
192
  const maxEpisodes = Math.max(...episodeMatrix.map(arr => arr.length));
198
193
  const finalEmbeds = [];
199
-
200
- // Parcours vertical
201
194
  for (let i = 0; i < maxEpisodes; i++) {
202
195
  let selectedUrl = null;
203
-
204
196
  for (const host of hostPriority) {
205
197
  for (const arr of episodeMatrix) {
206
198
  if (i < arr.length && arr[i].includes(host)) {
@@ -210,17 +202,17 @@ export async function getEmbed(animeUrl, hostPriority = ["vidmoly"]) {
210
202
  }
211
203
  if (selectedUrl) break;
212
204
  }
213
-
214
205
  finalEmbeds.push(selectedUrl || null);
215
206
  }
216
207
 
217
208
  const titles = await getEpisodeTitles(animeUrl);
218
- return titles.slice(0, finalEmbeds.length).map((title, i) => ({
219
- title,
220
- url: finalEmbeds[i]
209
+ return finalEmbeds.map((url, i) => ({
210
+ title: titles[i] || "Untitled",
211
+ url,
221
212
  }));
222
213
  }
223
214
 
215
+
224
216
  export async function getAnimeInfo(animeUrl) {
225
217
  const res = await axios.get(animeUrl, { headers: getHeaders(CATALOGUE_URL) });
226
218
  const $ = cheerio.load(res.data);