Skip to content

Commit

Permalink
just a lot of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
xJade00 committed Nov 29, 2019
1 parent a4bd2b3 commit 6e9efe1
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 150 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ A RSS parser that posts to discord webhooks with new posts.
| apoPort | The port for the API to listen to | 8080 |
| fdbCluster | The FDB cluster to grab info from. | /etc/foundationdb/fdb.cluster |
| redditUser | The name of the reddit user to put in the User-Agent. | ? |
| webhookProxy | The proxy to send webhooks through. For instance if you wanted to send it to https://mywebsite.com/api/webhooks/$ID/$TOKEN, then you'd set this value to mywebsite.com/api/webhooks/ | ?

9 changes: 5 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ version := "0.1"

scalaVersion := "2.13.0"

resolvers += "jitpack" at "https://jitpack.io"
resolvers += JCenterRepository
resolvers ++= Seq(
JCenterRepository,
"jitpack" at "https://jitpack.io"
)

libraryDependencies ++= Seq(
"com.github.pathikrit" %% "better-files" % "3.8.0",
"com.typesafe.play" %% "play-json" % "2.8.0-M4",
"org.foundationdb" % "fdb-java" % "6.1.9",
"org.scalaj" %% "scalaj-http" % "2.4.2",
"org.scala-lang.modules" %% "scala-xml" % "1.2.0",
"com.github.xaanit" % "Scalalin" % "2d3c325",
"club.minnced" % "discord-webhooks" % "0.1.7"
"com.github.xaanit" % "Scalalin" % "2d3c325"
)

mainClass in assembly := Some("it.xaan.rss.Main")
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/it/xaan/rss/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scala.util.{Failure, Success, Try}

