Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RollingGoron/b292b2244b1351415f95bdea68099ae5 to your computer and use it in GitHub Desktop.
Save RollingGoron/b292b2244b1351415f95bdea68099ae5 to your computer and use it in GitHub Desktop.
Kotlin RSS Parse
@Component
class RSSFeedParser : ApplicationListener<ContextRefreshedEvent> {
@Autowired
val feedRepo : FeedRepository? = null
var didStart : Boolean = false
var timer = Timer("schedule", true);
fun fetchFeeds() {
Logging.logout("Starting feed parsing...")
this.feedRepo?.findAll()?.let { feeds ->
val input = SyndFeedInput()
for (feed in feeds) {
Logging.logout("Parsing feed for ${feed.podcastTitle}...")
val parsedFeed = input.build(XmlReader(URL(feed.feed)))
if (feed.latestEpisode != parsedFeed.entries[0].title) {
Logging.logout("New episode ${parsedFeed.entries[0].title} for ${parsedFeed.title} found.")
feed.latestEpisode = parsedFeed.entries[0].title
this.feedRepo?.save(feed)
//Send push notification to user of feed.
PushController.sendPushNotificationToUsersFor(feed)
}
}
Logging.logout("Feed parsing complete...waiting 10 min for next pass.")
}
}
fun startTimer() {
this.timer = Timer("schedule", true);
timer.scheduleAtFixedRate(0, 600000) {
fetchFeeds()
}
}
@EventListener
override fun onApplicationEvent(contextRefreshedEvent : ContextRefreshedEvent) {
if (didStart == false) {
startTimer()
didStart = true
Logging.logout("Spring started and RSS Parsing is running...${Date().time/1000}")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment