File size: 4,883 Bytes
ca7de5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6799798
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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;
}