continue watching section, recently added section, new endpoints in collab with server, scan for new files, and more
This commit is contained in:
126
preload.js
126
preload.js
@@ -1,7 +1,121 @@
|
||||
const { contextBridge } = require('electron');
|
||||
console.log("Preload script starting...");
|
||||
try {
|
||||
const { contextBridge } = require('electron');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
console.log("Node modules loaded successfully.");
|
||||
|
||||
// Location to store the token (adjust the location as needed)
|
||||
const tokenFile = path.join(__dirname, 'accessToken.json');
|
||||
console.log("Token file path:", tokenFile);
|
||||
|
||||
contextBridge.exposeInMainWorld('api', {
|
||||
getMovies: () => fetch('http://localhost:8000/movies').then(r => r.json()),
|
||||
getMovieDetails: (id) => fetch(`http://localhost:8000/movies/${id}`).then(r => r.json()),
|
||||
getStreamUrl: (id) => `http://localhost:8000/stream/${id}`
|
||||
});
|
||||
function loadToken() {
|
||||
try {
|
||||
const data = fs.readFileSync(tokenFile, 'utf8');
|
||||
const parsed = JSON.parse(data);
|
||||
console.log("Token loaded:", parsed.token);
|
||||
return parsed.token;
|
||||
} catch (error) {
|
||||
console.warn("loadToken error:", error.message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function saveToken(token) {
|
||||
try {
|
||||
fs.writeFileSync(tokenFile, JSON.stringify({ token }), 'utf8');
|
||||
console.log("Token saved:", token);
|
||||
} catch (error) {
|
||||
console.error("saveToken error:", error.message);
|
||||
}
|
||||
}
|
||||
|
||||
let accessToken = loadToken();
|
||||
|
||||
// Updated fetchWithAuth: if JSON returns "Invalid or expired token" then
|
||||
// dispatch a custom event that the renderer will catch.
|
||||
function fetchWithAuth(url, options = {}) {
|
||||
options.headers = options.headers || {};
|
||||
if (accessToken) {
|
||||
options.headers['Authorization'] = `Bearer ${accessToken}`;
|
||||
}
|
||||
return fetch(url, options)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.detail && data.detail === "Invalid or expired token") {
|
||||
console.error("API reports expired token.");
|
||||
accessToken = null;
|
||||
// Dispatch custom event on the window so that the renderer can show the login overlay.
|
||||
window.dispatchEvent(new CustomEvent("tokenExpired", { detail: data.detail }));
|
||||
}
|
||||
return data;
|
||||
});
|
||||
}
|
||||
|
||||
// Now update getStreamUrl and getEpisodeStreamUrl to use fetchWithAuth.
|
||||
contextBridge.exposeInMainWorld('api', {
|
||||
// Movies API calls
|
||||
getMovies: () => fetchWithAuth('http://bbrunson.com:8495/movies'),
|
||||
getMovieDetails: (id) => fetchWithAuth(`http://bbrunson.com:8495/movies/${id}`),
|
||||
getStream: (id) => `http://bbrunson.com:8495/stream/${id}`,
|
||||
// TV Shows API calls
|
||||
getShows: () => fetchWithAuth('http://bbrunson.com:8495/shows'),
|
||||
getShowDetails: (showId) => fetchWithAuth(`http://bbrunson.com:8495/shows/${showId}`),
|
||||
getShowSeasons: (showId) => fetchWithAuth(`http://bbrunson.com:8495/shows/${showId}/seasons`),
|
||||
getSeasonEpisodes: (showId, season) =>
|
||||
fetchWithAuth(`http://bbrunson.com:8495/shows/${showId}/seasons/${season}/episodes`),
|
||||
getEpisodeStream: (episodeId) => `http://bbrunson.com:8495/stream_episode/${episodeId}`,
|
||||
getSubtitles: (id) => `http://bbrunson.com:8495/subtitles/${id}`,
|
||||
|
||||
// New: In Progress API call
|
||||
getInProgress: () => fetchWithAuth('http://bbrunson.com:8495/in_progress'),
|
||||
|
||||
// New: Get Episode Details API call
|
||||
getEpisodeDetails: (id) => fetchWithAuth(`http://bbrunson.com:8495/episodes/${id}`),
|
||||
|
||||
// New: Use the /episodes/{episode_id}/show endpoint to fetch TV show details for an episode
|
||||
getShowByEpisode: (episodeId) => fetchWithAuth(`http://bbrunson.com:8495/episodes/${episodeId}/show`),
|
||||
|
||||
// Authentication functions
|
||||
login: (username, password) => {
|
||||
return fetch('http://bbrunson.com:8495/login', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password })
|
||||
})
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
if (data.access_token) {
|
||||
accessToken = data.access_token;
|
||||
saveToken(accessToken);
|
||||
}
|
||||
return data;
|
||||
});
|
||||
},
|
||||
register: (username, password) => {
|
||||
return fetch('http://bbrunson.com:8495/register', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username, password })
|
||||
})
|
||||
.then(r => r.json());
|
||||
},
|
||||
getToken: () => accessToken,
|
||||
scanForNewFiles: () => fetchWithAuth('http://bbrunson.com:8495/scan', { method: 'POST' }),
|
||||
getSessionDetails: (sessionId) => fetchWithAuth(`http://bbrunson.com:8495/sessions/${sessionId}`),
|
||||
|
||||
// new progress APIs
|
||||
saveProgress: (mediaType, mediaId, lastPosition) =>
|
||||
fetchWithAuth(`http://bbrunson.com:8495/save_progress/${mediaType}/${mediaId}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ last_position: lastPosition })
|
||||
}),
|
||||
getProgress: (mediaType, mediaId) =>
|
||||
fetchWithAuth(`http://bbrunson.com:8495/get_progress/${mediaType}/${mediaId}`)
|
||||
});
|
||||
|
||||
console.log("Preload script finished register API.");
|
||||
} catch (error) {
|
||||
console.error('Error in preload.js:', error);
|
||||
}
|
||||
Reference in New Issue
Block a user