Skip to content

Commit

Permalink
Merge pull request #1 from windymelt/fix-updatedat
Browse files Browse the repository at this point in the history
Fix updatedAt
  • Loading branch information
windymelt committed Sep 13, 2023
2 parents 774b7f6 + 5818b2b commit c28d0fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The function requires two environment variables:

- `RSS_URL` for feed URL e.g. `https://scala.epfl.ch/feed`
- `WEBHOOK_URL` for Discord webhook URL e.g. `"https://discord.com/api/webhooks/1234567890987654321`
- `TZ_OFFSET`(optional) for your timezone in hours (defaults to system tz)
- this value is used for debugging

Handler should be `io.github.windymelt.rss2discord.Rss2Discord::handler`.

Expand Down
30 changes: 23 additions & 7 deletions src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,25 @@ import scala.concurrent.duration.FiniteDuration

val feedUrlEnv: Option[String] = sys.env.get("RSS_URL")
val webhookUrl: String = sys.env("WEBHOOK_URL")
val tzOffset: Int = sys.env
.get("TZ_OFFSET")
.map(_.toInt)
.getOrElse(
FiniteDuration(
DateTime.now().zone.getOffset(DateTime.now()),
"milliseconds"
).toHours.toInt
)

extension (e: SyndEntry)
def publishedDateTime: DateTime = e
.getPublishedDate()
.toLocalDateTime
.toDateTime(DateTimeZone.forOffsetHours(9))
def publishedDateTime: Option[DateTime] = Option(e.getPublishedDate).map {
_.toLocalDateTime
.toDateTime(DateTimeZone.forOffsetHours(tzOffset))
}
def updatedDateTime: Option[DateTime] = Option(e.getUpdatedDate).map {
_.toLocalDateTime
.toDateTime(DateTimeZone.forOffsetHours(tzOffset))
}

object Rss2Discord extends IOApp.Simple {
def handler(in: InputStream, out: OutputStream, ctx: Context): Unit = main(
Expand All @@ -41,9 +54,12 @@ object Rss2Discord extends IOApp.Simple {
val entriesToPost = feed
.getEntries()
.asScala
.filter(
_.publishedDateTime.isAfter(now.minusMinutes(30))
)
.filter {
case e if (e.publishedDateTime orElse e.updatedDateTime).isDefined =>
val dt = (e.publishedDateTime orElse e.updatedDateTime).get
dt.isAfter(now.minusMinutes(30))
case _ => false
}
entriesToPost.map(e => post(formatEntry(e))).toSeq.sequence.as(())
}

Expand Down

0 comments on commit c28d0fb

Please sign in to comment.