File size: 6,601 Bytes
ea5d04f
 
 
 
 
 
 
 
 
396151e
ea5d04f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import xml2js from 'xml2js';
import readTorrent from 'read-torrent';
import { promisify } from 'util';

const readTorrentPromise = promisify(readTorrent);

const RSS_FEEDS = [
    {
        name: 'TT',
        url: 'https://torrenting.com/t.rss?5;18;4;82;49;99;47;38;11;55;3;40;1;download;u=1750838;tp=axsrnyklbmpybwmsysakkectdkwjkrhb;mikmc;private;do-not-share',
        domain: 'torrenting.com'
    }
];

const parser = new xml2js.Parser({ 
    explicitArray: false,
    ignoreAttrs: true 
});

function parseTorrentInfo(item) {
    const desc = item.description || '';
    const match = desc.match(/Category:\s*([^]+?)Size:\s*([^]+?)$/);
    
    if (!match) {
        return { size: 'Unknown', category: 'Unknown', sizeInMB: 0 };
    }

    const category = match[1].trim();
    const size = match[2].trim();
    
    const sizeMatch = size.match(/([\d.]+)\s*(GB|MB|TB)/i);
    let sizeInMB = 0;
    
    if (sizeMatch) {
        const [, value, unit] = sizeMatch;
        sizeInMB = parseFloat(value);
        switch (unit.toUpperCase()) {
            case 'TB':
                sizeInMB *= 1024 * 1024;
                break;
            case 'GB':
                sizeInMB *= 1024;
                break;
        }
    }

    return { size, category, sizeInMB };
}

async function downloadAndParseTorrent(url) {
    try {
        console.log('Downloading torrent from:', url);
        
        const options = {
            headers: {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
                'Accept': '*/*',
                'Referer': 'https://torrenting.com/',
                'Origin': 'https://torrenting.com'
            }
        };

        const torrentInfo = await readTorrentPromise(url, options);
        console.log('Parsed torrent info:', {
            name: torrentInfo.name,
            length: torrentInfo.length,
            files: torrentInfo.files?.length || 0
        });

        if (!torrentInfo?.infoHash) {
            console.error('No info hash found');
            return null;
        }

        const videoFiles = torrentInfo.files?.filter(file => {
            const filePath = Array.isArray(file.path) ? file.path.join('/') : file.path;
            return /\.(mp4|mkv|avi|mov|wmv)$/i.test(filePath);
        }) || [];

        if (videoFiles.length === 0) {
            console.log('No video files found');
            return null;
        }

        videoFiles.sort((a, b) => b.length - a.length);
        const mainFile = videoFiles[0];
        const mainFilePath = Array.isArray(mainFile.path) ? mainFile.path.join('/') : mainFile.path;

        const magnetUri = `magnet:?xt=urn:btih:${torrentInfo.infoHash}` +
                         `&dn=${encodeURIComponent(torrentInfo.name)}` +
                         (torrentInfo.announce ? torrentInfo.announce.map(tr => `&tr=${encodeURIComponent(tr)}`).join('') : '');

        return {
            magnetLink: magnetUri,
            files: videoFiles,
            infoHash: torrentInfo.infoHash,
            mainFile: {
                path: mainFilePath,
                length: mainFile.length
            }
        };

    } catch (error) {
        console.error('Error:', error);
        return null;
    }
}

async function fetchRSSFeeds(imdbId) {
    console.log('\n🔄 Fetching RSS feeds for:', imdbId);
    let allStreams = [];

    for (const feed of RSS_FEEDS) {
        try {
            console.log(`\nFetching from ${feed.name}...`);
            
            const rssUrl = `${feed.url}&q=${imdbId}`;
            
            const response = await fetch(rssUrl, {
                headers: {
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
                    'Accept': 'application/rss+xml,application/xml,text/xml',
                    'Referer': 'https://torrenting.com/',
                    'Origin': 'https://torrenting.com'
                }
            });

            if (!response.ok) {
                console.error(`❌ Failed to fetch from ${feed.name}:`, response.status);
                continue;
            }

            const rssData = await response.text();
            const result = await parser.parseStringPromise(rssData);

            if (!result?.rss?.channel?.item) {
                console.log(`No items found in ${feed.name}`);
                continue;
            }

            const items = Array.isArray(result.rss.channel.item) ? 
                         result.rss.channel.item : [result.rss.channel.item];

            console.log(`Found ${items.length} items in ${feed.name}`);
            
            const streams = await Promise.all(items.map(async (item, index) => {
                try {
                    console.log(`\nProcessing item ${index + 1}/${items.length}:`, item.title);
                    
                    const torrentInfo = await downloadAndParseTorrent(item.link);
                    if (!torrentInfo) return null;

                    const { size, category } = parseTorrentInfo(item);
                    const quality = extractQuality(item.title);

                    return {
                        magnetLink: torrentInfo.magnetLink,
                        filename: torrentInfo.mainFile.path,
                        websiteTitle: item.title,
                        quality,
                        size,
                        category,
                        source: feed.name,
                        infoHash: torrentInfo.infoHash,
                        mainFileSize: torrentInfo.mainFile.length,
                        pubDate: item.pubDate
                    };
                } catch (error) {
                    console.error(`Error processing item ${index + 1}:`, error);
                    return null;
                }
            }));

            const validStreams = streams.filter(Boolean);
            console.log(`✅ Processed ${validStreams.length} valid streams from ${feed.name}`);
            allStreams = [...allStreams, ...validStreams];

        } catch (error) {
            console.error(`❌ Error fetching ${feed.name}:`, error);
        }
    }

    allStreams.sort((a, b) => {
        const qualityOrder = { '2160p': 4, '4k': 4, 'uhd': 4, '1080p': 3, '720p': 2 };
        return (qualityOrder[b.quality] || 0) - (qualityOrder[a.quality] || 0);
    });

    return allStreams;
}

function extractQuality(title) {
    const qualityMatch = title.match(/\b(2160p|1080p|720p|4k|uhd)\b/i);
    return qualityMatch ? qualityMatch[1].toLowerCase() : '';
}

export { fetchRSSFeeds };