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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
}
|
| 11 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
| 1 |
+
'use strict';
|
| 2 |
|
| 3 |
+
require('dotenv').config(); // Load environment variables
|
| 4 |
+
|
| 5 |
+
const axios = require('axios');
|
| 6 |
+
const builder = require('./addon');
|
| 7 |
+
const server = require('./server'); // Importing the custom server
|
| 8 |
+
const { serve } = server; // Destructuring 'serve' from the server module
|
| 9 |
+
const useCases = require('./use-cases');
|
| 10 |
+
const handlers = require('./handlers');
|
| 11 |
+
const { queryBuilder, transformModel, validate, parseTorrent } = require('./commons');
|
| 12 |
+
const { env } = require('./config');
|
| 13 |
+
|
| 14 |
+
// Log the environment variables to ensure they load correctly
|
| 15 |
+
console.log("Environment Variables Loaded:");
|
| 16 |
+
console.log("SEARCH_URL:", process.env.SEARCH_URL);
|
| 17 |
+
console.log("METADATA_URL:", process.env.METADATA_URL);
|
| 18 |
+
console.log("IMAGE_URL:", process.env.IMAGE_URL);
|
| 19 |
+
console.log("STREAM_URL:", process.env.STREAM_URL);
|
| 20 |
+
|
| 21 |
+
const PORT = process.env.PORT || env.port;
|
| 22 |
+
|
| 23 |
+
// Initialize handlers
|
| 24 |
+
const { searchCatalog, getMeta, getStreams } = handlers({
|
| 25 |
+
env,
|
| 26 |
+
...transformModel({ env, ...parseTorrent() }),
|
| 27 |
+
...validate({ env }),
|
| 28 |
+
...useCases({
|
| 29 |
+
httpClient: axios,
|
| 30 |
+
queryBuilder,
|
| 31 |
+
env,
|
| 32 |
+
}),
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
const addon = builder({ env });
|
| 36 |
+
|
| 37 |
+
// Define shared dependencies for Catalog, Meta, and Stream
|
| 38 |
+
const dependencies = {
|
| 39 |
+
httpClient: axios,
|
| 40 |
+
env: {
|
| 41 |
+
url: {
|
| 42 |
+
search: 'https://archive.org/advancedsearch.php',
|
| 43 |
+
metadata: 'https://archive.org/metadata',
|
| 44 |
+
stream: 'https://archive.org/download',
|
| 45 |
+
},
|
| 46 |
+
},
|
| 47 |
+
};
|
| 48 |
+
|
| 49 |
+
// Integrate Catalog, Meta, and Stream handlers from the './use-cases' folder
|
| 50 |
+
const Catalog = require('./use-cases/catalog');
|
| 51 |
+
const Meta = require('./use-cases/meta');
|
| 52 |
+
const Stream = require('./use-cases/stream');
|
| 53 |
+
|
| 54 |
+
// Export Stremio add-on functions
|
| 55 |
+
module.exports = {
|
| 56 |
+
...Catalog(dependencies),
|
| 57 |
+
...Meta(dependencies),
|
| 58 |
+
...Stream(dependencies),
|
| 59 |
+
};
|
| 60 |
+
|
| 61 |
+
// Define the Catalog Handler
|
| 62 |
+
addon.defineCatalogHandler(async (args) => {
|
| 63 |
+
console.log("Catalog Handler Invoked with Arguments:", args);
|
| 64 |
+
console.log("Extra Arguments:", args.extra);
|
| 65 |
+
|
| 66 |
+
try {
|
| 67 |
+
const query = queryBuilder(args);
|
| 68 |
+
console.log("Generated Lucene Query:", query);
|
| 69 |
+
|
| 70 |
+
if (!query) {
|
| 71 |
+
console.warn("Empty query generated, returning empty catalog.");
|
| 72 |
+
return { metas: [] };
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
// Adjusted fields to fetch
|
| 76 |
+
const fieldsToFetch = ['title', 'identifier']; // Adjust as needed
|
| 77 |
+
const response = await axios.get(process.env.SEARCH_URL, {
|
| 78 |
+
params: {
|
| 79 |
+
q: query,
|
| 80 |
+
fl: fieldsToFetch,
|
| 81 |
+
rows: 50,
|
| 82 |
+
output: 'json',
|
| 83 |
+
},
|
| 84 |
+
});
|
| 85 |
+
|
| 86 |
+
console.log("Response from Archive.org SEARCH_URL:", response.data);
|
| 87 |
+
|
| 88 |
+
if (!response.data || response.data.response.docs.length === 0) {
|
| 89 |
+
console.warn("Empty response data from SEARCH_URL.");
|
| 90 |
+
return { metas: [] };
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
const metas = response.data.response.docs.map((item) => ({
|
| 94 |
+
id: item.identifier,
|
| 95 |
+
name: item.title || "Unknown Title",
|
| 96 |
+
poster: `${process.env.IMAGE_URL}/${item.identifier}`,
|
| 97 |
+
type: 'movie',
|
| 98 |
+
}));
|
| 99 |
+
|
| 100 |
+
console.log("Generated Metadata for Catalog:", metas);
|
| 101 |
+
return { metas };
|
| 102 |
+
} catch (error) {
|
| 103 |
+
console.error("Error in Catalog Handler:", error.response?.data || error.message); // Improved error logging
|
| 104 |
+
return { metas: [] };
|
| 105 |
+
}
|
| 106 |
+
});
|
| 107 |
+
|
| 108 |
+
// Define the Metadata Handler
|
| 109 |
+
addon.defineMetaHandler(async (args) => {
|
| 110 |
+
console.log("Meta Handler Invoked with Arguments:", args);
|
| 111 |
+
|
| 112 |
+
try {
|
| 113 |
+
const { id } = args;
|
| 114 |
+
const response = await axios.get(process.env.METADATA_URL, {
|
| 115 |
+
params: { identifier: id },
|
| 116 |
+
});
|
| 117 |
+
|
| 118 |
+
console.log("Response from Archive.org METADATA_URL:", response.data);
|
| 119 |
+
|
| 120 |
+
if (!response.data || !response.data.metadata) {
|
| 121 |
+
console.warn("No metadata found for identifier:", id);
|
| 122 |
+
return { meta: {} };
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
const metadata = response.data.metadata;
|
| 126 |
+
return {
|
| 127 |
+
meta: {
|
| 128 |
+
id,
|
| 129 |
+
name: metadata.title || 'Unknown Title',
|
| 130 |
+
description: metadata.description || 'No description available',
|
| 131 |
+
poster: `${process.env.IMAGE_URL}/${id}`,
|
| 132 |
+
type: 'movie',
|
| 133 |
+
},
|
| 134 |
+
};
|
| 135 |
+
} catch (error) {
|
| 136 |
+
console.error("Error in Meta Handler:", error.response?.data || error.message); // Improved error logging
|
| 137 |
+
return { meta: {} };
|
| 138 |
}
|
| 139 |
+
});
|
| 140 |
+
|
| 141 |
+
// Define the Stream Handler
|
| 142 |
+
addon.defineStreamHandler(async (args) => {
|
| 143 |
+
console.log("Stream Handler Invoked with Arguments:", args);
|
| 144 |
+
|
| 145 |
+
try {
|
| 146 |
+
const { id } = args;
|
| 147 |
+
const response = await axios.get(process.env.STREAM_URL, {
|
| 148 |
+
params: { identifier: id },
|
| 149 |
+
});
|
| 150 |
+
|
| 151 |
+
console.log("Response from Archive.org STREAM_URL:", response.data);
|
| 152 |
|
| 153 |
+
if (!response.data || !response.data.files) {
|
| 154 |
+
console.warn("No stream files found for identifier:", id);
|
| 155 |
+
return { streams: [] };
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
const streams = response.data.files.map((file) => ({
|
| 159 |
+
title: file.name,
|
| 160 |
+
url: file.url,
|
| 161 |
+
quality: file.format,
|
| 162 |
+
}));
|
| 163 |
+
|
| 164 |
+
console.log("Generated Streams:", streams);
|
| 165 |
+
return { streams };
|
| 166 |
+
} catch (error) {
|
| 167 |
+
console.error("Error in Stream Handler:", error.response?.data || error.message); // Improved error logging
|
| 168 |
+
return { streams: [] };
|
| 169 |
}
|
| 170 |
+
});
|
| 171 |
+
|
| 172 |
+
// Debugging: Check the server module
|
| 173 |
+
console.log('Server module:', server); // Log the server object
|
| 174 |
+
console.log('Destructured serve function:', serve); // Log the serve function
|
| 175 |
|
| 176 |
+
// Start the server with the corrected method
|
| 177 |
+
serve({ port: PORT }); // Start the server on the given port
|