From 25ea96551d5d2ceebfad062255afddbabc1bf863 Mon Sep 17 00:00:00 2001 From: Prem Nirmal Date: Thu, 20 Jul 2023 12:24:42 +0100 Subject: [PATCH] User agent for yahoo --- .../premnirmal/ticker/model/StocksProvider.kt | 14 ++------------ .../premnirmal/ticker/network/NetworkModule.kt | 10 ++++++++-- .../premnirmal/ticker/network/StocksApi.kt | 17 ++++++++--------- app/version.properties | 4 ++-- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/model/StocksProvider.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/model/StocksProvider.kt index 695a20c5..e8dbc011 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/model/StocksProvider.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/model/StocksProvider.kt @@ -40,8 +40,7 @@ class StocksProvider @Inject constructor( private val widgetDataProvider: WidgetDataProvider, private val alarmScheduler: AlarmScheduler, private val storage: StocksStorage, - private val coroutineScope: CoroutineScope, - private val exponentialBackoff: ExponentialBackoff + private val coroutineScope: CoroutineScope ) { companion object { @@ -190,24 +189,15 @@ class StocksProvider @Inject constructor( lastFetched = clock.currentTimeMillis() _fetchState.emit(FetchState.Success(lastFetched)) saveLastFetched() - exponentialBackoff.reset() scheduleUpdate() } FetchResult.success(quoteMap.values.filter { tickerSet.contains(it.symbol) }.toList()) } } catch(ex: CancellationException) { - if (allowScheduling) { - val backOffTimeMs = exponentialBackoff.getBackoffDurationMs() - scheduleUpdateWithMs(backOffTimeMs) - } FetchResult.failure>(FetchException("Failed to fetch", ex)) } catch (ex: Throwable) { Timber.w(ex) - if (allowScheduling) { - _fetchState.emit(FetchState.Failure(ex)) - val backOffTimeMs = exponentialBackoff.getBackoffDurationMs() - scheduleUpdateWithMs(backOffTimeMs) - } + _fetchState.emit(FetchState.Failure(ex)) FetchResult.failure>(FetchException("Failed to fetch", ex)) } finally { appPreferences.setRefreshing(false) diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/network/NetworkModule.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/network/NetworkModule.kt index 798f2107..0f96e87f 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/network/NetworkModule.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/network/NetworkModule.kt @@ -50,7 +50,6 @@ class NetworkModule { @Named("yahoo") @Provides @Singleton internal fun provideHttpClientForYahoo( - userAgentInterceptor: UserAgentInterceptor, crumbInterceptor: CrumbInterceptor, cookieJar: YahooFinanceCookies ): OkHttpClient { @@ -59,7 +58,14 @@ class NetworkModule { if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE val okHttpClient = OkHttpClient.Builder() - .addInterceptor(userAgentInterceptor) + .addInterceptor { chain -> + val newRequest = chain.request() + .newBuilder() + .removeHeader("User-Agent") + .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36") + .build() + chain.proceed(newRequest) + } .addInterceptor(logger) .addInterceptor(crumbInterceptor) .cookieJar(cookieJar) diff --git a/app/src/main/kotlin/com/github/premnirmal/ticker/network/StocksApi.kt b/app/src/main/kotlin/com/github/premnirmal/ticker/network/StocksApi.kt index 1a3b30b5..988363f2 100644 --- a/app/src/main/kotlin/com/github/premnirmal/ticker/network/StocksApi.kt +++ b/app/src/main/kotlin/com/github/premnirmal/ticker/network/StocksApi.kt @@ -32,7 +32,8 @@ class StocksApi @Inject constructor( Regex("csrfToken\" value=\"(.+)\">") } - private suspend fun loadCrumb() { withContext(Dispatchers.IO) { + private suspend fun loadCrumb() { + withContext(Dispatchers.IO) { try { val initialLoad = yahooFinanceInitialLoad.initialLoad() val html = initialLoad.body() ?: "" @@ -67,7 +68,7 @@ class StocksApi @Inject constructor( Timber.e("Failed to get crumb with code: ${crumbResponse.code()}") } } catch (e: Exception) { - Timber.e(e, "Initial load failed") + Timber.e(e, "Crumb load failed") } } } @@ -129,11 +130,8 @@ class StocksApi @Inject constructor( private suspend fun getStocksYahoo( tickerList: List, invocationCount: Int = 1 - ): List = withContext(Dispatchers.IO) { - val crumb = appPreferences.getCrumb() - if (crumb.isNullOrEmpty()) { - loadCrumb() - } + ): List = + withContext(Dispatchers.IO) { val query = tickerList.joinToString(",") var quotesResponse: retrofit2.Response? = null try { @@ -142,13 +140,14 @@ class StocksApi @Inject constructor( appPreferences.setCrumb(null) loadCrumb() if (invocationCount == 1) { - return@withContext getStocksYahoo(tickerList, invocationCount + 1) + return@withContext getStocksYahoo(tickerList, invocationCount = invocationCount + 1) } } } catch (ex: Exception) { Timber.e(ex) + throw ex } - val quoteNets = quotesResponse?.body()?.quoteResponse?.result ?: emptyList() + val quoteNets = quotesResponse.body()?.quoteResponse?.result ?: emptyList() quoteNets } diff --git a/app/version.properties b/app/version.properties index 6f4911f2..35a76cc5 100644 --- a/app/version.properties +++ b/app/version.properties @@ -1,3 +1,3 @@ # this file is purely for f-droid because it cannot infer the version name/code from the git tag -versionName=3.9.826 -versionCode=300900826 +versionName=3.9.827 +versionCode=300900827