helpyourselfio / src /util.js
oimerts's picture
Upload 9 files
6799798 verified
Raw History Blame Contribute Delete
4.88 kB
import { VIDEO_EXTENSIONS } from './const.js';
export function isVideo(filename) {
return VIDEO_EXTENSIONS.some(ext =>
filename.toLowerCase().endsWith(ext)
);
}
export function extractInfoHash(magnetLink) {
const match = magnetLink.match(/xt=urn:btih:([^&]+)/i);
return match ? match[1].toLowerCase() : null;
}
export async function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export function base64Encode(str) {
return Buffer.from(str).toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}
export function base64Decode(str) {
str = str.replace(/-/g, '+').replace(/_/g, '/');
while (str.length % 4) str += '=';
return Buffer.from(str, 'base64').toString('ascii');
}
export function parseQuality(str) {
// Extract quality value (e.g., '1080p', '4K', '720p')
const qualityMatch = str.match(/(\d{3,4}p|4k|uhd)/i);
if (!qualityMatch) return 0;
const quality = qualityMatch[0].toLowerCase();
// Convert quality to numeric value for sorting
switch (quality) {
case '4k':
case 'uhd':
return 2160;
case '1080p':
return 1080;
case '720p':
return 720;
case '480p':
return 480;
default:
return parseInt(quality) || 0;
}
}
export function parseSize(str) {
// Extract size value and unit (e.g., '2.5 GB', '800 MB')
const sizeMatch = str.match(/(\d+(?:\.\d+)?)\s*(GB|MB)/i);
if (!sizeMatch) return 0;
const [, size, unit] = sizeMatch;
const numericSize = parseFloat(size);
// Convert all sizes to MB for consistent comparison
if (unit.toLowerCase() === 'gb') {
return numericSize * 1024;
}
return numericSize;
}
export function detectVideoFeatures(filename) {
const features = [];
// Video Features
if (/(DV|DOLBY.?VISION)/i.test(filename)) {
features.push('DV');
}
if (/HDR/i.test(filename)) {
features.push('HDR');
}
if (/HDR10\+/i.test(filename)) {
features.push('HDR10+');
}
if (/HEVC|[hx]265/i.test(filename)) {
features.push('HEVC');
}
if (/10.?bit/i.test(filename)) {
features.push('10bit');
}
// Audio Features
if (/DDP|DD\+|E-?AC-?3|DOLBY.?DIGITAL.?\+/i.test(filename)) {
features.push('DD+');
}
if (/ATMOS|DOLBY.?ATMOS/i.test(filename)) {
features.push('ATMOS');
}
if (/DTS-?HD.?MA|DTS.?MA/i.test(filename)) {
features.push('DTS-HD MA');
}
if (/DTS-?HD/i.test(filename) && !features.includes('DTS-HD MA')) {
features.push('DTS-HD');
}
if (/DTS-?X/i.test(filename)) {
features.push('DTS:X');
}
if (/TrueHD/i.test(filename)) {
features.push('TrueHD');
}
// Source Features
if (/\bBD.?RIP\b|\bBLURAY\b/i.test(filename)) {
features.push('BluRay');
} else if (/\bWEB.?(?:DL|RIP)\b/i.test(filename)) {
features.push('WEB-DL');
} else if (/\bHDTV\b/i.test(filename)) {
features.push('HDTV');
}
return features;
}
// Extract release year from string
export function extractYear(str) {
const match = str.match(/\b(19|20)\d{2}\b/);
return match ? match[0] : null;
}
// Extract release group from filename
export function extractReleaseGroup(filename) {
// Match content between last '-' and file extension, or within brackets at the end
const match = filename.match(/-([^-.]+)(?:\.[^.]+)?$|\[([^\]]+)\](?:\.[^.]+)?$/);
return match ? (match[1] || match[2]) : null;
}
// Format file size in human readable format
export function formatFileSize(bytes) {
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
let size = bytes;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(2)} ${units[unitIndex]}`;
}
// Clean up title/filename
export function cleanTitle(title) {
return title
.replace(/\.[^/.]+$/, '') // Remove file extension
.replace(/\./g, ' ') // Replace dots with spaces
.replace(/[_]/g, ' ') // Replace underscores with spaces
.replace(/\[[^\]]*\]/g, '') // Remove content within []
.replace(/-([^-]+)$/, '') // Remove release group after last -
.replace(/\s+/g, ' ') // Remove multiple spaces
.trim(); // Trim whitespace
}
// Compare versions (e.g., "1.0.0" vs "1.0.1")
export function compareVersions(v1, v2) {
const normalize = v => v.split('.').map(Number);
const [a1, a2, a3] = normalize(v1);
const [b1, b2, b3] = normalize(v2);
if (a1 !== b1) return a1 - b1;
if (a2 !== b2) return a2 - b2;
return a3 - b3;
}