wyzie-lib 2.1.5 → 2.1.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/README.md CHANGED
@@ -1,4 +1,9 @@
1
- # Wyzie Lib
1
+ <p align="center">
2
+ <a href="https://sub.wyzie.ru/">
3
+ <img src="https://i.postimg.cc/L5ppKYC5/cclogo.png" height="120">
4
+ <h1 align="center">Wyzie Lib</h1>
5
+ </a>
6
+ </p>
2
7
 
3
8
  Wyzie Lib is a package made for easily implementing [Wyzie Subs](https://sub.wyzie.ru) into your
4
9
  project without all the fuss. [Read our source code!](https://github.com/itzcozi/wyzie-lib)
@@ -7,8 +12,8 @@ project without all the fuss. [Read our source code!](https://github.com/itzcozi
7
12
 
8
13
  ## Features
9
14
 
10
- - **Simple**: Just one function for searching subtitles using Wyzie Subs API.
11
15
  - **Fast**: This package was written in Vite with TypeScript, so it's fast and reliable.
16
+ - **Simple**: Just one function for searching subtitles using Wyzie Subs API.
12
17
  - **VTT Parser**: Convert SRT subtitles to VTT format quickly.
13
18
  - **Open-Source**: The API and package are open-source.
14
19
 
@@ -38,17 +43,16 @@ yarn add wyzie-lib
38
43
  import { type SubtitleData, searchSubtitles, parseToVTT } from "wyzie-lib";
39
44
 
40
45
  // Fetches all subtitles for the media with the TMDB ID 286217
41
- // -----------------------------------------------------------
42
46
  const data: SubtitleData[] = await searchSubtitles({ tmdb_id: 286217, format: "srt" });
43
47
  console.log(data[0].id); // Prints the ID of the first subtitle object
44
48
 
45
49
  // Converts the first subtitle from SRT to VTT format
46
- // --------------------------------------------------
47
50
  const vttContent = await parseToVTT(data[0].url); // Passes the first subtitle URL
48
51
  console.log(vttContent); // Prints the raw VTT content
49
52
  ```
50
53
 
51
- Check out [demo.html](https://raw.githubusercontent.com/itzCozi/wyzie-lib/refs/heads/main/demo.html) for a working example using the VTT parser.
54
+ Check out [demo.html](https://raw.githubusercontent.com/itzCozi/wyzie-lib/refs/heads/main/demo.html)
55
+ for a working example using the VTT parser.
52
56
 
53
57
  ### Parameters
54
58
 
@@ -68,6 +72,10 @@ Check out [demo.html](https://raw.githubusercontent.com/itzCozi/wyzie-lib/refs/h
68
72
  - **QueryParams**: All parameters (optional and required) available for the wyzie-subs API.
69
73
  - **SubtitleData**: All returned values from the API with their respective types.
70
74
 
75
+ ![SearchSubtitlesParams](https://i.postimg.cc/yYZVMzH0/searchsubparam.png)
76
+ ![QueryParams](https://i.postimg.cc/v8SrP5Yc/Screenshot-2025-01-13-8-40-11-AM.png)
77
+ ![SubtitleData](https://i.postimg.cc/6QdCb8xP/subdata.png)
78
+
71
79
  <hr />
72
80
 
73
81
  <sup>
@@ -0,0 +1,88 @@
1
+ define(["exports"], function(exports) {
2
+ "use strict";
3
+ async function constructUrl({
4
+ tmdb_id,
5
+ imdb_id,
6
+ season,
7
+ episode,
8
+ encoding,
9
+ language,
10
+ format,
11
+ hi
12
+ }) {
13
+ const url = new URL("https://sub.wyzie.ru/search");
14
+ const queryParams = {
15
+ id: String(tmdb_id || imdb_id),
16
+ season,
17
+ episode,
18
+ encoding,
19
+ language,
20
+ format,
21
+ hi
22
+ };
23
+ Object.entries(queryParams).forEach(([key, value]) => {
24
+ if (value !== void 0) {
25
+ url.searchParams.append(key, String(value));
26
+ }
27
+ });
28
+ return url;
29
+ }
30
+ async function fetchSubtitles(url) {
31
+ const response = await fetch(url.toString());
32
+ if (!response.ok) {
33
+ throw new Error(`HTTP error! status: ${response.status}`);
34
+ }
35
+ return response.json();
36
+ }
37
+ async function searchSubtitles(params) {
38
+ try {
39
+ const url = await constructUrl(params);
40
+ return await fetchSubtitles(url);
41
+ } catch (error) {
42
+ throw new Error(`Error fetching subtitles: ${error}`);
43
+ }
44
+ }
45
+ async function parseToVTT(subtitleUrl) {
46
+ try {
47
+ const response = await fetch(subtitleUrl);
48
+ if (!response.ok) {
49
+ throw new Error(`Failed to fetch subtitle content: ${response.status}`);
50
+ }
51
+ const content = await response.text();
52
+ const normalizedContent = content.replace(/\r\n|\r/g, "\n").trim();
53
+ const blocks = normalizedContent.split(/\n\n+/);
54
+ const timestampRegex = /^\d{1,2}:\d{2}:\d{2}[,.]\d{3}\s*-->\s*\d{1,2}:\d{2}:\d{2}[,.]\d{3}$/;
55
+ const hasValidSRTFormat = blocks.some((block) => {
56
+ const lines = block.split("\n").map((line) => line.trim());
57
+ return lines.some((line) => timestampRegex.test(line));
58
+ });
59
+ if (!hasValidSRTFormat) {
60
+ throw new Error("Invalid subtitle format: not SRT");
61
+ }
62
+ const vttLines = ["WEBVTT", ""];
63
+ for (const block of blocks) {
64
+ const lines = block.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
65
+ if (lines.length < 2)
66
+ continue;
67
+ const timestampIndex = lines.findIndex((line) => timestampRegex.test(line));
68
+ if (timestampIndex === -1)
69
+ continue;
70
+ const textLines = lines.slice(timestampIndex + 1).filter((line) => !/^\d+$/.test(line));
71
+ if (textLines.length === 0)
72
+ continue;
73
+ let timestampLine = lines[timestampIndex];
74
+ timestampLine = timestampLine.replace(/[,.](?=\s*-->)/, "").replace(/[,.]$/, "").replace(/,(\d{3})/g, ".$1");
75
+ vttLines.push(`${timestampLine}
76
+ ${textLines.join("\n")}
77
+ `);
78
+ }
79
+ return vttLines.join("\n").replace(/\n{3,}/g, "\n\n").trim() + "\n\n";
80
+ } catch (error) {
81
+ console.error("Error in parseToVTT:", error);
82
+ throw error;
83
+ }
84
+ }
85
+ exports.parseToVTT = parseToVTT;
86
+ exports.searchSubtitles = searchSubtitles;
87
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
88
+ });
package/lib/main.cjs ADDED
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ async function constructUrl({
4
+ tmdb_id,
5
+ imdb_id,
6
+ season,
7
+ episode,
8
+ encoding,
9
+ language,
10
+ format,
11
+ hi
12
+ }) {
13
+ const url = new URL("https://sub.wyzie.ru/search");
14
+ const queryParams = {
15
+ id: String(tmdb_id || imdb_id),
16
+ season,
17
+ episode,
18
+ encoding,
19
+ language,
20
+ format,
21
+ hi
22
+ };
23
+ Object.entries(queryParams).forEach(([key, value]) => {
24
+ if (value !== void 0) {
25
+ url.searchParams.append(key, String(value));
26
+ }
27
+ });
28
+ return url;
29
+ }
30
+ async function fetchSubtitles(url) {
31
+ const response = await fetch(url.toString());
32
+ if (!response.ok) {
33
+ throw new Error(`HTTP error! status: ${response.status}`);
34
+ }
35
+ return response.json();
36
+ }
37
+ async function searchSubtitles(params) {
38
+ try {
39
+ const url = await constructUrl(params);
40
+ return await fetchSubtitles(url);
41
+ } catch (error) {
42
+ throw new Error(`Error fetching subtitles: ${error}`);
43
+ }
44
+ }
45
+ async function parseToVTT(subtitleUrl) {
46
+ try {
47
+ const response = await fetch(subtitleUrl);
48
+ if (!response.ok) {
49
+ throw new Error(`Failed to fetch subtitle content: ${response.status}`);
50
+ }
51
+ const content = await response.text();
52
+ const normalizedContent = content.replace(/\r\n|\r/g, "\n").trim();
53
+ const blocks = normalizedContent.split(/\n\n+/);
54
+ const timestampRegex = /^\d{1,2}:\d{2}:\d{2}[,.]\d{3}\s*-->\s*\d{1,2}:\d{2}:\d{2}[,.]\d{3}$/;
55
+ const hasValidSRTFormat = blocks.some((block) => {
56
+ const lines = block.split("\n").map((line) => line.trim());
57
+ return lines.some((line) => timestampRegex.test(line));
58
+ });
59
+ if (!hasValidSRTFormat) {
60
+ throw new Error("Invalid subtitle format: not SRT");
61
+ }
62
+ const vttLines = ["WEBVTT", ""];
63
+ for (const block of blocks) {
64
+ const lines = block.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
65
+ if (lines.length < 2)
66
+ continue;
67
+ const timestampIndex = lines.findIndex((line) => timestampRegex.test(line));
68
+ if (timestampIndex === -1)
69
+ continue;
70
+ const textLines = lines.slice(timestampIndex + 1).filter((line) => !/^\d+$/.test(line));
71
+ if (textLines.length === 0)
72
+ continue;
73
+ let timestampLine = lines[timestampIndex];
74
+ timestampLine = timestampLine.replace(/[,.](?=\s*-->)/, "").replace(/[,.]$/, "").replace(/,(\d{3})/g, ".$1");
75
+ vttLines.push(`${timestampLine}
76
+ ${textLines.join("\n")}
77
+ `);
78
+ }
79
+ return vttLines.join("\n").replace(/\n{3,}/g, "\n\n").trim() + "\n\n";
80
+ } catch (error) {
81
+ console.error("Error in parseToVTT:", error);
82
+ throw error;
83
+ }
84
+ }
85
+ exports.parseToVTT = parseToVTT;
86
+ exports.searchSubtitles = searchSubtitles;
package/lib/main.d.ts CHANGED
@@ -1,42 +1,97 @@
1
+ /**
2
+ * Parses subtitle content from a URL to VTT format.
3
+ *
4
+ * @param {string} subtitleUrl - The URL of the subtitle to parse.
5
+ * @returns {Promise<string>} A promise that resolves to the subtitle content in VTT format.
6
+ * @throws {Error} Throws an error if fetching or parsing the subtitle content fails.
7
+ */
1
8
  export declare function parseToVTT(subtitleUrl: string): Promise<string>;
2
9
 
10
+ /**
11
+ * Parameters used to construct the URL for subtitle search (requires an ID).
12
+ */
3
13
  export declare type QueryParams = {
14
+ /** Unique identifier (either TMDB or IMDB ID). */
4
15
  id: string;
16
+ /** Season number if the content is a series. */
5
17
  season?: number;
18
+ /** Episode number if the content is a series. */
6
19
  episode?: number;
20
+ /** Encoding of the subtitle files. */
21
+ encoding?: string;
22
+ /** ISO 3166 code of the subtitle desired. */
7
23
  language?: string;
24
+ /** Which subtitle file format you want */
8
25
  format?: string;
26
+ /** Determines if you get a hearing impaired subtitles */
9
27
  hi?: boolean;
10
28
  };
11
29
 
30
+ /**
31
+ * Searches for subtitles based on the provided parameters.
32
+ *
33
+ * @param {SearchSubtitlesParams} params - The parameters for searching: SearchSubtitlesParams.
34
+ * @returns {Promise<SubtitleData[]>} A promise that resolves to an array of subtitle data.
35
+ * @throws {Error} Throws an error if fetching subtitles fails or something goes wrong.
36
+ */
12
37
  export declare function searchSubtitles(params: SearchSubtitlesParams): Promise<SubtitleData[]>;
13
38
 
14
- export declare type SearchSubtitlesParams = ({
39
+ /**
40
+ * Parameters for searching subtitles.
41
+ * Either IMDB or TMDB ID is required and if episode is provided, season is also required.
42
+ */
43
+ export declare type SearchSubtitlesParams = (
44
+ /** The TMDB ID of the media you want subtitles for (either TMDB or IMDB ID). */
45
+ {
15
46
  tmdb_id: number;
16
47
  imdb_id?: never;
17
- } | {
48
+ }
49
+ /** The IMDB ID of the media you want subtitles for (either TMDB or IMDB ID). */
50
+ | {
18
51
  imdb_id: string;
19
52
  tmdb_id?: never;
20
53
  }) & {
54
+ /** ISO 3166 code of the subtitle desired. */
21
55
  language?: string;
56
+ /** The subtitle file's encoding (UTF-8, ASCII) */
57
+ encoding?: string;
58
+ /** Which subtitle file format you want. */
22
59
  format?: string;
60
+ /** Determines if you get hearing impaired subtitles. */
23
61
  hi?: boolean;
24
- } & ({
62
+ } & (
63
+ /** The number of the desired season you want subtitles for. */
64
+ {
25
65
  season: number;
26
66
  episode: number;
27
- } | {
67
+ }
68
+ /** The number of the desired episode you want subtitles for. */
69
+ | {
28
70
  season?: never;
29
71
  episode?: never;
30
72
  });
31
73
 
74
+ /**
75
+ * Data structure representing a single subtitle object.
76
+ */
32
77
  export declare type SubtitleData = {
78
+ /** Unique identifier (either TMDB or IMDB ID). */
33
79
  id: string;
80
+ /** The subtitle file's URL. */
34
81
  url: string;
82
+ /** The format of the subtitle file. */
35
83
  format: string;
84
+ /** The encoding of the subtitle files. (UTF-8, ASCII, ETC) */
85
+ encoding: string;
86
+ /** Boolean indicating if the subtitle's is hearing impaired. */
36
87
  isHearingImpaired: boolean;
88
+ /** URL to a PNG of the flag of the subtitle's language. */
37
89
  flagUrl: string;
90
+ /** The name/title of the media. */
38
91
  media: string;
92
+ /** The display language; Example: English. */
39
93
  display: string;
94
+ /** ISO 3166 code; Example: en (2 alphabetic letters). */
40
95
  language: string;
41
96
  };
42
97
 
@@ -0,0 +1,89 @@
1
+ var main = function(exports) {
2
+ "use strict";
3
+ async function constructUrl({
4
+ tmdb_id,
5
+ imdb_id,
6
+ season,
7
+ episode,
8
+ encoding,
9
+ language,
10
+ format,
11
+ hi
12
+ }) {
13
+ const url = new URL("https://sub.wyzie.ru/search");
14
+ const queryParams = {
15
+ id: String(tmdb_id || imdb_id),
16
+ season,
17
+ episode,
18
+ encoding,
19
+ language,
20
+ format,
21
+ hi
22
+ };
23
+ Object.entries(queryParams).forEach(([key, value]) => {
24
+ if (value !== void 0) {
25
+ url.searchParams.append(key, String(value));
26
+ }
27
+ });
28
+ return url;
29
+ }
30
+ async function fetchSubtitles(url) {
31
+ const response = await fetch(url.toString());
32
+ if (!response.ok) {
33
+ throw new Error(`HTTP error! status: ${response.status}`);
34
+ }
35
+ return response.json();
36
+ }
37
+ async function searchSubtitles(params) {
38
+ try {
39
+ const url = await constructUrl(params);
40
+ return await fetchSubtitles(url);
41
+ } catch (error) {
42
+ throw new Error(`Error fetching subtitles: ${error}`);
43
+ }
44
+ }
45
+ async function parseToVTT(subtitleUrl) {
46
+ try {
47
+ const response = await fetch(subtitleUrl);
48
+ if (!response.ok) {
49
+ throw new Error(`Failed to fetch subtitle content: ${response.status}`);
50
+ }
51
+ const content = await response.text();
52
+ const normalizedContent = content.replace(/\r\n|\r/g, "\n").trim();
53
+ const blocks = normalizedContent.split(/\n\n+/);
54
+ const timestampRegex = /^\d{1,2}:\d{2}:\d{2}[,.]\d{3}\s*-->\s*\d{1,2}:\d{2}:\d{2}[,.]\d{3}$/;
55
+ const hasValidSRTFormat = blocks.some((block) => {
56
+ const lines = block.split("\n").map((line) => line.trim());
57
+ return lines.some((line) => timestampRegex.test(line));
58
+ });
59
+ if (!hasValidSRTFormat) {
60
+ throw new Error("Invalid subtitle format: not SRT");
61
+ }
62
+ const vttLines = ["WEBVTT", ""];
63
+ for (const block of blocks) {
64
+ const lines = block.split("\n").map((line) => line.trim()).filter((line) => line.length > 0);
65
+ if (lines.length < 2)
66
+ continue;
67
+ const timestampIndex = lines.findIndex((line) => timestampRegex.test(line));
68
+ if (timestampIndex === -1)
69
+ continue;
70
+ const textLines = lines.slice(timestampIndex + 1).filter((line) => !/^\d+$/.test(line));
71
+ if (textLines.length === 0)
72
+ continue;
73
+ let timestampLine = lines[timestampIndex];
74
+ timestampLine = timestampLine.replace(/[,.](?=\s*-->)/, "").replace(/[,.]$/, "").replace(/,(\d{3})/g, ".$1");
75
+ vttLines.push(`${timestampLine}
76
+ ${textLines.join("\n")}
77
+ `);
78
+ }
79
+ return vttLines.join("\n").replace(/\n{3,}/g, "\n\n").trim() + "\n\n";
80
+ } catch (error) {
81
+ console.error("Error in parseToVTT:", error);
82
+ throw error;
83
+ }
84
+ }
85
+ exports.parseToVTT = parseToVTT;
86
+ exports.searchSubtitles = searchSubtitles;
87
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
88
+ return exports;
89
+ }({});
package/lib/main.js CHANGED
@@ -3,6 +3,7 @@ async function constructUrl({
3
3
  imdb_id,
4
4
  season,
5
5
  episode,
6
+ encoding,
6
7
  language,
7
8
  format,
8
9
  hi
@@ -12,6 +13,7 @@ async function constructUrl({
12
13
  id: String(tmdb_id || imdb_id),
13
14
  season,
14
15
  episode,
16
+ encoding,
15
17
  language,
16
18
  format,
17
19
  hi
package/lib/main.umd.cjs CHANGED
@@ -7,6 +7,7 @@
7
7
  imdb_id,
8
8
  season,
9
9
  episode,
10
+ encoding,
10
11
  language,
11
12
  format,
12
13
  hi
@@ -16,6 +17,7 @@
16
17
  id: String(tmdb_id || imdb_id),
17
18
  season,
18
19
  episode,
20
+ encoding,
19
21
  language,
20
22
  format,
21
23
  hi
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wyzie-lib",
3
- "version": "2.1.5",
3
+ "version": "2.1.7",
4
4
  "icon": "https://i.postimg.cc/L5ppKYC5/cclogo.png",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -31,6 +31,26 @@
31
31
  "require": {
32
32
  "types": "./lib/main.d.ts",
33
33
  "default": "./lib/main.umd.cjs"
34
+ },
35
+ "umd": {
36
+ "types": "./lib/main.d.ts",
37
+ "default": "./lib/main.umd.js"
38
+ },
39
+ "es": {
40
+ "types": "./lib/main.d.ts",
41
+ "default": "./lib/main.es.js"
42
+ },
43
+ "cjs": {
44
+ "types": "./lib/main.d.ts",
45
+ "default": "./lib/main.cjs.js"
46
+ },
47
+ "iife": {
48
+ "types": "./lib/main.d.ts",
49
+ "default": "./lib/main.iife.js"
50
+ },
51
+ "amd": {
52
+ "types": "./lib/main.d.ts",
53
+ "default": "./lib/main.amd.js"
34
54
  }
35
55
  }
36
56
  },
@@ -38,18 +58,17 @@
38
58
  "type": "git",
39
59
  "url": "git+https://github.com/itzcozi/wyzie-lib.git"
40
60
  },
41
- "devDependencies": {
42
- "prettier": "^3.4.2",
43
- "typescript": "^5.7.3",
44
- "vite": "^4.5.5",
45
- "vite-plugin-dts": "^4.5.0",
46
- "vitest": "^2.1.8"
47
- },
48
61
  "scripts": {
49
- "dev": "vite",
50
62
  "build": "vite build && tsc",
51
63
  "test": "npx vitest",
52
64
  "format": "prettier --log-level warn --write \"{src/**/*.{ts},*.{ts,js,html,css,json,md,xml}}\"",
53
65
  "preview": "vite preview"
66
+ },
67
+ "devDependencies": {
68
+ "prettier": "^3.5.1",
69
+ "typescript": "^5.7.3",
70
+ "vite": "^4.5.9",
71
+ "vite-plugin-dts": "^4.5.0",
72
+ "vitest": "^2.1.9"
54
73
  }
55
- }
74
+ }