backend-manager 3.2.146 → 3.2.148

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "3.2.146",
3
+ "version": "3.2.148",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -2,15 +2,17 @@
2
2
  const fetch = require('wonderful-fetch');
3
3
  const powertools = require('node-powertools');
4
4
  const moment = require('moment');
5
+ const JSON5 = require('json5');
5
6
 
6
7
  const PROMPT = `
7
- Company: {name}: {description}
8
- Current Date: {date}
8
+ Company: {app.name}: {app.brand.description}
9
+ Date: {date}
9
10
  Instructions: {prompt}
10
11
 
11
- Use the following information to find a topic related to our company description:
12
+ Use the following information to find a topic related to our company:
12
13
  {suggestion}
13
14
  `
15
+
14
16
  const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36';
15
17
 
16
18
  function Module() {
@@ -28,24 +30,11 @@ Module.prototype.main = function (assistant, context) {
28
30
  // Log
29
31
  assistant.log(`Starting...`);
30
32
 
31
- // Get settings
32
- const settings = Manager.config.ghostii;
33
-
34
33
  // Set post ID
35
34
  self.postId = moment().unix();
36
35
 
37
- // Log
38
- assistant.log(`Settings`, settings);
39
-
40
- // Quit if articles are disabled
41
- if (!settings.articles || !settings.sources.length) {
42
- assistant.log(`Quitting because articles are disabled`);
43
-
44
- return resolve();
45
- }
46
-
47
36
  // Get app content
48
- self.appObject = await self.getAppData().catch((e) => e);
37
+ self.appObject = await self.getAppData(Manager.config.app.id).catch((e) => e);
49
38
  if (self.appObject instanceof Error) {
50
39
  return reject(self.appObject);
51
40
  }
@@ -53,14 +42,45 @@ Module.prototype.main = function (assistant, context) {
53
42
  // Log
54
43
  assistant.log(`App object`, self.appObject);
55
44
 
56
- // Harvest articles
57
- const result = await self.harvest(settings).catch((e) => e);
58
- if (result instanceof Error) {
59
- return reject(result);
60
- }
45
+ // Get settings
46
+ let settingsArray = powertools.arrayify(Manager.config.ghostii);
61
47
 
62
- // Log
63
- assistant.log(`Finished!`, result);
48
+ // Loop through each item
49
+ for (const settings of settingsArray) {
50
+ const appId = settings.app || self.appObject.id;
51
+
52
+ // Fix settings
53
+ settings.articles = settings.articles || 0;
54
+ settings.sources = settings.sources || [];
55
+ settings.prompt = settings.prompt || '';
56
+ settings.app = await self.getAppData(appId).catch((e) => e);
57
+
58
+ // Check for errors
59
+ if (settings.app instanceof Error) {
60
+ assistant.error(`Error fetching app data`, settings.app);
61
+
62
+ continue;
63
+ }
64
+
65
+ // Log
66
+ assistant.log(`Settings (app=${appId})`, settings);
67
+
68
+ // Quit if articles are disabled
69
+ if (!settings.articles || !settings.sources.length) {
70
+ assistant.log(`Quitting because articles are disabled`);
71
+
72
+ continue;
73
+ }
74
+
75
+ // Harvest articles
76
+ const result = await self.harvest(settings).catch((e) => e);
77
+ if (result instanceof Error) {
78
+ return reject(result);
79
+ }
80
+
81
+ // Log
82
+ assistant.log(`Finished!`, result);
83
+ }
64
84
 
65
85
  // Resolve
66
86
  return resolve();
@@ -80,7 +100,7 @@ Module.prototype.harvest = function (settings) {
80
100
  const date = moment().format('MMMM YYYY');
81
101
 
82
102
  // Log
83
- assistant.log(`harvest(): Starting...`);
103
+ assistant.log(`harvest(): Starting ${settings.app.id}...`);
84
104
 
85
105
  // Process the number of sources in the settings
86
106
  for (let index = 0; index < settings.articles; index++) {
@@ -109,8 +129,7 @@ Module.prototype.harvest = function (settings) {
109
129
 
110
130
  // Set suggestion
111
131
  const final = powertools.template(PROMPT, {
112
- name: self.appObject.name,
113
- description: self.appObject.brand.description,
132
+ ...settings,
114
133
  prompt: settings.prompt,
115
134
  date: date,
116
135
  suggestion: suggestion,
@@ -120,7 +139,7 @@ Module.prototype.harvest = function (settings) {
120
139
  assistant.log('harvest(): Get final content', final);
121
140
 
122
141
  // Request to Ghostii
123
- const article = await self.requestGhostii(final).catch((e) => e);
142
+ const article = await self.requestGhostii(settings, final).catch((e) => e);
124
143
  if (article instanceof Error) {
125
144
  assistant.error(`harvest(): Error requesting Ghostii`, article);
126
145
 
@@ -131,7 +150,7 @@ Module.prototype.harvest = function (settings) {
131
150
  assistant.log(`harvest(): Article`, article);
132
151
 
133
152
  // Upload post to blog
134
- const uploadedPost = await self.uploadPost(article).catch((e) => e);
153
+ const uploadedPost = await self.uploadPost(settings, article).catch((e) => e);
135
154
  if (uploadedPost instanceof Error) {
136
155
  assistant.error(`harvest(): Error uploading post to blog`, uploadedPost);
137
156
 
@@ -147,7 +166,7 @@ Module.prototype.harvest = function (settings) {
147
166
  });
148
167
  }
149
168
 
150
- Module.prototype.getAppData = function (settings) {
169
+ Module.prototype.getAppData = function (id) {
151
170
  const self = this;
152
171
 
153
172
  // Shortcuts
@@ -164,16 +183,9 @@ Module.prototype.getAppData = function (settings) {
164
183
  tries: 3,
165
184
  response: 'json',
166
185
  body: {
167
- id: Manager.config.app.id,
186
+ id: id,
168
187
  },
169
188
  })
170
- // .then((r) => {
171
- // return resolve({
172
- // name: r?.name,
173
- // description: r?.brand?.description || '',
174
- // acceptable: r?.sponsorships?.acceptable || [],
175
- // })
176
- // })
177
189
  .then((r) => resolve(r))
178
190
  .catch((e) => reject(e));
179
191
  });
@@ -193,13 +205,16 @@ Module.prototype.getURLContent = function (url) {
193
205
  fetch(url, {
194
206
  timeout: 30000,
195
207
  tries: 3,
196
- response: 'text',
208
+ response: 'raw',
197
209
  headers: {
198
210
  'User-Agent': USER_AGENT,
199
211
  }
200
212
  })
201
- .then((r) => {
202
- return resolve(extractBodyContent(r));
213
+ .then(async (r) => {
214
+ const contentType = res.headers.get('content-type');
215
+ const text = await res.text();
216
+
217
+ return resolve(extractBodyContent(text, contentType, url));
203
218
  })
204
219
  .catch((e) => reject(e));
205
220
  });
@@ -222,7 +237,7 @@ Module.prototype.isURL = function (url) {
222
237
  }
223
238
 
224
239
  // Request to Ghostii
225
- Module.prototype.requestGhostii = function (content) {
240
+ Module.prototype.requestGhostii = function (settings, content) {
226
241
  const self = this;
227
242
 
228
243
  // Shortcuts
@@ -244,6 +259,8 @@ Module.prototype.requestGhostii = function (content) {
244
259
  description: content,
245
260
  insertLinks: true,
246
261
  headerImageUrl: 'unsplash',
262
+ url: settings.app.url,
263
+ feedUrl: `${settings.app.url}/feeds/posts.json`,
247
264
  },
248
265
  })
249
266
  .then((r) => resolve(r))
@@ -251,7 +268,7 @@ Module.prototype.requestGhostii = function (content) {
251
268
  });
252
269
  }
253
270
 
254
- Module.prototype.uploadPost = function (article) {
271
+ Module.prototype.uploadPost = function (settings, article) {
255
272
  const self = this;
256
273
 
257
274
  // Shortcuts
@@ -262,7 +279,7 @@ Module.prototype.uploadPost = function (article) {
262
279
 
263
280
  return new Promise(async function(resolve, reject) {
264
281
  // Fetch URL
265
- fetch(`${self.appObject.server}/bm_api`, {
282
+ fetch(`${settings.app.server}/bm_api`, {
266
283
  method: 'post',
267
284
  timeout: 90000,
268
285
  tries: 1,
@@ -279,8 +296,8 @@ Module.prototype.uploadPost = function (article) {
279
296
  id: self.postId++,
280
297
  author: 'alex',
281
298
  path: 'ghostii',
282
- githubUser: self.appObject.github.user,
283
- githubRepo: self.appObject.github.repo,
299
+ githubUser: settings.app.github.user,
300
+ githubRepo: settings.app.github.repo,
284
301
  },
285
302
  },
286
303
  })
@@ -289,9 +306,22 @@ Module.prototype.uploadPost = function (article) {
289
306
  });
290
307
  }
291
308
 
292
- const extractBodyContent = (html) => {
309
+ const extractBodyContent = (text, contentType, url) => {
310
+ const parsed = tryParse(text);
311
+
312
+ // Try JSON
313
+ if (parsed) {
314
+ // If it's from rss.app, extract the content
315
+ if (url.includes('rss.app') && parsed.items) {
316
+ return parsed.items.map((i) => `${i.title}: ${i.content_text}`).join('\n');
317
+ }
318
+
319
+ // If we cant recognize the JSON, return the original text
320
+ return text;
321
+ }
322
+
293
323
  // Extract the content within the body tag
294
- const bodyMatch = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
324
+ const bodyMatch = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
295
325
  if (!bodyMatch) return '';
296
326
 
297
327
  let bodyContent = bodyMatch[1];
@@ -304,4 +334,12 @@ const extractBodyContent = (html) => {
304
334
  return bodyContent.replace(/<[^>]*>/g, ' ').replace(/\s+/g, ' ').trim();
305
335
  };
306
336
 
337
+ function tryParse(json) {
338
+ try {
339
+ return JSON5.parse(json);
340
+ } catch (e) {
341
+ return null;
342
+ }
343
+ };
344
+
307
345
  module.exports = Module;
@@ -33,12 +33,14 @@
33
33
  appId: '1:123:web:456',
34
34
  measurementId: 'G-0123456789',
35
35
  },
36
- ghostii: {
37
- articles: 1,
38
- sources: [
39
- '$app',
40
- // Add more sources here
41
- ],
42
- prompt: '',
43
- },
36
+ ghostii: [
37
+ {
38
+ articles: 1,
39
+ sources: [
40
+ '$app',
41
+ // Add more sources here
42
+ ],
43
+ prompt: '',
44
+ }
45
+ ],
44
46
  }