Skip to content

Commit

Permalink
fix: fix supports for mirror sites
Browse files Browse the repository at this point in the history
  • Loading branch information
kuoruan committed Jan 23, 2024
1 parent a8c2ebf commit 1fa494c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
13 changes: 6 additions & 7 deletions src/components/TorrentDownloadItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ td(
</template>

<script>
import { TORRENT_LINK_TAG_REGEX } from "@/utils/misc";
import { getTorrentLinkFromHTML } from "@/utils/misc";
export default {
name: "TorrentDownloadItem",
Expand Down Expand Up @@ -56,20 +56,19 @@ export default {
reject(new Error("下载失败,请重试!"));
},
onload: ({ context = {}, responseText = "" }) => {
let matches;
let torrent;
if (
responseText &&
(matches = responseText.match(TORRENT_LINK_TAG_REGEX)) &&
matches.length >= 3
(torrent = getTorrentLinkFromHTML(responseText))
) {
let url = matches[1];
let url = torrent.href;
if (url.indexOf("//") === 0) {
url = window.location.protocol + url;
}
resolve({
url,
filename: `${matches[2] || context.pageTitle}.torrent`,
url: url,
filename: `${torrent.title || context.pageTitle}.torrent`,
});
} else {
reject(new Error("获取下载链接失败!"));
Expand Down
12 changes: 10 additions & 2 deletions src/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ export function hashCode(str) {
return hash;
}

export const TORRENT_LINK_TAG_REGEX =
/<a(?:.+)href="((?:https?:)?\/\/[a-zA-Z0-9.-]+\/[^"]+\.torrent)"(?:.*)>(.+)?<\/a>/;
const TORRENT_LINK_TAG_REGEX =
/<a(?:.+)href="((?:https?:)?\/\/[a-zA-Z0-9.-]+[^"]+\.torrent)"(?:.*)>(.+)?<\/a>/;

export function getTorrentLinkFromHTML(html) {
const matches = html.match(TORRENT_LINK_TAG_REGEX);
if (matches && matches.length === 3) {
return { href: matches[1].replace(/\n/g, ""), title: matches[2] };
}
return null;
}
38 changes: 35 additions & 3 deletions tests/torrent.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
import { TORRENT_LINK_TAG_REGEX } from "@/utils/misc";
import { it } from "vitest";
import { expect } from "vitest";

import { getTorrentLinkFromHTML } from "@/utils/misc";

describe("Torrent", () => {
it("should match torrent link tag", () => {
it("should match dmhy torrent link tag", () => {
const torrentLinkTag =
'<a href="//dl.dmhy.org/2024/01/23/66e247e9ed66b639a46f2472a2f9fd46f2025fec.torrent">[2024.01.24] プロジェクトセカイ カラフルステージ! feat.初音ミク ワンダーランズ×ショウタイム SEKAI ALBUM Vol.2 [MP3 320K]</a>';
const torrentLinkTag2 =
'<strong>會員專用連接:</strong>&nbsp;<a href="http://dl.dmhy.org/2024/01/23/06abefc6121c0428b6abdddf1b24744fba6f3738.torrent">[MagicStar] SHUT UP EP07 [WEBDL] [1080p]【生】</a>';
const torrentLinkTag3 =
'<a href="https://dl.dmhy.org/2024/01/23/7731c887aeb285ca0373a595c520b3b0cef448e1.torrent">【ASMR】舔耳朵♪[WAV/MP3/MP4]</a>';

expect(getTorrentLinkFromHTML(torrentLinkTag)).toEqual({
href: "//dl.dmhy.org/2024/01/23/66e247e9ed66b639a46f2472a2f9fd46f2025fec.torrent",
title:
"[2024.01.24] プロジェクトセカイ カラフルステージ! feat.初音ミク ワンダーランズ×ショウタイム SEKAI ALBUM Vol.2 [MP3 320K]",
});

expect(getTorrentLinkFromHTML(torrentLinkTag2)).toEqual({
href: "http://dl.dmhy.org/2024/01/23/06abefc6121c0428b6abdddf1b24744fba6f3738.torrent",
title: "[MagicStar] SHUT UP EP07 [WEBDL] [1080p]【生】",
});

expect(getTorrentLinkFromHTML(torrentLinkTag3)).toEqual({
href: "https://dl.dmhy.org/2024/01/23/7731c887aeb285ca0373a595c520b3b0cef448e1.torrent",
title: "【ASMR】舔耳朵♪[WAV/MP3/MP4]",
});
});

it("should match mirror site torrent link tag", () => {
const link1 =
'<strong>會員專用連接:</strong>&nbsp;<a href="//dmhytorrents.b168.net\n/2023/12/11/61e671afe573d1de89dfbfc24e1cea86c2efac04.torrent">【喵萌奶茶屋】★10月新番★[腼腆英雄 / SHY][10][1080p][简日双语][招募翻译]</a>';

expect(torrentLinkTag.match(TORRENT_LINK_TAG_REGEX)).toBeTruthy();
expect(getTorrentLinkFromHTML(link1)).toEqual({
href: "//dmhytorrents.b168.net/2023/12/11/61e671afe573d1de89dfbfc24e1cea86c2efac04.torrent",
title:
"【喵萌奶茶屋】★10月新番★[腼腆英雄 / SHY][10][1080p][简日双语][招募翻译]",
});
});
});

0 comments on commit 1fa494c

Please sign in to comment.