|
| 1 | +package com.maxrave.simpmusic.service.rss |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.util.Xml |
| 5 | +import androidx.work.CoroutineWorker |
| 6 | +import androidx.work.WorkerParameters |
| 7 | +import com.maxrave.domain.data.entities.NotificationEntity |
| 8 | +import com.maxrave.domain.extension.epochMillisToLocalDateTime |
| 9 | +import com.maxrave.domain.extension.now |
| 10 | +import com.maxrave.domain.repository.CommonRepository |
| 11 | +import com.maxrave.logger.Logger |
| 12 | +import com.maxrave.simpmusic.service.test.notification.NotificationHandler |
| 13 | +import kotlinx.coroutines.Dispatchers |
| 14 | +import kotlinx.coroutines.withContext |
| 15 | +import org.koin.core.component.KoinComponent |
| 16 | +import org.koin.core.component.inject |
| 17 | +import org.xmlpull.v1.XmlPullParser |
| 18 | +import java.net.HttpURLConnection |
| 19 | +import java.net.URL |
| 20 | +import java.text.SimpleDateFormat |
| 21 | +import java.util.Locale |
| 22 | + |
| 23 | +/** |
| 24 | + * Periodically scans the personal blog RSS feed and pushes a local notification for each |
| 25 | + * post that is (a) published within the [WINDOW_MS] window relative to the run time AND |
| 26 | + * (b) not already stored in the notification DB. |
| 27 | + * |
| 28 | + * The DB is the source of truth for "already pushed" — see [CommonRepository.isNotificationExists]. |
| 29 | + * The time window is intentionally wider than the scheduling interval so a delayed WorkManager |
| 30 | + * run (Doze, constraints) does not skip a post; the DB check still guarantees one push per post. |
| 31 | + */ |
| 32 | +class RssFeedNotifyWork( |
| 33 | + context: Context, |
| 34 | + params: WorkerParameters, |
| 35 | +) : CoroutineWorker(context, params), |
| 36 | + KoinComponent { |
| 37 | + private val commonRepository: CommonRepository by inject() |
| 38 | + |
| 39 | + override suspend fun doWork(): Result = |
| 40 | + withContext(Dispatchers.IO) { |
| 41 | + try { |
| 42 | + Logger.w(TAG, "doWork: fetching $FEED_URL") |
| 43 | + val items = parseRss(fetchFeed(FEED_URL)) |
| 44 | + val nowMillis = System.currentTimeMillis() |
| 45 | + |
| 46 | + // Oldest first so notifications arrive in chronological order. |
| 47 | + items.sortedBy { it.pubMillis }.forEach { item -> |
| 48 | + val withinWindow = item.pubMillis > 0L && (nowMillis - item.pubMillis) <= WINDOW_MS |
| 49 | + if (withinWindow && !commonRepository.isNotificationExists(item.link)) { |
| 50 | + NotificationHandler.createBlogNotificationChannel(applicationContext) |
| 51 | + NotificationHandler.createBlogNotification( |
| 52 | + context = applicationContext, |
| 53 | + title = item.title, |
| 54 | + text = item.description, |
| 55 | + url = item.link, |
| 56 | + ) |
| 57 | + commonRepository.insertNotification( |
| 58 | + NotificationEntity( |
| 59 | + channelId = "", |
| 60 | + name = item.title, |
| 61 | + type = NotificationEntity.TYPE_BLOG, |
| 62 | + link = item.link, |
| 63 | + description = item.description, |
| 64 | + time = if (item.pubMillis > 0L) epochMillisToLocalDateTime(item.pubMillis) else now(), |
| 65 | + ), |
| 66 | + ) |
| 67 | + Logger.w(TAG, "Pushed blog notification: ${item.title}") |
| 68 | + } |
| 69 | + } |
| 70 | + Result.success() |
| 71 | + } catch (e: Exception) { |
| 72 | + Logger.e(TAG, "doWork failed: ${e.message}") |
| 73 | + Result.retry() |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private fun fetchFeed(urlStr: String): String { |
| 78 | + val connection = |
| 79 | + (URL(urlStr).openConnection() as HttpURLConnection).apply { |
| 80 | + connectTimeout = 15_000 |
| 81 | + readTimeout = 15_000 |
| 82 | + requestMethod = "GET" |
| 83 | + setRequestProperty("User-Agent", "SimpMusic") |
| 84 | + } |
| 85 | + return try { |
| 86 | + connection.inputStream.bufferedReader().use { it.readText() } |
| 87 | + } finally { |
| 88 | + connection.disconnect() |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private fun parseRss(xml: String): List<RssItem> { |
| 93 | + val parser = Xml.newPullParser() |
| 94 | + parser.setInput(xml.reader()) |
| 95 | + val items = mutableListOf<RssItem>() |
| 96 | + var insideItem = false |
| 97 | + var title = "" |
| 98 | + var link = "" |
| 99 | + var description = "" |
| 100 | + var pubDate = "" |
| 101 | + var event = parser.eventType |
| 102 | + while (event != XmlPullParser.END_DOCUMENT) { |
| 103 | + when (event) { |
| 104 | + XmlPullParser.START_TAG -> |
| 105 | + when (parser.name) { |
| 106 | + "item" -> { |
| 107 | + insideItem = true |
| 108 | + title = "" |
| 109 | + link = "" |
| 110 | + description = "" |
| 111 | + pubDate = "" |
| 112 | + } |
| 113 | + "title" -> if (insideItem) title = parser.nextText().trim() |
| 114 | + "link" -> if (insideItem) link = parser.nextText().trim() |
| 115 | + "description" -> if (insideItem) description = parser.nextText().trim() |
| 116 | + "pubDate" -> if (insideItem) pubDate = parser.nextText().trim() |
| 117 | + } |
| 118 | + |
| 119 | + XmlPullParser.END_TAG -> |
| 120 | + if (parser.name == "item") { |
| 121 | + insideItem = false |
| 122 | + if (link.isNotEmpty()) { |
| 123 | + items.add(RssItem(title, link, description, parsePubMillis(pubDate))) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + event = parser.next() |
| 128 | + } |
| 129 | + return items |
| 130 | + } |
| 131 | + |
| 132 | + private fun parsePubMillis(pubDate: String): Long = |
| 133 | + try { |
| 134 | + // RFC-822, e.g. "Fri, 26 Jun 2026 19:05:06 GMT" |
| 135 | + SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH).parse(pubDate)?.time ?: 0L |
| 136 | + } catch (e: Exception) { |
| 137 | + 0L |
| 138 | + } |
| 139 | + |
| 140 | + private data class RssItem( |
| 141 | + val title: String, |
| 142 | + val link: String, |
| 143 | + val description: String, |
| 144 | + val pubMillis: Long, |
| 145 | + ) |
| 146 | + |
| 147 | + companion object { |
| 148 | + private const val TAG = "RssFeedNotifyWork" |
| 149 | + const val FEED_URL = "https://www.maxrave.dev/rss.xml" |
| 150 | + |
| 151 | + // 48h — wider than the 24h schedule so a delayed run still catches recent posts; |
| 152 | + // the DB dedup prevents any double push. |
| 153 | + private const val WINDOW_MS = 48L * 60L * 60L * 1000L |
| 154 | + } |
| 155 | +} |
0 commit comments