Add more extensive fake update capabilities

This commit is contained in:
Wieland Schöbl
2021-05-28 22:28:06 +02:00
parent ccb074d81d
commit 8e323a8446
4 changed files with 54 additions and 29 deletions

View File

@@ -1,6 +1,7 @@
package de.wulkanat
import de.wulkanat.web.fakeUpdateBlogPost
import de.wulkanat.web.fakeUpdateJobListings
import net.dv8tion.jda.api.hooks.ListenerAdapter
import net.dv8tion.jda.api.EmbedBuilder
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent
@@ -23,13 +24,19 @@ class AdminCli : ListenerAdapter() {
when (command[0].value) {
"stop" -> exitProcess(1)
"fakeUpdate" -> {
// TODO: implement fake update for blog posts
// BLOG_POST_WATCHER.current = setOf()
fakeUpdateBlogPost()
if (command.size < 2) {
Admin.println("Specify type")
} else {
val amount = command.getOrNull(2)?.value?.trim('`')?.toIntOrNull() ?: 1
when (command[1].value.trim('`')) {
"blog" -> fakeUpdateBlogPost(amount)
"twitter" -> TwitterJob.lastTweetID = "poggers"
"jobs" -> fakeUpdateJobListings(amount)
else -> Admin.println("Must be blog|twitter|jobs")
}
TwitterJob.lastTweetID = "poggers"
Admin.println("Posting on next update cycle.")
Admin.println("Posting $amount on next update cycle.")
}
}
"info" -> {
Admin.info()
@@ -66,7 +73,7 @@ class AdminCli : ListenerAdapter() {
"""
**${prefix}stop**
Stop the bot
**${prefix}fakeUpdate**
**${prefix}fakeUpdate [blog|twitter|jobs] [amount]**
Post a fake update to every registered channel (can be used if bot missed the update)
**${prefix}info**
Show an overview over all registered channels

View File

@@ -8,7 +8,7 @@ import de.wulkanat.model.BlogPostPreview
import de.wulkanat.model.JobListingPreview
private const val BLOG_POST_STATE_FILE_NAME = "blog_state.json"
fun fakeUpdateBlogPost() = removeFirstFromSiteSave<BlogPostPreview>(BLOG_POST_STATE_FILE_NAME)
fun fakeUpdateBlogPost(amount: Int = 1) = removeFromSiteSave<BlogPostPreview>(BLOG_POST_STATE_FILE_NAME, amount)
fun getNewBlogPosts() = updateSite("https://hytale.com/news", BLOG_POST_STATE_FILE_NAME) { doc ->
doc["postWrapper"].map {
BlogPostPreview(
@@ -23,7 +23,7 @@ fun getNewBlogPosts() = updateSite("https://hytale.com/news", BLOG_POST_STATE_FI
}
private const val JOB_LISTING_STATE_FILE_NAME = "jobs_state.json"
fun fakeUpdateJobListings() = removeFirstFromSiteSave<JobListingPreview>(JOB_LISTING_STATE_FILE_NAME)
fun fakeUpdateJobListings(amount: Int = 1) = removeFromSiteSave<JobListingPreview>(JOB_LISTING_STATE_FILE_NAME, amount)
fun getNewJobListings() = updateSite("https://hypixelstudios.com/jobs/", JOB_LISTING_STATE_FILE_NAME) { doc ->
doc["current-jobs__departments"].flatMap { jobDepartment ->
val jobDepartmentName = jobDepartment["current-jobs__department-name"].text

View File

@@ -11,10 +11,15 @@ import java.io.IOException
/**
* Removes the first element of a saved JSON list file
*/
inline fun <reified T>
removeFirstFromSiteSave(fileName: String) = File(fileName).takeIf { it.exists() }?.let {
it.writeText(Json.encodeToString(Json.decodeFromString<List<T>>(it.readText()).toMutableList().apply { removeFirst() }))
}
inline fun <reified T> removeFromSiteSave(fileName: String, amount: Int = 1) =
File(fileName).takeIf { it.exists() }?.let {
it.writeText(
if (amount >= 0) Json.encodeToString(
Json.decodeFromString<List<T>>(it.readText()).subList(0, amount)
)
else "[]"
)
}
inline fun <reified T> updateSite(url: String, fileName: String, parser: (Document) -> List<T>) = try {
val currentStateFile = File(fileName)