forked from ardoviniandrea/ViniPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxtreamClient.js
More file actions
132 lines (113 loc) · 4.78 KB
/
Copy pathxtreamClient.js
File metadata and controls
132 lines (113 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// xtreamClient.js
const axios = require('axios');
class XtreamClient {
constructor(baseUrl, username, password, userAgent = 'Xtream-JS-Client') {
if (!baseUrl || typeof baseUrl !== 'string') {
throw new Error('[XC Client Constructor] Invalid or missing baseUrl provided.');
}
// Normalize URL: remove any paths and trailing slashes
try {
const url = new URL(baseUrl);
this.baseUrl = `${url.protocol}//${url.host}`;
} catch (e) {
// Provide more context in the error
console.error(`[XC Client Constructor] Failed to parse baseUrl "${baseUrl}": ${e.message}`);
throw new Error(`[XC Client Constructor] Invalid baseUrl format: "${baseUrl}". Please provide a valid URL (e.g., http://example.com:8080).`);
}
this.username = username;
this.password = password;
this.client = axios.create({
timeout: 60000, // 60 second timeout
headers: { 'User-Agent': userAgent }
});
console.log(`[XC Client Constructor] Client initialized for base URL: ${this.baseUrl} with User-Agent: ${userAgent}`); // Added log
}
/**
* Makes a request to the provider's API.
* @param {string} action The API action (e.g., 'get_vod_streams')
* @param {object} params Additional URL parameters
* @returns {Promise<object|Array>} The JSON response from the API
*/
async _makeRequest(action, params = {}) {
try {
const url = `${this.baseUrl}/player_api.php`;
const allParams = {
username: this.username,
password: this.password,
action: action,
...params
};
console.log(`[XC Client] Requesting action: ${action}`);
const response = await this.client.get(url, { params: allParams });
if (!response.data) {
throw new Error('Empty response from provider');
}
return response.data;
} catch (error) {
const msg = `[XC Client] Error in action '${action}': ${error.message}`;
console.error(msg);
throw new Error(msg);
}
}
/** Fetches all VOD streams (movies). */
async getVodStreams() {
return this._makeRequest('get_vod_streams');
}
/** Fetches all series. */
async getSeries() {
return this._makeRequest('get_series');
}
/** Fetches detailed info for one movie. */
async getVodInfo(vodId) {
return this._makeRequest('get_vod_info', { vod_id: vodId });
}
/** Fetches detailed info for one series, including episodes. */
async getSeriesInfo(seriesId) {
return this._makeRequest('get_series_info', { series_id: seriesId });
}
/** Fetches all VOD categories. */
async getVodCategories() {
return this._makeRequest('get_vod_categories');
}
/** Fetches all Series categories. */
async getSeriesCategories() {
return this._makeRequest('get_series_categories');
}
/** Fetches all Live TV categories. */
async getLiveCategories() {
return this._makeRequest('get_live_categories');
}
/**
* Fetches all category types (Live, VOD, Series) concurrently and returns a merged, unique list.
* @returns {Promise<string[]>} A sorted array of unique category names.
*/
async getAllCategories() {
try {
console.log('[XC Client] Fetching all category types concurrently...');
const [live, vod, series] = await Promise.all([
this.getLiveCategories(),
this.getVodCategories(),
this.getSeriesCategories()
]);
const allCategories = new Set();
// Add categories from all responses, checking if they are arrays
if (Array.isArray(live)) {
live.forEach(c => allCategories.add(c.category_name));
}
if (Array.isArray(vod)) {
vod.forEach(c => allCategories.add(c.category_name));
}
if (Array.isArray(series)) {
series.forEach(c => allCategories.add(c.category_name));
}
const sortedCategories = Array.from(allCategories).sort((a, b) => a.localeCompare(b));
console.log(`[XC Client] Found ${sortedCategories.length} unique categories across all types.`);
return sortedCategories;
} catch (error) {
console.error(`[XC Client] Failed to fetch all categories: ${error.message}`);
// Depending on desired behavior, you might re-throw or return an empty array
throw new Error(`Failed to fetch all categories: ${error.message}`);
}
}
}
module.exports = XtreamClient;