-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathindex.js
105 lines (79 loc) · 2.62 KB
/
index.js
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
const { execSync } = require("child_process");
const fs = require("fs");
require("dotenv").config();
const wordcount = require("word-count");
const { createWordpressDraft } = require("./api");
const { generateArticle, generateTitles } = require("./gen");
const { logger } = require("./logger")
const { queryDdg, summarizeContent } = require("./serp");
const { serve } = require("@hono/node-server");
const { Hono } = require("hono");
const readKeywords = () => {
const files = fs.readdirSync("./keywords");
return files;
};
const main = async (keyword, skipPublish = false) => {
if (!keyword) {
logger.info("Please provide a keyword");
return;
}
logger.info(`Processing keyword: ${keyword}`);
// const results = await queryGoogle(keyword);
const results = await queryDdg(keyword);
if (results.length === 0) {
logger.info(
"No results found. Something might be wrong with this keyword. Skipping...",
);
return;
}
let summaries = [];
for (const result of results) {
logger.info(`PROCESSING "${result.title} - ${result.url}"`);
const summary = await summarizeContent(result.url);
summaries.push(summary);
}
logger.info(`Summarized ${summaries.length} articles. Generating article...`);
const article = await generateArticle(keyword, summaries);
logger.info(`Article generated. ${wordcount(article)} words`);
const titles = await generateTitles(keyword);
logger.info(`Titles generated: \n${titles}`);
const fileContent = `
${titles}
${article}
`;
const enableWordpress = process.env.ENABLE_WORDPRESS;
if (!skipPublish && enableWordpress) {
logger.info(`Creating Wordpress draft with title: ${keyword}`);
await createWordpressDraft(keyword, fileContent);
logger.info(`Wordpress draft created`);
}
fs.writeFileSync(`./articles/${keyword}.md`, fileContent);
logger.info(`Article saved to ./articles/${keyword}.md`);
// logger.info(`Deleting keyword file`);
// fs.unlinkSync(`./keywords/${keyword}`);
logger.info("DONE");
return { keyword, titles, article };
};
const run = async () => {
const keywords = readKeywords();
await main(keywords[0]);
execSync("sleep 5");
run();
};
const app = new Hono();
app.get("/", async (c) => {
const keyword = c.req.query("keyword");
const skipPublish = !!c.req.query("skip_publish") || false;
if (!keyword) {
return c.throw(400, "Please provide a keyword");
}
const result = await main(keyword, skipPublish);
return c.json(result);
});
const port = process.env.PORT || "5139";
serve({
fetch: app.fetch,
port,
});
console.log(`Listening on port ${port}`);
module.exports = { logger }