mikmc commited on
Commit
52a2d13
·
verified ·
1 Parent(s): 76042b9

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +88 -5
index.js CHANGED
@@ -33,6 +33,30 @@ const { searchCatalog, getMeta, getStreams } = handlers({
33
 
34
  const addon = builder({ env });
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  // Define the Catalog Handler
37
  addon.defineCatalogHandler(async (args) => {
38
  console.log("Catalog Handler Invoked with Arguments:", args);
@@ -80,12 +104,71 @@ addon.defineCatalogHandler(async (args) => {
80
  }
81
  });
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- // Define meta and stream handlers
85
- addon.defineMetaHandler(getMeta);
86
- addon.defineStreamHandler(getStreams);
 
 
 
 
87
 
88
  // Start the server
89
- server(addon).serve({
90
- port: PORT,
91
  });
 
33
 
34
  const addon = builder({ env });
35
 
36
+ // Define shared dependencies for Catalog, Meta, and Stream
37
+ const dependencies = {
38
+ httpClient: axios,
39
+ env: {
40
+ url: {
41
+ search: 'https://archive.org/advancedsearch.php',
42
+ metadata: 'https://archive.org/metadata',
43
+ stream: 'https://archive.org/download',
44
+ },
45
+ },
46
+ };
47
+
48
+ // Integrate Catalog, Meta, and Stream handlers
49
+ const Catalog = require('./catalog');
50
+ const Meta = require('./meta');
51
+ const Stream = require('./stream');
52
+
53
+ // Export Stremio add-on functions
54
+ module.exports = {
55
+ ...Catalog(dependencies),
56
+ ...Meta(dependencies),
57
+ ...Stream(dependencies),
58
+ };
59
+
60
  // Define the Catalog Handler
61
  addon.defineCatalogHandler(async (args) => {
62
  console.log("Catalog Handler Invoked with Arguments:", args);
 
104
  }
105
  });
106
 
107
+ // Define the Metadata Handler
108
+ addon.defineMetaHandler(async (args) => {
109
+ console.log("Meta Handler Invoked with Arguments:", args);
110
+
111
+ try {
112
+ const { id } = args;
113
+ const response = await axios.get(process.env.METADATA_URL, {
114
+ params: { identifier: id },
115
+ });
116
+
117
+ console.log("Response from Archive.org METADATA_URL:", response.data);
118
+
119
+ if (!response.data || !response.data.metadata) {
120
+ console.warn("No metadata found for identifier:", id);
121
+ return { meta: {} };
122
+ }
123
+
124
+ const metadata = response.data.metadata;
125
+ return {
126
+ meta: {
127
+ id,
128
+ name: metadata.title || 'Unknown Title',
129
+ description: metadata.description || 'No description available',
130
+ poster: `${process.env.IMAGE_URL}/${id}`,
131
+ type: 'movie',
132
+ },
133
+ };
134
+ } catch (error) {
135
+ console.error("Error in Meta Handler:", error.response?.data || error.message); // Improved error logging
136
+ return { meta: {} };
137
+ }
138
+ });
139
+
140
+ // Define the Stream Handler
141
+ addon.defineStreamHandler(async (args) => {
142
+ console.log("Stream Handler Invoked with Arguments:", args);
143
+
144
+ try {
145
+ const { id } = args;
146
+ const response = await axios.get(process.env.STREAM_URL, {
147
+ params: { identifier: id },
148
+ });
149
+
150
+ console.log("Response from Archive.org STREAM_URL:", response.data);
151
+
152
+ if (!response.data || !response.data.files) {
153
+ console.warn("No stream files found for identifier:", id);
154
+ return { streams: [] };
155
+ }
156
+
157
+ const streams = response.data.files.map((file) => ({
158
+ title: file.name,
159
+ url: file.url,
160
+ quality: file.format,
161
+ }));
162
 
163
+ console.log("Generated Streams:", streams);
164
+ return { streams };
165
+ } catch (error) {
166
+ console.error("Error in Stream Handler:", error.response?.data || error.message); // Improved error logging
167
+ return { streams: [] };
168
+ }
169
+ });
170
 
171
  // Start the server
172
+ server.start(PORT, () => {
173
+ console.log(`Server is running on port ${PORT}`);
174
  });