frostpv 1.0.9 → 1.0.10

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.
Files changed (2) hide show
  1. package/index.js +66 -24
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1151,41 +1151,83 @@ async function downloadSmartVideo(url, config) {
1151
1151
  try {
1152
1152
  const data = await youtube(url);
1153
1153
  // Try to find the video URL in the response
1154
- // Common patterns: data.url, data.mp4, data.link, or direct string
1155
1154
 
1156
1155
  if (!data) throw new Error("No data returned from youtube downloader");
1157
1156
 
1158
- // Recursive function to find a valid video URL in the object
1159
- const findVideoUrl = (obj) => {
1160
- if (!obj) return null;
1161
- if (typeof obj === 'string' && obj.startsWith('http')) return obj;
1157
+ // Helper to check if a value looks like a video URL
1158
+ const isVideoUrl = (val) => typeof val === 'string' && val.startsWith('http') && !val.includes('.mp3');
1159
+
1160
+ let bestUrl = null;
1161
+ let bestScore = -1;
1162
+
1163
+ // Function to score keys based on quality
1164
+ const getScore = (key) => {
1165
+ const k = key.toLowerCase();
1166
+ if (k.includes('1080')) return 100;
1167
+ if (k.includes('720')) return 80;
1168
+ if (k.includes('480')) return 60;
1169
+ if (k.includes('hd')) return 90;
1170
+ if (k.includes('sd')) return 50;
1171
+ if (k.includes('360')) return 40;
1172
+ if (k.includes('mp4')) return 30;
1173
+ if (k.includes('video')) return 20;
1174
+ return 10;
1175
+ };
1162
1176
 
1163
- if (Array.isArray(obj)) {
1164
- for (const item of obj) {
1165
- const found = findVideoUrl(item);
1166
- if (found) return found;
1177
+ // Recursive function to gather all candidates
1178
+ const gatherCandidates = (obj) => {
1179
+ if (!obj) return;
1180
+
1181
+ if (typeof obj === 'object') {
1182
+ for (const key in obj) {
1183
+ const val = obj[key];
1184
+ if (isVideoUrl(val)) {
1185
+ const score = getScore(key);
1186
+ if (score > bestScore) {
1187
+ bestScore = score;
1188
+ bestUrl = val;
1189
+ }
1190
+ } else if (typeof val === 'object') {
1191
+ // Check if this object represents a format (e.g. { quality: '720p', url: '...' })
1192
+ if (val.url && isVideoUrl(val.url)) {
1193
+ let score = getScore(key); // Score from key name
1194
+ if (val.quality || val.resolution) {
1195
+ score = Math.max(score, getScore(String(val.quality || val.resolution)));
1196
+ }
1197
+ if (score > bestScore) {
1198
+ bestScore = score;
1199
+ bestUrl = val.url;
1200
+ }
1201
+ }
1202
+ // Recurse
1203
+ gatherCandidates(val);
1204
+ }
1167
1205
  }
1168
- } else if (typeof obj === 'object') {
1169
- // Prioritize certain keys
1170
- const priorities = ['url', 'link', 'download', 'mp4', 'video', 'src'];
1206
+ }
1207
+ };
1208
+
1209
+ gatherCandidates(data);
1210
+
1211
+ // If no scored candidate found, try direct simple extraction with priority keys
1212
+ if (!bestUrl) {
1213
+ const priorities = ['mp4', 'url', 'link', 'download', 'video'];
1214
+ const findSimple = (obj) => {
1215
+ if (!obj) return null;
1171
1216
  for (const key of priorities) {
1172
- if (obj[key]) {
1173
- const found = findVideoUrl(obj[key]);
1174
- if (found) return found;
1175
- }
1217
+ if (obj[key] && isVideoUrl(obj[key])) return obj[key];
1176
1218
  }
1177
- // If not found in priorities, check all keys
1178
- for (const key of Object.keys(obj)) {
1179
- if (!priorities.includes(key)) {
1180
- const found = findVideoUrl(obj[key]);
1219
+ for (const key in obj) {
1220
+ if (typeof obj[key] === 'object') {
1221
+ const found = findSimple(obj[key]);
1181
1222
  if (found) return found;
1182
1223
  }
1183
1224
  }
1184
- }
1185
- return null;
1186
- };
1225
+ return null;
1226
+ };
1227
+ bestUrl = findSimple(data);
1228
+ }
1187
1229
 
1188
- videoUrl = findVideoUrl(data);
1230
+ videoUrl = bestUrl;
1189
1231
 
1190
1232
  if (!videoUrl) {
1191
1233
  console.log('YouTube data dump:', JSON.stringify(data, null, 2)); // Debug log since we can't test
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frostpv",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "downloads",
5
5
  "main": "index.js",
6
6
  "scripts": {