wyzie-lib 2.2.6 → 2.2.7

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": "wyzie-lib",
3
- "version": "2.2.6",
3
+ "version": "2.2.7",
4
4
  "icon": "https://i.postimg.cc/L5ppKYC5/cclogo.png",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -60,8 +60,8 @@
60
60
  "url": "git+https://github.com/itzcozi/wyzie-lib.git"
61
61
  },
62
62
  "devDependencies": {
63
- "prettier": "^3.6.0",
64
- "typescript": "^5.8.3",
63
+ "prettier": "^3.7.4",
64
+ "typescript": "^5.9.3",
65
65
  "vite": "^4.5.14",
66
66
  "vite-plugin-dts": "^4.5.4",
67
67
  "vitest": "^2.1.9"
package/lib/main.amd.js DELETED
@@ -1,117 +0,0 @@
1
- define(["exports"], function(exports) {
2
- "use strict";
3
- const config = {
4
- baseUrl: "https://sub.wyzie.ru"
5
- };
6
- function configure(options) {
7
- if (options.baseUrl) {
8
- config.baseUrl = options.baseUrl.replace(/\/$/, "");
9
- }
10
- }
11
- async function constructUrl({
12
- tmdb_id,
13
- imdb_id,
14
- season,
15
- episode,
16
- encoding,
17
- language,
18
- format,
19
- source,
20
- hi,
21
- ...extraParams
22
- }) {
23
- if (!tmdb_id && !imdb_id) {
24
- throw new Error("Either tmdb_id or imdb_id must be provided.");
25
- }
26
- const hasSeason = season !== void 0;
27
- const hasEpisode = episode !== void 0;
28
- if (hasSeason && !hasEpisode || !hasSeason && hasEpisode) {
29
- throw new Error("Season and episode must be provided together or omitted together.");
30
- }
31
- const url = new URL(`${config.baseUrl}/search`);
32
- const queryParams = {
33
- id: String(tmdb_id || imdb_id),
34
- season,
35
- episode,
36
- encoding: Array.isArray(encoding) ? encoding.join(",") : encoding,
37
- language: Array.isArray(language) ? language.join(",") : language,
38
- format: Array.isArray(format) ? format.join(",") : format,
39
- source: Array.isArray(source) ? source.join(",") : source,
40
- hi
41
- };
42
- Object.entries(queryParams).forEach(([key, value]) => {
43
- if (value !== void 0) {
44
- url.searchParams.append(key, String(value));
45
- }
46
- });
47
- Object.entries(extraParams).forEach(([key, value]) => {
48
- if (value !== void 0) {
49
- if (Array.isArray(value)) {
50
- url.searchParams.append(key, value.join(","));
51
- } else {
52
- url.searchParams.append(key, String(value));
53
- }
54
- }
55
- });
56
- return url;
57
- }
58
- async function fetchSubtitles(url) {
59
- const response = await fetch(url.toString());
60
- if (!response.ok) {
61
- throw new Error(`HTTP error! status: ${response.status}`);
62
- }
63
- return response.json();
64
- }
65
- async function searchSubtitles(params) {
66
- try {
67
- const url = await constructUrl(params);
68
- return await fetchSubtitles(url);
69
- } catch (error) {
70
- throw new Error(`Error fetching subtitles: ${error}`);
71
- }
72
- }
73
- async function parseToVTT(subtitleUrl) {
74
- try {
75
- const response = await fetch(subtitleUrl);
76
- if (!response.ok) {
77
- throw new Error(`Failed to fetch subtitle content: ${response.status}`);
78
- }
79
- const content = await response.text();
80
- const normalizedContent = content.replace(/\r\n|\r/g, "\n").trim();
81
- const blocks = normalizedContent.split(/\n\n+/);
82
- const timestampRegex = /^\d{1,2}:\d{2}:\d{2}[,.]\d{3}\s*-->\s*\d{1,2}:\d{2}:\d{2}[,.]\d{3}$/;
83
- const hasValidSRTFormat = blocks.some((block) => {
84
- const lines = block.split("\n").map((line) => line.trim());
85
- return lines.some((line) => timestampRegex.test(line));
86
- });
87
- if (!hasValidSRTFormat) {
88
- throw new Error("Invalid subtitle format: not SRT");
89
- }
90
- const vttLines = ["WEBVTT", ""];
91
- for (const block of blocks) {
92
- const lines = block.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
93
- if (lines.length < 2)
94
- continue;
95
- const timestampIndex = lines.findIndex((line) => timestampRegex.test(line));
96
- if (timestampIndex === -1)
97
- continue;
98
- const textLines = lines.slice(timestampIndex + 1).filter((line) => !/^\d+$/.test(line));
99
- if (textLines.length === 0)
100
- continue;
101
- let timestampLine = lines[timestampIndex];
102
- timestampLine = timestampLine.replace(/[,.](?=\s*-->)/, "").replace(/[,.]$/, "").replace(/,(\d{3})/g, ".$1");
103
- vttLines.push(`${timestampLine}
104
- ${textLines.join("\n")}
105
- `);
106
- }
107
- return vttLines.join("\n").replace(/\n{3,}/g, "\n\n").trim() + "\n\n";
108
- } catch (error) {
109
- console.error("Error in parseToVTT:", error);
110
- throw error;
111
- }
112
- }
113
- exports.configure = configure;
114
- exports.parseToVTT = parseToVTT;
115
- exports.searchSubtitles = searchSubtitles;
116
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
117
- });
package/lib/main.cjs DELETED
@@ -1,115 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const config = {
4
- baseUrl: "https://sub.wyzie.ru"
5
- };
6
- function configure(options) {
7
- if (options.baseUrl) {
8
- config.baseUrl = options.baseUrl.replace(/\/$/, "");
9
- }
10
- }
11
- async function constructUrl({
12
- tmdb_id,
13
- imdb_id,
14
- season,
15
- episode,
16
- encoding,
17
- language,
18
- format,
19
- source,
20
- hi,
21
- ...extraParams
22
- }) {
23
- if (!tmdb_id && !imdb_id) {
24
- throw new Error("Either tmdb_id or imdb_id must be provided.");
25
- }
26
- const hasSeason = season !== void 0;
27
- const hasEpisode = episode !== void 0;
28
- if (hasSeason && !hasEpisode || !hasSeason && hasEpisode) {
29
- throw new Error("Season and episode must be provided together or omitted together.");
30
- }
31
- const url = new URL(`${config.baseUrl}/search`);
32
- const queryParams = {
33
- id: String(tmdb_id || imdb_id),
34
- season,
35
- episode,
36
- encoding: Array.isArray(encoding) ? encoding.join(",") : encoding,
37
- language: Array.isArray(language) ? language.join(",") : language,
38
- format: Array.isArray(format) ? format.join(",") : format,
39
- source: Array.isArray(source) ? source.join(",") : source,
40
- hi
41
- };
42
- Object.entries(queryParams).forEach(([key, value]) => {
43
- if (value !== void 0) {
44
- url.searchParams.append(key, String(value));
45
- }
46
- });
47
- Object.entries(extraParams).forEach(([key, value]) => {
48
- if (value !== void 0) {
49
- if (Array.isArray(value)) {
50
- url.searchParams.append(key, value.join(","));
51
- } else {
52
- url.searchParams.append(key, String(value));
53
- }
54
- }
55
- });
56
- return url;
57
- }
58
- async function fetchSubtitles(url) {
59
- const response = await fetch(url.toString());
60
- if (!response.ok) {
61
- throw new Error(`HTTP error! status: ${response.status}`);
62
- }
63
- return response.json();
64
- }
65
- async function searchSubtitles(params) {
66
- try {
67
- const url = await constructUrl(params);
68
- return await fetchSubtitles(url);
69
- } catch (error) {
70
- throw new Error(`Error fetching subtitles: ${error}`);
71
- }
72
- }
73
- async function parseToVTT(subtitleUrl) {
74
- try {
75
- const response = await fetch(subtitleUrl);
76
- if (!response.ok) {
77
- throw new Error(`Failed to fetch subtitle content: ${response.status}`);
78
- }
79
- const content = await response.text();
80
- const normalizedContent = content.replace(/\r\n|\r/g, "\n").trim();
81
- const blocks = normalizedContent.split(/\n\n+/);
82
- const timestampRegex = /^\d{1,2}:\d{2}:\d{2}[,.]\d{3}\s*-->\s*\d{1,2}:\d{2}:\d{2}[,.]\d{3}$/;
83
- const hasValidSRTFormat = blocks.some((block) => {
84
- const lines = block.split("\n").map((line) => line.trim());
85
- return lines.some((line) => timestampRegex.test(line));
86
- });
87
- if (!hasValidSRTFormat) {
88
- throw new Error("Invalid subtitle format: not SRT");
89
- }
90
- const vttLines = ["WEBVTT", ""];
91
- for (const block of blocks) {
92
- const lines = block.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
93
- if (lines.length < 2)
94
- continue;
95
- const timestampIndex = lines.findIndex((line) => timestampRegex.test(line));
96
- if (timestampIndex === -1)
97
- continue;
98
- const textLines = lines.slice(timestampIndex + 1).filter((line) => !/^\d+$/.test(line));
99
- if (textLines.length === 0)
100
- continue;
101
- let timestampLine = lines[timestampIndex];
102
- timestampLine = timestampLine.replace(/[,.](?=\s*-->)/, "").replace(/[,.]$/, "").replace(/,(\d{3})/g, ".$1");
103
- vttLines.push(`${timestampLine}
104
- ${textLines.join("\n")}
105
- `);
106
- }
107
- return vttLines.join("\n").replace(/\n{3,}/g, "\n\n").trim() + "\n\n";
108
- } catch (error) {
109
- console.error("Error in parseToVTT:", error);
110
- throw error;
111
- }
112
- }
113
- exports.configure = configure;
114
- exports.parseToVTT = parseToVTT;
115
- exports.searchSubtitles = searchSubtitles;
package/lib/main.d.ts DELETED
@@ -1,122 +0,0 @@
1
- /**
2
- * Type for the configuration options for the library.
3
- */
4
- export declare type ConfigurationOptions = {
5
- /** The API's hostname (default: sub.wyzie.ru) */
6
- baseUrl: string;
7
- };
8
-
9
- /**
10
- * Configure the library settings.
11
- *
12
- * @param {ConfigurationOptions} options - Config options for the library.
13
- * @throws {Error} Throws an error if the baseUrl is not provided.
14
- */
15
- export declare function configure(options: ConfigurationOptions): void;
16
-
17
- /**
18
- * Parses subtitle content from a URL to VTT format.
19
- *
20
- * @param {string} subtitleUrl - The URL of the subtitle to parse.
21
- * @returns {Promise<string>} A promise that resolves to the subtitle content in VTT format.
22
- * @throws {Error} Throws an error if fetching or parsing the subtitle content fails.
23
- */
24
- export declare function parseToVTT(subtitleUrl: string): Promise<string>;
25
-
26
- /**
27
- * Parameters used to construct the URL for subtitle search (requires an ID).
28
- */
29
- export declare type QueryParams = {
30
- /** Unique identifier (either TMDB or IMDB ID). */
31
- id: string;
32
- /** Season number if the content is a series. */
33
- season?: number;
34
- /** Episode number if the content is a series. */
35
- episode?: number;
36
- /** Encoding of the subtitle files. */
37
- encoding?: string;
38
- /** ISO 3166 code of the subtitle desired. */
39
- language?: string;
40
- /** Which subtitle file format you want */
41
- format?: string;
42
- /** Determines if you get a hearing impaired subtitles */
43
- hi?: boolean;
44
- /** The source where the subtitle will be scraped from. */
45
- source?: string;
46
- };
47
-
48
- /**
49
- * Searches for subtitles based on the provided parameters.
50
- *
51
- * @param {SearchSubtitlesParams} params - The parameters for searching: SearchSubtitlesParams.
52
- * @returns {Promise<SubtitleData[]>} A promise that resolves to an array of subtitle data.
53
- * @throws {Error} Throws an error if fetching subtitles fails or something goes wrong.
54
- */
55
- export declare function searchSubtitles(params: SearchSubtitlesParams): Promise<SubtitleData[]>;
56
-
57
- /**
58
- * Parameters for searching subtitles.
59
- * Either IMDB or TMDB ID is required and if episode is provided, season is also required.
60
- */
61
- export declare type SearchSubtitlesParams = (
62
- /** The TMDB ID of the media you want subtitles for (either TMDB or IMDB ID). */
63
- {
64
- tmdb_id: number;
65
- imdb_id?: never;
66
- }
67
- /** The IMDB ID of the media you want subtitles for (either TMDB or IMDB ID). */
68
- | {
69
- imdb_id: string;
70
- tmdb_id?: never;
71
- }) & {
72
- /** ISO 3166 code or codes of the subtitle desired. */
73
- language?: string | string[];
74
- /** The subtitle file's character encoding or encodings. */
75
- encoding?: string | string[];
76
- /** Which subtitle file format(s) you want. */
77
- format?: string | string[];
78
- /** Determines if you get hearing impaired subtitles. */
79
- hi?: boolean;
80
- /** The source where the subtitle will be scraped. Accepts a single value or a list. */
81
- source?: string | string[];
82
- /** Additional parameters that can be used for filtering or other purposes. */
83
- [key: string]: any;
84
- } & (
85
- /** The number of the desired season you want subtitles for. */
86
- {
87
- season: number;
88
- episode: number;
89
- }
90
- /** The number of the desired episode you want subtitles for. */
91
- | {
92
- season?: never;
93
- episode?: never;
94
- });
95
-
96
- /**
97
- * Data structure representing a single subtitle object.
98
- */
99
- export declare type SubtitleData = {
100
- /** Unique identifier (either TMDB or IMDB ID). */
101
- id: string;
102
- /** The subtitle file's URL. */
103
- url: string;
104
- /** The format of the subtitle file. */
105
- format: string;
106
- /** The subtitle file's character encoding. (UTF-8, ASCII, ETC) */
107
- encoding: string;
108
- /** Boolean indicating if the subtitle's is hearing impaired. */
109
- isHearingImpaired: boolean;
110
- /** URL to a PNG of the flag of the subtitle's language. */
111
- flagUrl: string;
112
- /** The name/title of the media. */
113
- media: string;
114
- /** The display language; Example: English. */
115
- display: string;
116
- /** ISO 3166 code; Example: en (2 alphabetic letters). */
117
- language: string;
118
- /** The subtitle's source (ex: subdl, subf2m, opensubtitles). */
119
- source?: string | string[];
120
- };
121
-
122
- export { }
package/lib/main.iife.js DELETED
@@ -1,118 +0,0 @@
1
- var main = function(exports) {
2
- "use strict";
3
- const config = {
4
- baseUrl: "https://sub.wyzie.ru"
5
- };
6
- function configure(options) {
7
- if (options.baseUrl) {
8
- config.baseUrl = options.baseUrl.replace(/\/$/, "");
9
- }
10
- }
11
- async function constructUrl({
12
- tmdb_id,
13
- imdb_id,
14
- season,
15
- episode,
16
- encoding,
17
- language,
18
- format,
19
- source,
20
- hi,
21
- ...extraParams
22
- }) {
23
- if (!tmdb_id && !imdb_id) {
24
- throw new Error("Either tmdb_id or imdb_id must be provided.");
25
- }
26
- const hasSeason = season !== void 0;
27
- const hasEpisode = episode !== void 0;
28
- if (hasSeason && !hasEpisode || !hasSeason && hasEpisode) {
29
- throw new Error("Season and episode must be provided together or omitted together.");
30
- }
31
- const url = new URL(`${config.baseUrl}/search`);
32
- const queryParams = {
33
- id: String(tmdb_id || imdb_id),
34
- season,
35
- episode,
36
- encoding: Array.isArray(encoding) ? encoding.join(",") : encoding,
37
- language: Array.isArray(language) ? language.join(",") : language,
38
- format: Array.isArray(format) ? format.join(",") : format,
39
- source: Array.isArray(source) ? source.join(",") : source,
40
- hi
41
- };
42
- Object.entries(queryParams).forEach(([key, value]) => {
43
- if (value !== void 0) {
44
- url.searchParams.append(key, String(value));
45
- }
46
- });
47
- Object.entries(extraParams).forEach(([key, value]) => {
48
- if (value !== void 0) {
49
- if (Array.isArray(value)) {
50
- url.searchParams.append(key, value.join(","));
51
- } else {
52
- url.searchParams.append(key, String(value));
53
- }
54
- }
55
- });
56
- return url;
57
- }
58
- async function fetchSubtitles(url) {
59
- const response = await fetch(url.toString());
60
- if (!response.ok) {
61
- throw new Error(`HTTP error! status: ${response.status}`);
62
- }
63
- return response.json();
64
- }
65
- async function searchSubtitles(params) {
66
- try {
67
- const url = await constructUrl(params);
68
- return await fetchSubtitles(url);
69
- } catch (error) {
70
- throw new Error(`Error fetching subtitles: ${error}`);
71
- }
72
- }
73
- async function parseToVTT(subtitleUrl) {
74
- try {
75
- const response = await fetch(subtitleUrl);
76
- if (!response.ok) {
77
- throw new Error(`Failed to fetch subtitle content: ${response.status}`);
78
- }
79
- const content = await response.text();
80
- const normalizedContent = content.replace(/\r\n|\r/g, "\n").trim();
81
- const blocks = normalizedContent.split(/\n\n+/);
82
- const timestampRegex = /^\d{1,2}:\d{2}:\d{2}[,.]\d{3}\s*-->\s*\d{1,2}:\d{2}:\d{2}[,.]\d{3}$/;
83
- const hasValidSRTFormat = blocks.some((block) => {
84
- const lines = block.split("\n").map((line) => line.trim());
85
- return lines.some((line) => timestampRegex.test(line));
86
- });
87
- if (!hasValidSRTFormat) {
88
- throw new Error("Invalid subtitle format: not SRT");
89
- }
90
- const vttLines = ["WEBVTT", ""];
91
- for (const block of blocks) {
92
- const lines = block.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
93
- if (lines.length < 2)
94
- continue;
95
- const timestampIndex = lines.findIndex((line) => timestampRegex.test(line));
96
- if (timestampIndex === -1)
97
- continue;
98
- const textLines = lines.slice(timestampIndex + 1).filter((line) => !/^\d+$/.test(line));
99
- if (textLines.length === 0)
100
- continue;
101
- let timestampLine = lines[timestampIndex];
102
- timestampLine = timestampLine.replace(/[,.](?=\s*-->)/, "").replace(/[,.]$/, "").replace(/,(\d{3})/g, ".$1");
103
- vttLines.push(`${timestampLine}
104
- ${textLines.join("\n")}
105
- `);
106
- }
107
- return vttLines.join("\n").replace(/\n{3,}/g, "\n\n").trim() + "\n\n";
108
- } catch (error) {
109
- console.error("Error in parseToVTT:", error);
110
- throw error;
111
- }
112
- }
113
- exports.configure = configure;
114
- exports.parseToVTT = parseToVTT;
115
- exports.searchSubtitles = searchSubtitles;
116
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
117
- return exports;
118
- }({});
package/lib/main.js DELETED
@@ -1,115 +0,0 @@
1
- const config = {
2
- baseUrl: "https://sub.wyzie.ru"
3
- };
4
- function configure(options) {
5
- if (options.baseUrl) {
6
- config.baseUrl = options.baseUrl.replace(/\/$/, "");
7
- }
8
- }
9
- async function constructUrl({
10
- tmdb_id,
11
- imdb_id,
12
- season,
13
- episode,
14
- encoding,
15
- language,
16
- format,
17
- source,
18
- hi,
19
- ...extraParams
20
- }) {
21
- if (!tmdb_id && !imdb_id) {
22
- throw new Error("Either tmdb_id or imdb_id must be provided.");
23
- }
24
- const hasSeason = season !== void 0;
25
- const hasEpisode = episode !== void 0;
26
- if (hasSeason && !hasEpisode || !hasSeason && hasEpisode) {
27
- throw new Error("Season and episode must be provided together or omitted together.");
28
- }
29
- const url = new URL(`${config.baseUrl}/search`);
30
- const queryParams = {
31
- id: String(tmdb_id || imdb_id),
32
- season,
33
- episode,
34
- encoding: Array.isArray(encoding) ? encoding.join(",") : encoding,
35
- language: Array.isArray(language) ? language.join(",") : language,
36
- format: Array.isArray(format) ? format.join(",") : format,
37
- source: Array.isArray(source) ? source.join(",") : source,
38
- hi
39
- };
40
- Object.entries(queryParams).forEach(([key, value]) => {
41
- if (value !== void 0) {
42
- url.searchParams.append(key, String(value));
43
- }
44
- });
45
- Object.entries(extraParams).forEach(([key, value]) => {
46
- if (value !== void 0) {
47
- if (Array.isArray(value)) {
48
- url.searchParams.append(key, value.join(","));
49
- } else {
50
- url.searchParams.append(key, String(value));
51
- }
52
- }
53
- });
54
- return url;
55
- }
56
- async function fetchSubtitles(url) {
57
- const response = await fetch(url.toString());
58
- if (!response.ok) {
59
- throw new Error(`HTTP error! status: ${response.status}`);
60
- }
61
- return response.json();
62
- }
63
- async function searchSubtitles(params) {
64
- try {
65
- const url = await constructUrl(params);
66
- return await fetchSubtitles(url);
67
- } catch (error) {
68
- throw new Error(`Error fetching subtitles: ${error}`);
69
- }
70
- }
71
- async function parseToVTT(subtitleUrl) {
72
- try {
73
- const response = await fetch(subtitleUrl);
74
- if (!response.ok) {
75
- throw new Error(`Failed to fetch subtitle content: ${response.status}`);
76
- }
77
- const content = await response.text();
78
- const normalizedContent = content.replace(/\r\n|\r/g, "\n").trim();
79
- const blocks = normalizedContent.split(/\n\n+/);
80
- const timestampRegex = /^\d{1,2}:\d{2}:\d{2}[,.]\d{3}\s*-->\s*\d{1,2}:\d{2}:\d{2}[,.]\d{3}$/;
81
- const hasValidSRTFormat = blocks.some((block) => {
82
- const lines = block.split("\n").map((line) => line.trim());
83
- return lines.some((line) => timestampRegex.test(line));
84
- });
85
- if (!hasValidSRTFormat) {
86
- throw new Error("Invalid subtitle format: not SRT");
87
- }
88
- const vttLines = ["WEBVTT", ""];
89
- for (const block of blocks) {
90
- const lines = block.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
91
- if (lines.length < 2)
92
- continue;
93
- const timestampIndex = lines.findIndex((line) => timestampRegex.test(line));
94
- if (timestampIndex === -1)
95
- continue;
96
- const textLines = lines.slice(timestampIndex + 1).filter((line) => !/^\d+$/.test(line));
97
- if (textLines.length === 0)
98
- continue;
99
- let timestampLine = lines[timestampIndex];
100
- timestampLine = timestampLine.replace(/[,.](?=\s*-->)/, "").replace(/[,.]$/, "").replace(/,(\d{3})/g, ".$1");
101
- vttLines.push(`${timestampLine}
102
- ${textLines.join("\n")}
103
- `);
104
- }
105
- return vttLines.join("\n").replace(/\n{3,}/g, "\n\n").trim() + "\n\n";
106
- } catch (error) {
107
- console.error("Error in parseToVTT:", error);
108
- throw error;
109
- }
110
- }
111
- export {
112
- configure,
113
- parseToVTT,
114
- searchSubtitles
115
- };
package/lib/main.umd.cjs DELETED
@@ -1,119 +0,0 @@
1
- (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.main = {}));
3
- })(this, function(exports2) {
4
- "use strict";
5
- const config = {
6
- baseUrl: "https://sub.wyzie.ru"
7
- };
8
- function configure(options) {
9
- if (options.baseUrl) {
10
- config.baseUrl = options.baseUrl.replace(/\/$/, "");
11
- }
12
- }
13
- async function constructUrl({
14
- tmdb_id,
15
- imdb_id,
16
- season,
17
- episode,
18
- encoding,
19
- language,
20
- format,
21
- source,
22
- hi,
23
- ...extraParams
24
- }) {
25
- if (!tmdb_id && !imdb_id) {
26
- throw new Error("Either tmdb_id or imdb_id must be provided.");
27
- }
28
- const hasSeason = season !== void 0;
29
- const hasEpisode = episode !== void 0;
30
- if (hasSeason && !hasEpisode || !hasSeason && hasEpisode) {
31
- throw new Error("Season and episode must be provided together or omitted together.");
32
- }
33
- const url = new URL(`${config.baseUrl}/search`);
34
- const queryParams = {
35
- id: String(tmdb_id || imdb_id),
36
- season,
37
- episode,
38
- encoding: Array.isArray(encoding) ? encoding.join(",") : encoding,
39
- language: Array.isArray(language) ? language.join(",") : language,
40
- format: Array.isArray(format) ? format.join(",") : format,
41
- source: Array.isArray(source) ? source.join(",") : source,
42
- hi
43
- };
44
- Object.entries(queryParams).forEach(([key, value]) => {
45
- if (value !== void 0) {
46
- url.searchParams.append(key, String(value));
47
- }
48
- });
49
- Object.entries(extraParams).forEach(([key, value]) => {
50
- if (value !== void 0) {
51
- if (Array.isArray(value)) {
52
- url.searchParams.append(key, value.join(","));
53
- } else {
54
- url.searchParams.append(key, String(value));
55
- }
56
- }
57
- });
58
- return url;
59
- }
60
- async function fetchSubtitles(url) {
61
- const response = await fetch(url.toString());
62
- if (!response.ok) {
63
- throw new Error(`HTTP error! status: ${response.status}`);
64
- }
65
- return response.json();
66
- }
67
- async function searchSubtitles(params) {
68
- try {
69
- const url = await constructUrl(params);
70
- return await fetchSubtitles(url);
71
- } catch (error) {
72
- throw new Error(`Error fetching subtitles: ${error}`);
73
- }
74
- }
75
- async function parseToVTT(subtitleUrl) {
76
- try {
77
- const response = await fetch(subtitleUrl);
78
- if (!response.ok) {
79
- throw new Error(`Failed to fetch subtitle content: ${response.status}`);
80
- }
81
- const content = await response.text();
82
- const normalizedContent = content.replace(/\r\n|\r/g, "\n").trim();
83
- const blocks = normalizedContent.split(/\n\n+/);
84
- const timestampRegex = /^\d{1,2}:\d{2}:\d{2}[,.]\d{3}\s*-->\s*\d{1,2}:\d{2}:\d{2}[,.]\d{3}$/;
85
- const hasValidSRTFormat = blocks.some((block) => {
86
- const lines = block.split("\n").map((line) => line.trim());
87
- return lines.some((line) => timestampRegex.test(line));
88
- });
89
- if (!hasValidSRTFormat) {
90
- throw new Error("Invalid subtitle format: not SRT");
91
- }
92
- const vttLines = ["WEBVTT", ""];
93
- for (const block of blocks) {
94
- const lines = block.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
95
- if (lines.length < 2)
96
- continue;
97
- const timestampIndex = lines.findIndex((line) => timestampRegex.test(line));
98
- if (timestampIndex === -1)
99
- continue;
100
- const textLines = lines.slice(timestampIndex + 1).filter((line) => !/^\d+$/.test(line));
101
- if (textLines.length === 0)
102
- continue;
103
- let timestampLine = lines[timestampIndex];
104
- timestampLine = timestampLine.replace(/[,.](?=\s*-->)/, "").replace(/[,.]$/, "").replace(/,(\d{3})/g, ".$1");
105
- vttLines.push(`${timestampLine}
106
- ${textLines.join("\n")}
107
- `);
108
- }
109
- return vttLines.join("\n").replace(/\n{3,}/g, "\n\n").trim() + "\n\n";
110
- } catch (error) {
111
- console.error("Error in parseToVTT:", error);
112
- throw error;
113
- }
114
- }
115
- exports2.configure = configure;
116
- exports2.parseToVTT = parseToVTT;
117
- exports2.searchSubtitles = searchSubtitles;
118
- Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
119
- });