-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileBased.ts
62 lines (51 loc) · 2.25 KB
/
FileBased.ts
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
import { BaseGeneratorPlugin } from "@/lib/BaseGeneratorPlugin"
import * as fs from "fs"
import * as path from "path"
export class FileBased extends BaseGeneratorPlugin {
name = "FileBased"
sentPostsFile: string
Posts: { [key: string]: string } = {}
PostsArray: { id: string, text: string }[]
sentPostIDs: Set<string>
lastPost: { id: string, text: string }
constructor() {
super()
this.generatePost = this.generatePost.bind(this)
this.loadPosts = this.loadPosts.bind(this)
this.loadPosts()
this.ready()
}
async generatePost(): Promise<string | void> {
const availablePosts = this.PostsArray.filter(Post => !this.sentPostIDs.has(Post.id))
if (availablePosts.length === 0) return
const PostContent = availablePosts[Math.floor(Math.random() * availablePosts.length)]
this.log(`Pulled post (${PostContent.id}): ${PostContent.text}`)
this.lastPost = PostContent
return PostContent.text
}
async confirmPostSent() {
if (!this.lastPost) return
this.sentPostIDs.add(this.lastPost.id)
// write sent Post to file
fs.appendFileSync(this.sentPostsFile, this.lastPost.id + "\n")
this.lastPost = undefined
}
loadPosts() {
// load all Posts from Posts folder
const postsPath = path.resolve(__dirname, "../../../posts/")
const PostFiles = fs.readdirSync(postsPath, "utf-8")
for (const PostFile of PostFiles) {
if (!PostFile.match(/\.json$/)) continue
const PostFileData = fs.readFileSync(path.join(postsPath, PostFile), "utf-8")
const PostFileJSON = JSON.parse(PostFileData)
Object.entries(PostFileJSON).forEach(([id, text]) => {
const key = `${PostFile.replace(/\.json$/, "")}-${id}`
this.Posts[key] = text as string
})
this.sentPostsFile = "sent-posts.txt"
this.PostsArray = Object.entries(this.Posts).map(([id, text]) => ( {id, text} ))
}
this.log(`Loaded ${this.PostsArray.length} posts from ${PostFiles.length} files.`)
this.sentPostIDs = new Set(fs.existsSync(this.sentPostsFile) ? fs.readFileSync(this.sentPostsFile, "utf-8").split("\n") : [])
}
}