1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-10 04:52:38 +01:00

Merge new auth flyweight and update hd image fetcher

This commit is contained in:
Allan Wang 2018-12-26 18:35:01 -05:00
parent 3a3096be58
commit bddb58f035
No known key found for this signature in database
GPG Key ID: C93E3F9C679D7A56
3 changed files with 27 additions and 42 deletions

View File

@ -24,38 +24,25 @@ import com.pitchedapps.frost.facebook.FB_URL_BASE
import com.pitchedapps.frost.facebook.FB_USER_MATCHER
import com.pitchedapps.frost.facebook.USER_AGENT_BASIC
import com.pitchedapps.frost.facebook.get
import com.pitchedapps.frost.rx.RxFlyweight
import com.pitchedapps.frost.rx.Flyweight
import com.pitchedapps.frost.utils.L
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch
import kotlinx.coroutines.selects.select
import kotlinx.coroutines.GlobalScope
import okhttp3.Call
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.logging.HttpLoggingInterceptor
import org.apache.commons.text.StringEscapeUtils
import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine
/**
* Created by Allan Wang on 21/12/17.
*/
private class RxAuth : RxFlyweight<String, Long, RequestAuth>() {
override fun call(input: String) = input.getAuth()
override fun validate(input: String, cond: Long) =
System.currentTimeMillis() - cond < 3600000 // valid for an hour
override fun cache(input: String) = System.currentTimeMillis()
val fbAuth = Flyweight<String, RequestAuth>(GlobalScope, 100,3600000 /* an hour */) {
it.getAuth()
}
private val auth = RxAuth()
/**
* Synchronously fetch [RequestAuth] from cookie
* [action] will only be called if a valid auth is found.
@ -64,7 +51,7 @@ private val auth = RxAuth()
fun String?.fbRequest(fail: () -> Unit = {}, action: RequestAuth.() -> Unit) {
if (this == null) return fail()
try {
val auth = auth(this).blockingGet()
val auth = fbAuth(this).blockingGet()
auth.action()
} catch (e: Exception) {
L.e { "Failed auth for ${hashCode()}: ${e.message}" }
@ -72,8 +59,6 @@ fun String?.fbRequest(fail: () -> Unit = {}, action: RequestAuth.() -> Unit) {
}
}
data class FbRequest(val cookie: String, val request: suspend (RequestAuth) -> Unit)
/**
* Underlying container for all fb requests
*/

View File

@ -33,8 +33,9 @@ import com.pitchedapps.frost.facebook.FB_URL_BASE
import com.pitchedapps.frost.facebook.formattedFbUrl
import com.pitchedapps.frost.facebook.get
import io.reactivex.Maybe
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import okhttp3.Call
import okhttp3.Request
import java.io.IOException
import java.io.InputStream
@ -123,21 +124,22 @@ class HdImageFetcher(private val model: HdImageMaybe) : DataFetcher<InputStream>
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
if (!model.isValid) return callback.fail("Model is invalid")
model.cookie.fbRequest(fail = { callback.fail("Invalid auth") }) {
if (cancelled) return@fbRequest callback.fail("Cancelled")
val url = getFullSizedImage(model.id).invoke()
?: return@fbRequest callback.fail("Null url")
if (cancelled) return@fbRequest callback.fail("Cancelled")
if (!url.contains("png") && !url.contains("jpg")) return@fbRequest callback.fail("Invalid format")
urlCall = Request.Builder().url(url).get().call()
inputStream = try {
urlCall?.execute()?.body()?.byteStream()
} catch (e: IOException) {
null
val result: Result<InputStream?> = runCatching {
runBlocking {
withTimeout(20000L) {
val auth = fbAuth.fetch(model.cookie)
if (cancelled) throw RuntimeException("Cancelled")
val url = auth.getFullSizedImage(model.id).invoke() ?: throw RuntimeException("Null url")
if (cancelled) throw RuntimeException("Cancelled")
if (!url.contains("png") && !url.contains("jpg")) throw RuntimeException("Invalid format")
urlCall?.execute()?.body()?.byteStream()
}
}
callback.onDataReady(inputStream)
}
if (result.isSuccess)
callback.onDataReady(result.getOrNull())
else
callback.onLoadFailed(result.exceptionOrNull() as? Exception ?: RuntimeException("Failed"))
}
override fun cleanup() {

View File

@ -25,7 +25,7 @@ import android.content.Intent
import android.os.BaseBundle
import android.os.PersistableBundle
import com.pitchedapps.frost.facebook.requests.RequestAuth
import com.pitchedapps.frost.facebook.requests.fbRequest
import com.pitchedapps.frost.facebook.requests.fbAuth
import com.pitchedapps.frost.facebook.requests.markNotificationRead
import com.pitchedapps.frost.utils.EnumBundle
import com.pitchedapps.frost.utils.EnumBundleCompanion
@ -179,15 +179,13 @@ class FrostRequestService : BaseJobService() {
}
launch(Dispatchers.IO) {
try {
var failed = true
cookie.fbRequest {
L.d { "Requesting frost service for ${command.name}" }
command.invoke(this, bundle)
failed = false
}
val auth = fbAuth.fetch(cookie)
command.invoke(auth, bundle)
L.d {
"${if (failed) "Failed" else "Finished"} frost service for ${command.name} in ${System.currentTimeMillis() - startTime} ms"
"Finished frost service for ${command.name} in ${System.currentTimeMillis() - startTime} ms"
}
} catch (e: Exception) {
L.e(e) { "Failed frost service for ${command.name} in ${System.currentTimeMillis() - startTime} ms" }
} finally {
jobFinished(params, false)
}