-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from aymene69/dev
Fixed torrent service
- Loading branch information
Showing
4 changed files
with
121 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import PQueue from "p-queue"; | ||
import getTorrentInfo from "./getTorrentInfo.js"; | ||
|
||
const wait = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
||
export async function threadedTorrent(itemList, maxResults, maxThread) { | ||
if (maxThread > 1) { | ||
const queue = new PQueue({ concurrency: parseInt(maxThread) }); // Limite le nombre de workers à 5 | ||
if (itemList.length !== 0) { | ||
const items = itemList.slice(0, parseInt(maxResults)); | ||
const filteredItems = await Promise.all( | ||
items.map(async elem => { | ||
return await queue.add(async () => { | ||
let torrentInfo; | ||
console.log("Fetching torrent info for " + elem.title); | ||
torrentInfo = await getTorrentInfo(elem.link); | ||
if (torrentInfo === undefined) { | ||
console.log("Error fetching torrent info for " + elem.title); | ||
console.log("Retrying..."); | ||
let tries = 0; | ||
while (true) { | ||
if (tries > 5) { | ||
console.log("Failed to fetch torrent info for " + elem.title); | ||
break; | ||
} | ||
await wait(5000); | ||
console.log("Retrying..."); | ||
try { | ||
torrentInfo = await getTorrentInfo(elem.link); | ||
} catch (error) { | ||
torrentInfo = undefined; | ||
} | ||
if (torrentInfo !== undefined) { | ||
console.log("Success"); | ||
break; | ||
} | ||
tries += 1; | ||
} | ||
} | ||
if (torrentInfo !== undefined) { | ||
const { infoHash, magnetLink } = torrentInfo; | ||
elem.torrentInfo = torrentInfo; | ||
elem.magnetLink = magnetLink; | ||
return { ...elem, infoHash }; // Ajoute l'availability à l'élément | ||
} | ||
return undefined; | ||
}); | ||
}), | ||
); | ||
// Filtre les éléments en fonction de l'availability | ||
const finalFilteredItems = filteredItems.filter(elem => elem !== undefined); | ||
|
||
return finalFilteredItems; | ||
} | ||
|
||
return []; | ||
} else { | ||
if (itemList.length !== 0) { | ||
const items = itemList.slice(0, parseInt(maxResults)); | ||
const filteredItems = await Promise.all( | ||
items.map(async elem => { | ||
let torrentInfo; | ||
console.log("Fetching torrent info for " + elem.title); | ||
torrentInfo = await getTorrentInfo(elem.link); | ||
if (torrentInfo === undefined) { | ||
console.log("Error fetching torrent info for " + elem.title); | ||
console.log("Retrying..."); | ||
let tries = 0; | ||
while (true) { | ||
if (tries > 5) { | ||
console.log("Failed to fetch torrent info for " + elem.title); | ||
break; | ||
} | ||
await wait(5000); | ||
console.log("Retrying..."); | ||
try { | ||
torrentInfo = await getTorrentInfo(elem.link); | ||
} catch (error) { | ||
torrentInfo = undefined; | ||
} | ||
if (torrentInfo !== undefined) { | ||
console.log("Success"); | ||
break; | ||
} | ||
tries += 1; | ||
} | ||
} | ||
if (torrentInfo !== undefined) { | ||
const { infoHash, magnetLink } = torrentInfo; | ||
elem.torrentInfo = torrentInfo; | ||
elem.magnetLink = magnetLink; | ||
return { ...elem, infoHash }; // Ajoute l'availability à l'élément | ||
} | ||
return undefined; | ||
}), | ||
); | ||
// Filtre les éléments en fonction de l'availability | ||
const finalFilteredItems = filteredItems.filter(elem => elem !== undefined); | ||
|
||
return finalFilteredItems; | ||
} | ||
|
||
return []; | ||
} | ||
} |