Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions app/src/main/kotlin/amarr/torznab/TorznabApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,44 @@ private suspend fun ApplicationCall.handleRequests(indexer: Indexer) {
xmlDeclMode = XmlDeclMode.Charset
xmlVersion = XmlVersion.XML10
} // This API uses XML instead of JSON

request.queryParameters["t"]?.let {
when (it) {
"caps" -> {
application.log.debug("Handling caps request")
respondText(xmlFormat.encodeToString(indexer.capabilities()), contentType = ContentType.Application.Xml)
}

"tvsearch" -> performSearch(indexer, xmlFormat)
"search" -> performSearch(indexer, xmlFormat)
"tvsearch" -> performSearch(indexer, xmlFormat, isTvSearch = true)

"movie" -> performSearch(indexer, xmlFormat, isTvSearch = false)

else -> throw IllegalArgumentException("Unknown action: $it")
}
} ?: throw IllegalArgumentException("Missing action")
}

private suspend fun ApplicationCall.performSearch(indexer: Indexer, xmlFormat: XML) {
private suspend fun ApplicationCall.performSearch(indexer: Indexer, xmlFormat: XML, isTvSearch: Boolean) {
val query = request.queryParameters["q"].orEmpty()
val finalQuery = if (isTvSearch) {
// Handle season and episode
val season = request.queryParameters["season"].orEmpty()
val episode = request.queryParameters["episode"].orEmpty().padStart(2, '0')
"$query ${season}x$episode"
} else {
// Only use the query for movies
query
}

val offset = request.queryParameters["offset"]?.toIntOrNull() ?: 0
val limit = request.queryParameters["limit"]?.toIntOrNull() ?: 100
val cat = request.queryParameters["cat"]?.split(",")?.map { cat -> cat.toInt() } ?: emptyList()
application.log.debug("Handling search request: {}, {}, {}, {}", query, offset, limit, cat)

application.log.debug("Handling search request: {}, {}, {}, {}", finalQuery, offset, limit, cat)

try {
respondText(
xmlFormat.encodeToString(indexer.search(query, offset, limit, cat)),
xmlFormat.encodeToString(indexer.search(finalQuery, offset, limit, cat)),
contentType = ContentType.Application.Xml
)
} catch (e: ThrottledException) {
Expand All @@ -69,4 +83,4 @@ private suspend fun ApplicationCall.performSearch(indexer: Indexer, xmlFormat: X
application.log.warn("Unauthorized, returning 401")
respondText("Unauthorized, check your credentials.", status = HttpStatusCode.Unauthorized)
}
}
}