Added rss feed proxy

This commit is contained in:
Mikunj 2020-01-31 10:26:10 +11:00
parent d7f1b86615
commit 76c2fdf9d2

View File

@ -4,8 +4,9 @@ import android.content.Context
import android.os.Handler import android.os.Handler
import android.text.Html import android.text.Html
import android.util.Log import android.util.Log
import com.prof.rssparser.Parser import com.prof.rssparser.engine.XMLParser
import kotlinx.coroutines.* import kotlinx.coroutines.Job
import kotlinx.coroutines.Runnable
import org.thoughtcrime.securesms.jobs.PushDecryptJob import org.thoughtcrime.securesms.jobs.PushDecryptJob
import org.whispersystems.libsignal.util.guava.Optional import org.whispersystems.libsignal.util.guava.Optional
import org.whispersystems.signalservice.api.messages.SignalServiceContent import org.whispersystems.signalservice.api.messages.SignalServiceContent
@ -13,6 +14,8 @@ import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage
import org.whispersystems.signalservice.api.messages.SignalServiceGroup import org.whispersystems.signalservice.api.messages.SignalServiceGroup
import org.whispersystems.signalservice.api.push.SignalServiceAddress import org.whispersystems.signalservice.api.push.SignalServiceAddress
import org.whispersystems.signalservice.loki.api.LokiRSSFeed import org.whispersystems.signalservice.loki.api.LokiRSSFeed
import org.whispersystems.signalservice.loki.api.LokiRSSProxy
import org.whispersystems.signalservice.loki.utilities.successBackground
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.regex.Pattern import java.util.regex.Pattern
@ -22,7 +25,6 @@ class LokiRSSFeedPoller(private val context: Context, private val feed: LokiRSSF
private var hasStarted = false private var hasStarted = false
private val task = object : Runnable { private val task = object : Runnable {
override fun run() { override fun run() {
poll() poll()
handler.postDelayed(this, interval) handler.postDelayed(this, interval)
@ -46,32 +48,28 @@ class LokiRSSFeedPoller(private val context: Context, private val feed: LokiRSSF
} }
private fun poll() { private fun poll() {
CoroutineScope(Dispatchers.Main).launch { LokiRSSProxy.fetch(feed.url).successBackground { xml ->
try { val items = XMLParser(xml).call()
val url = feed.url items.reversed().forEach { item ->
val parser = Parser() val title = item.title ?: return@forEach
val items = parser.getArticles(url) val description = item.description ?: return@forEach
items.reversed().forEach { item -> val dateAsString = item.pubDate ?: return@forEach
val title = item.title ?: return@forEach val formatter = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z") // e.g. Tue, 27 Aug 2019 03:52:05 +0000
val description = item.description ?: return@forEach val date = formatter.parse(dateAsString)
val dateAsString = item.pubDate ?: return@forEach val timestamp = date.time
val formatter = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z") // e.g. Tue, 27 Aug 2019 03:52:05 +0000 var bodyAsHTML = "$title<br>$description"
val date = formatter.parse(dateAsString) val urlRegex = Pattern.compile("<a\\s+(?:[^>]*?\\s+)?href=\"([^\"]*)\".*?>(.*?)<.*?\\/a>")
val timestamp = date.time val matcher = urlRegex.matcher(bodyAsHTML)
var bodyAsHTML = "$title<br>$description" bodyAsHTML = matcher.replaceAll("$2 ($1)")
val urlRegex = Pattern.compile("<a\\s+(?:[^>]*?\\s+)?href=\"([^\"]*)\".*?>(.*?)<.*?\\/a>") val body = Html.fromHtml(bodyAsHTML).toString().trim()
val matcher = urlRegex.matcher(bodyAsHTML) val id = feed.id.toByteArray()
bodyAsHTML = matcher.replaceAll("$2 ($1)") val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null)
val body = Html.fromHtml(bodyAsHTML).toString().trim() val x2 = SignalServiceDataMessage(timestamp, x1, null, body)
val id = feed.id.toByteArray() val x3 = SignalServiceContent(x2, "Loki", SignalServiceAddress.DEFAULT_DEVICE_ID, timestamp, false)
val x1 = SignalServiceGroup(SignalServiceGroup.Type.UPDATE, id, null, null, null) PushDecryptJob(context).handleTextMessage(x3, x2, Optional.absent(), Optional.absent())
val x2 = SignalServiceDataMessage(timestamp, x1, null, body)
val x3 = SignalServiceContent(x2, "Loki", SignalServiceAddress.DEFAULT_DEVICE_ID, timestamp, false)
PushDecryptJob(context).handleTextMessage(x3, x2, Optional.absent(), Optional.absent())
}
} catch (exception: Exception) {
Log.d("Loki", "Couldn't update RSS feed with ID: $feed.id.")
} }
}.fail { exception ->
Log.d("Loki", "Couldn't update RSS feed with ID: $feed.id. $exception")
} }
} }
} }