object Main {
def main(args: Array[String]): Unit = {
implicit val format: OFormat[Config] = Json.format[Config]
implicit val format: OFormat[Config] = Json.format[Config]
val cfg = File("./config.json")
cfg.createFileIfNotExists()
val config = Try(Json.parse(cfg.byteArray).validate[Config].get) match {
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/it/xaan/rss/Master.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class Master(val config: Config) {

def start(): Unit =
executor.scheduleAtFixedRate(() => {
val all = fdb.allFeeds
val all = fdb.allFeeds.getOrElse(Set())
if (premium.get()) {
val premiumGuilds = fdb.premiumGuilds
val feeds = all // All feeds
val premiumGuilds = fdb.premiumGuilds.getOrElse(Set())
val feeds = all
.filter(feed =>
feed.info.exists( // Filter to only the ones that match this filter
info => premiumGuilds.contains(info.guild.toLong) // Only guilds that are in the "premium guild" set
Expand Down
52 changes: 29 additions & 23 deletions src/main/scala/it/xaan/rss/Webhook.scala
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
package it.xaan.rss

import club.minnced.discord.webhook.WebhookClient
import club.minnced.discord.webhook.send.{WebhookEmbed, WebhookEmbedBuilder, WebhookMessageBuilder}
import it.xaan.rss.data.RssFeed
import it.xaan.rss.parsing.ParsedFeed
import java.util.regex.Pattern

import scala.util.{Failure, Try}
import it.xaan.rss.data.{Config, EmbedFooter, RssFeed, WebhookEmbed, WebhookMessage}
import it.xaan.rss.parsing.ParsedFeed
import scalaj.http.Http

object Webhook {
def send(feed: RssFeed, parsed: ParsedFeed): Unit = {
val pattern = Pattern.compile("(?:https?://)?(?:\\w+\\.)?discordapp\\.com/api(?:/v\\d+)?/webhooks/(\\d+)/([\\w-]+)(?:/(?:\\w+)?)?")

def send(feed: RssFeed, parsed: ParsedFeed, config: Config): Unit = {
feed.info.foreach(guild => {
parsed.stories.foreach(story => {
if (!guild.excludedTags.exists(story.category.equals) && (guild.includedTags.isEmpty || guild.includedTags.exists(story.category.equals))) {
if (!guild.disallowed.exists(story.category.equals) && (guild.allowed.isEmpty || guild.allowed.exists(story.category.equals))) {
if (guild.lastUpdated < story.updated) {
val client = WebhookClient.withUrl(guild.webhook)
val message = new WebhookMessageBuilder()
.setAvatarUrl("https://cdn.discordapp.com/attachments/518914613010497538/613217485264781312/8ca21ef535d1f1ce25c4e8f8446ccbff.jpg")
.setUsername("Tatsumaki RSS")
.append(s"<${story.url}>")
.addEmbeds(
new WebhookEmbedBuilder()
.setTitle(new WebhookEmbed.EmbedTitle(trail(story.title, 240), null))
.setDescription(trail(story.description, 40))
.setColor(0x249999)
.setFooter(new WebhookEmbed.EmbedFooter(s"RSS Feed: ${feed.url}", null))
.build()
val matched = pattern.matcher(guild.webhook)
matched.matches()
val id = matched.group(1)
val token = matched.group(2)
val message = WebhookMessage(
avatar_url = "https://cdn.discordapp.com/attachments/518914613010497538/613217485264781312/8ca21ef535d1f1ce25c4e8f8446ccbff.jpg",
content = s"<${story.url}>",
username = "Tatsumaki RSS",
embeds = Seq(
WebhookEmbed(
title = trail(story.title, 240),
description = trail(story.description, 40),
color = 0x249999,
footer = EmbedFooter(text = "RSS Feed: ${feed.url}")
)
)
Try(client.send(message.build()).get()) match {
case Failure(exception) => println(s"Couldn't send message. ${exception.getLocalizedMessage}") // TODO: LOGGING
case _ =>
}
).toJson

Http(s"https://${config.webhookProxy}$id/$token")
.postData(message)
.headers(("Content-Type", "application/json"))
.asString
}
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/it/xaan/rss/Worker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Worker(val config: Config, val fdb: Foundation) {
parsed match {
case Failure(exception) => exception.printStackTrace() // TODO: LOGGING
case Success(value) =>
Webhook.send(feed, value)
Webhook.send(feed, value, config)
feed.info.foreach(info => fdb.updateChecked(feed.url, info.guild))
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/it/xaan/rss/data/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ case class Config(
maxThreads: Int = -1,
apiPort: Int = 8080,
fdbCluster: String = "/etc/foundationdb/fdb.cluster",
redditUser: String = ""
redditUser: String = "",
webhookProxy: String = ""
)
12 changes: 6 additions & 6 deletions src/main/scala/it/xaan/rss/data/RssFeed.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ object RssFeed {
* @param channel The channel that subscribed to this RSS URL
* @param guild The guild that subscribed to this RSS URL
* @param webhook The URl of the webhook to post to.
* @param includedTags The tags to whitelist.
* @param excludedTags The tags to blacklist.
* @param allowed The tags to whitelist.
* @param disallowed The tags to blacklist.
*/
case class Info(
channel: String,
guild: String,
webhook: String = "",
lastUpdated: Long = 0,
includedTags: Set[String] = Set(),
excludedTags: Set[String] = Set()
allowed: Set[String] = Set(),
disallowed: Set[String] = Set()
) {

override def toString: String =
Expand All @@ -52,8 +52,8 @@ case class Info(
| guild=$guild,
| webhook=$webhook,
| lastUpdated=$lastUpdated
| includedTags=$includedTags,
| excludedTags=$excludedTags
| includedTags=$allowed,
| excludedTags=$disallowed
|]
|""".stripMargin
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/scala/it/xaan/rss/data/WebhookMessage.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package it.xaan.rss.data

import play.api.libs.json.{Json, OFormat}

case class WebhookMessage(
avatar_url: String,
username: String,
content: String,
embeds: Seq[WebhookEmbed]
) {
implicit val ef: OFormat[EmbedFooter] = Json.format[EmbedFooter]
implicit val we: OFormat[WebhookEmbed] = Json.format[WebhookEmbed]
implicit val wm: OFormat[WebhookMessage] = Json.format[WebhookMessage]
def toJson: String = Json.toJson(this).toString()
}


case class WebhookEmbed(
title: String,
description: String,
color: Int,
footer: EmbedFooter
)

case class EmbedFooter(
text: String
)
13 changes: 13 additions & 0 deletions src/main/scala/it/xaan/rss/data/rest/DeleteFeed.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package it.xaan.rss.data.rest

import play.api.libs.json.{Json, OFormat}

case class DeleteFeed(
guild: String,
channel: String,
url: String
)

object DeleteFeed {
implicit val format: OFormat[DeleteFeed] = Json.format[DeleteFeed]
}
16 changes: 16 additions & 0 deletions src/main/scala/it/xaan/rss/data/rest/RestFeed.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package it.xaan.rss.data.rest

import play.api.libs.json.{Json, OFormat}

case class RestFeed(
guild: String,
channel: String,
url: String,
webhook: String,
allowed: Set[String],
disallowed: Set[String]
)

object RestFeed {
implicit val format: OFormat[RestFeed] = Json.format[RestFeed]
}
Loading

0 comments on commit 6e9efe1

Please sign in to comment.