Skip to content

Commit

Permalink
User agent for yahoo
Browse files Browse the repository at this point in the history
  • Loading branch information
premnirmal committed Jul 20, 2023
1 parent 45d3b07 commit 25ea965
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<List<Quote>>(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<List<Quote>>(FetchException("Failed to fetch", ex))
} finally {
appPreferences.setRefreshing(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class NetworkModule {

@Named("yahoo")
@Provides @Singleton internal fun provideHttpClientForYahoo(
userAgentInterceptor: UserAgentInterceptor,
crumbInterceptor: CrumbInterceptor,
cookieJar: YahooFinanceCookies
): OkHttpClient {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() ?: ""
Expand Down Expand Up @@ -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")
}
}
}
Expand Down Expand Up @@ -129,11 +130,8 @@ class StocksApi @Inject constructor(
private suspend fun getStocksYahoo(
tickerList: List<String>,
invocationCount: Int = 1
): List<YahooQuoteNet> = withContext(Dispatchers.IO) {
val crumb = appPreferences.getCrumb()
if (crumb.isNullOrEmpty()) {
loadCrumb()
}
): List<YahooQuoteNet> =
withContext(Dispatchers.IO) {
val query = tickerList.joinToString(",")
var quotesResponse: retrofit2.Response<YahooResponse>? = null
try {
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions app/version.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 25ea965

Please sign in to comment.