adding version update

This commit is contained in:
Jose Conde
2025-09-08 18:18:57 +02:00
parent 5f83d4aad4
commit aaa57ad8f8
10 changed files with 58 additions and 13 deletions

View File

@@ -1,16 +1,21 @@
package net.xintanalabs.rssotto.components.checkers.scrape
import kotlinx.coroutines.delay
import org.jsoup.Jsoup
import org.springframework.stereotype.Component
@Component("jsoup")
class JSoupFetcher: IScrapeFetcher {
override suspend fun fetch(url: String): String {
delay(1000) // 1-second delay to avoid rate limiting
return try {
Jsoup.connect(url)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
.header("Accept-Language", "en-US,en;q=0.5")
.header("Accept-Encoding", "gzip, deflate")
//.header("Connection", "keep-alive")
.header("Upgrade-Insecure-Requests", "1")
.get()
.html()
} catch (e: Exception) {

View File

@@ -32,7 +32,7 @@ class ScrapeChecker(
val response = fetcher.fetch(url)
val cleanedResponse = response.replace(">\\s+<".toRegex(), "><")
log.info("Response : {}", cleanedResponse)
val regex = paramsDict["regex"]
val regex: String = paramsDict["regex"] as String
log.info("Regex : {}", regex)
val match = Pattern.compile(regex).matcher(cleanedResponse)
if (!match.find() || match.groupCount() < 1) {

View File

@@ -15,9 +15,10 @@ data class App(
val fields: Map<String, String>,
val downloadUrl: String,
val currentVersion: String,
val latestVersion: String,
val latestVersion: String? = null,
val status: AppStatus,
val createdAt: Long = 0,
val updatedAt: Long = 0,
val lastCheckedAt: Long = 0
val lastCheckedAt: Long = 0,
val active: Boolean = true
)

View File

@@ -58,7 +58,7 @@ class AppService(private val mongoDBClient: MongoDBClient) {
)
return mongoDBClient.updateOne(
collection,
"uid",
"_id",
id,
updateFields,
App::class.java
@@ -68,4 +68,21 @@ class AppService(private val mongoDBClient: MongoDBClient) {
}
}
suspend fun updateLatestVersion(appId: String, latestVersion: String): Long {
try {
val updateFields = mapOf<String, String>(
"latestVersion" to latestVersion
)
return mongoDBClient.updateOne(
collection,
"_id",
appId,
updateFields,
App::class.java
)
} catch (e: Exception) {
throw RuntimeException("Failed to update app: ${e.message}", e)
}
}
}

View File

@@ -6,7 +6,7 @@ import net.xintanalabs.rssotto.model.CheckerType
import org.springframework.stereotype.Service
@Service
class CheckerTypeService (mongoDBClient: MongoDBClient) : ServiceBase<CheckerType>(mongoDBClient){
class CheckerTypeService (mongoDBClient: MongoDBClient) : ServiceMongoDBBase<CheckerType>(mongoDBClient){
override val collection: String = Constants.COLLECTION_CHECKER_TYPES
override val entityClass: Class<CheckerType> = CheckerType::class.java

View File

@@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class LatestVersionFinderService(private val checkerFactory: CheckerFactory) {
class LatestVersionFinderService(private val checkerFactory: CheckerFactory, private val versionService: VersionService) {
private val log = LoggerFactory.getLogger(LatestVersionFinderService::class.java)
suspend fun getLatestAppVersion(app: App, sources: List<Source>, checkerTypes: List<CheckerType>): String? {
@@ -19,7 +19,7 @@ class LatestVersionFinderService(private val checkerFactory: CheckerFactory) {
if (source != null) {
val checkerType: CheckerType? = checkerTypes.find { ct -> ct.id == source.checkerType }
if (checkerType != null) {
if (checkerType != null && app.id !=null) {
val checker: IVersionChecker = checkerFactory.createChecker(checkerType.name)
val paramsMap: Map<String, String> = mapOf<String, String>(
"url" to getUrl(app, source),
@@ -29,7 +29,7 @@ class LatestVersionFinderService(private val checkerFactory: CheckerFactory) {
appVersion = checker.getLatestVersion(paramsMap)
log.info("App (${app.name}, latest versión ${appVersion}")
versionService.setVersion(app.id, appVersion)
}
}
return appVersion
@@ -42,7 +42,7 @@ class LatestVersionFinderService(private val checkerFactory: CheckerFactory) {
): Map<String, String?> {
val versionsMap: MutableMap<String, String?> = mutableMapOf<String, String?>()
apps.map { app ->
if (app.id !== null) {
if (app.id !== null && app.active) {
val appLatestVersion: String? = getLatestAppVersion(app, sources, checkerTypes)
versionsMap[app.id] = appLatestVersion
}

View File

@@ -4,7 +4,7 @@ import kotlinx.coroutines.flow.toList
import net.xintanalabs.rssotto.db.mongodb.MongoDBClient
import org.springframework.beans.factory.annotation.Autowired
abstract class ServiceBase<T: Any> @Autowired constructor(protected val mongoDBClient: MongoDBClient) {
abstract class ServiceMongoDBBase<T: Any> @Autowired constructor(protected val mongoDBClient: MongoDBClient) {
protected abstract val collection: String
protected abstract val entityClass: Class<T>
protected val idField: String = "id"

View File

@@ -6,7 +6,7 @@ import net.xintanalabs.rssotto.model.Source
import org.springframework.stereotype.Service
@Service
class SourceService(mongoDBClient: MongoDBClient): ServiceBase<Source>(mongoDBClient) {
class SourceService(mongoDBClient: MongoDBClient): ServiceMongoDBBase<Source>(mongoDBClient) {
override val collection: String = Constants.COLLECTION_SOURCES
override val entityClass: Class<Source> = Source::class.java

View File

@@ -1,13 +1,12 @@
package net.xintanalabs.rssotto.services
import kotlinx.coroutines.flow.toList
import net.xintanalabs.rssotto.constants.Constants
import net.xintanalabs.rssotto.db.mongodb.MongoDBClient
import net.xintanalabs.rssotto.model.Type
import org.springframework.stereotype.Service
@Service
class TypeService(mongoDBClient: MongoDBClient) : ServiceBase<Type>(mongoDBClient) {
class TypeService(mongoDBClient: MongoDBClient) : ServiceMongoDBBase<Type>(mongoDBClient) {
override val collection: String = Constants.COLLECTION_TYPES
override val entityClass: Class<Type> = Type::class.java

View File

@@ -0,0 +1,23 @@
package net.xintanalabs.rssotto.services
import net.xintanalabs.rssotto.db.mongodb.MongoDBClient
import org.springframework.stereotype.Service
@Service
class VersionService(private val appService: AppService) {
private val latestVersionsMap: MutableMap<String, String> = mutableMapOf()
suspend fun getVersion(appId: String): String? {
var version = latestVersionsMap[appId]
if (version == null) {
version = appService.getAppById(appId)?.latestVersion
}
return version
}
suspend fun setVersion(appId: String, version: String) {
latestVersionsMap[appId] = version;
appService.updateLatestVersion(appId, version)
}
}