-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
66 changed files
with
2,391 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
vripper-core/src/main/kotlin/me/vripper/parser/PostLookupAPIParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package me.vripper.parser | ||
|
||
import me.vripper.entities.LogEntry | ||
import me.vripper.exception.DownloadException | ||
import me.vripper.exception.PostParseException | ||
import me.vripper.model.PostItem | ||
import me.vripper.services.* | ||
import me.vripper.utilities.Tasks | ||
import me.vripper.utilities.formatToString | ||
import net.jodah.failsafe.Failsafe | ||
import net.jodah.failsafe.function.CheckedSupplier | ||
import org.apache.hc.client5.http.classic.HttpClient | ||
import org.apache.hc.client5.http.classic.methods.HttpGet | ||
import org.apache.hc.core5.net.URIBuilder | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import javax.xml.parsers.SAXParserFactory | ||
|
||
class PostLookupAPIParser(private val threadId: Long, private val postId: Long) : KoinComponent { | ||
private val log by me.vripper.delegate.LoggerDelegate() | ||
private val settingsService: SettingsService by inject() | ||
private val retryPolicyService: RetryPolicyService by inject() | ||
private val httpService: HTTPService by inject() | ||
private val vgAuthService: VGAuthService by inject() | ||
private val dataTransaction: DataTransaction by inject() | ||
|
||
@Throws(PostParseException::class) | ||
fun parse(): PostItem { | ||
log.debug("Parsing post $postId") | ||
val httpGet = | ||
HttpGet(URIBuilder("${settingsService.settings.viperSettings.host}/vr.php").also { | ||
it.setParameter( | ||
"p", postId.toString() | ||
) | ||
}.build()) | ||
val threadLookupAPIResponseHandler = ThreadLookupAPIResponseHandler() | ||
log.debug("Requesting {}", httpGet) | ||
Tasks.increment() | ||
return try { | ||
Failsafe.with(retryPolicyService.buildGenericRetryPolicy<Any>()).onFailure { | ||
log.error( | ||
"parsing failed for thread $threadId, post $postId", it.failure | ||
) | ||
dataTransaction.saveLog( | ||
LogEntry( | ||
type = LogEntry.Type.POST, status = LogEntry.Status.ERROR, message = """ | ||
Failed to process thread $threadId, post $postId | ||
${it.failure.formatToString()} | ||
""".trimIndent() | ||
) | ||
) | ||
}.get(CheckedSupplier { | ||
val connection: HttpClient = httpService.client | ||
connection.execute( | ||
httpGet, vgAuthService.context | ||
) { response -> | ||
if (response.code / 100 != 2) { | ||
throw DownloadException("Unexpected response code '${response.code}' for $httpGet") | ||
} | ||
factory.newSAXParser() | ||
.parse(response.entity.content, threadLookupAPIResponseHandler) | ||
threadLookupAPIResponseHandler.result.postItemList.first() | ||
} | ||
}) | ||
} catch (e: Exception) { | ||
throw PostParseException(e) | ||
} finally { | ||
Tasks.decrement() | ||
} | ||
} | ||
|
||
companion object { | ||
private val factory = SAXParserFactory.newInstance() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.