File size: 2,685 Bytes
c0b3afa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const log = require('./logger');
const { getContentDetailsById, getImdbId } = require('../api/tmdb');

const prepareStreams = async (content, apiKey, language, showRating, showTagline, userAgent = '', type) => {
    const today = new Date();

    if (!Array.isArray(content)) {
        throw new TypeError('Expected content to be an array');
    }

    const contentDetails = await Promise.all(
        content.map(item => getContentDetailsById(item, type, apiKey, language))
    );

    const imdbIdResults = await Promise.all(contentDetails.map(async item => {
        return (new Date(item.released) <= today) ? getImdbId(item.id, type, apiKey, language) : null;
    }));

    const preparedContent = contentDetails.map((item, index) => {
        const rating = showRating ? (item.rating?.toFixed(1) || 'N/A') : '';
        const ratingValue = parseFloat(rating);
        const emoji = ratingValue > 0 ? ratingToEmoji(ratingValue) : '';
        const ratingText = ratingValue > 0 ? `${rating} ${emoji}` : '';
        const voteCountText = showRating && item.vote_count ? ` (${formatVoteCount(item.vote_count)} 👥)` : '';

        const externalUrl = item.released
            ? (new Date(item.released) > today
                ? `https://www.themoviedb.org/${type}/${item.id}`
                : userAgent.includes('Stremio')
                    ? `stremio:///detail/${type}/${imdbIdResults[index] || ''}`
                    : `https://web.stremio.com/#/detail/${type}/${imdbIdResults[index] || ''}`)
            : `https://www.themoviedb.org/${type}/${item.id}`;

        const newLine = '\n';
        const title = `${item.title}${ratingText ? `${newLine}${ratingText}` : ''}${voteCountText ? `${voteCountText}` : ''}${showTagline && item.tagline ? `${newLine}${item.tagline}` : ''}`;

        return {
            name: item.released ? item.released.match(/^\d{4}/)?.[0] || 'TMDB' : 'TMDB',
            title: title,
            externalUrl: externalUrl,
            rating: rating,
            ratingValue: ratingValue,
            emoji: emoji,
            ratingText: ratingText,
            voteCountText: voteCountText
        };
    });

    return preparedContent;
};


const formatVoteCount = (voteCount) => {
    if (voteCount >= 1000000) {
        return `${Math.round(voteCount / 1000000)}M`;
    } else if (voteCount >= 1000) {
        return `${Math.round(voteCount / 1000)}k`;
    }
    return voteCount.toString();
};

const ratingToEmoji = (rating) => {
    if (rating >= 9) return '🏆';
    if (rating >= 8) return '🔥';
    if (rating >= 6) return '⭐';
    if (rating >= 5) return '😐';
    return '🥱';
};

module.exports = {
    prepareStreams
};