File size: 4,960 Bytes
c0b3afa
5b22b17
c0b3afa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b22b17
 
 
 
 
 
 
c0b3afa
5b22b17
c0b3afa
5b22b17
 
 
c0b3afa
5b22b17
c0b3afa
5b22b17
 
 
c0b3afa
5b22b17
c0b3afa
5b22b17
 
 
 
 
 
c0b3afa
5b22b17
c0b3afa
 
5b22b17
c0b3afa
 
5b22b17
 
c0b3afa
5b22b17
c0b3afa
5b22b17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0b3afa
 
5b22b17
 
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
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
const log = require('../helpers/logger');
const { providersDb } = require('../helpers/db');
const { makeRequest } = require('../api/tmdb');

async function fetchProvidersFromTMDB(apiKey) {
    try {
        const [movieData, tvData] = await Promise.all([
            makeRequest(`https://api.themoviedb.org/3/watch/providers/movie`, apiKey),
            makeRequest(`https://api.themoviedb.org/3/watch/providers/tv`, apiKey)
        ]);

        if (!movieData || !tvData) {
            throw new Error('Failed to fetch providers from TMDB');
        }

        return [...movieData.results, ...tvData.results];
    } catch (error) {
        log.error(`Error fetching providers from TMDB: ${error.message}`);
        throw error;
    }
}

function fetchProvidersFromDatabase() {
    return new Promise((resolve, reject) => {
        providersDb.all(`SELECT * FROM providers`, (err, rows) => {
            if (err) {
                log.error(`Error fetching providers from the database: ${err.message}`);
                return reject(err);
            }

            log.debug(`Fetched ${rows ? rows.length : 0} providers from the database.`);

            if (rows && rows.length > 0) {
                const now = new Date();
                const lastFetched = new Date(rows[0].last_fetched);

                log.debug(`Current time: ${now.toISOString()}, Last fetched: ${lastFetched.toISOString()}`);

                if (!isNaN(lastFetched.getTime())) {
                    const timeDifference = now.getTime() - lastFetched.getTime();
                    const twentyFourHoursInMillis = 24 * 60 * 60 * 1000;

                    log.debug(`Time difference: ${timeDifference} ms (${(timeDifference / (1000 * 60)).toFixed(2)} minutes)`);

                    if (timeDifference < twentyFourHoursInMillis) {
                        log.info('Providers fetched from the database (less than 24 hours old).');
                        return resolve(rows);
                    } else {
                        log.info(`Providers are older than 24 hours. Time since last fetch: ${(timeDifference / (1000 * 60 * 60)).toFixed(2)} hours.`);
                    }
                } else {
                    log.error('Invalid date format for last_fetched.');
                }
            } else {
                log.info('No providers found in the database.');
            }

            resolve(null);
        });
    });
}

function updateProvidersInDatabase(providers) {
    return new Promise((resolve, reject) => {
        const mergedProviders = {};

        providers.forEach(provider => {
            const { provider_id, provider_name, logo_path, display_priorities } = provider;
            if (!mergedProviders[provider_name]) {
                mergedProviders[provider_name] = { 
                    provider_id, 
                    provider_name, 
                    logo_path, 
                    display_priorities: JSON.stringify(display_priorities)
                };
            }
        });

        const uniqueProviders = Object.values(mergedProviders);

        const insertOrUpdateProvider = `
            INSERT INTO providers (provider_id, provider_name, logo_path, display_priorities, last_fetched) 
            VALUES (?, ?, ?, ?, ?)
            ON CONFLICT(provider_id) DO UPDATE SET
                provider_name = excluded.provider_name,
                logo_path = excluded.logo_path,
                display_priorities = excluded.display_priorities,
                last_fetched = excluded.last_fetched;
        `;

        const currentTimestamp = new Date().toISOString();
        providersDb.serialize(() => {
            const stmt = providersDb.prepare(insertOrUpdateProvider);
            uniqueProviders.forEach(provider => {
                stmt.run(
                    provider.provider_id, 
                    provider.provider_name, 
                    provider.logo_path, 
                    provider.display_priorities,
                    currentTimestamp,
                    (err) => {
                        if (err) {
                            log.error('Error updating provider in database:', err.message);
                            return reject(err);
                        }
                        log.debug(`Inserted/Updated provider: ${provider.provider_name} (ID: ${provider.provider_id})`);
                    }
                );
            });
            stmt.finalize();
        });

        log.info('Providers successfully updated in the database.');
        resolve(uniqueProviders);
    });
}

async function getProviders(apiKey) {
    const providersFromDb = await fetchProvidersFromDatabase();
    if (providersFromDb) {
        return providersFromDb;
    }

    const providersFromApi = await fetchProvidersFromTMDB(apiKey);
    const updatedProviders = await updateProvidersInDatabase(providersFromApi);
    return updatedProviders;
}

module.exports = { getProviders };