Skip to content

Commit

Permalink
v0.13.0 update.
Browse files Browse the repository at this point in the history
[新增]长按快进触动反馈。
[新增]本地登入功能添加提醒,只能保持2小时。
[新增]OkHttp添加10MB缓存。我也不知道有什么用。
[新增]readme增加开发者碎碎念。
  • Loading branch information
YenalyLiew committed Mar 14, 2024
1 parent 087dc88 commit 0d4a8c9
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 16 deletions.
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ GitHub 上的补充。目前是随着版本更新来更新共享关键H帧集,

你先等会,等该网页出现错误提示之后,会自动弹出一个登入框,你从那里登入就可以。

注意:该方法为备用方法,只能保持登入状态 2 个小时,所以还是建议尽可能使用网页登入。

- **为什么不支持 Google 登录?**

去问 Google 为什么不给开放吧,我目前是没什么好办法。
Expand All @@ -160,7 +162,12 @@ GitHub 上的补充。目前是随着版本更新来更新共享关键H帧集,

**[新增]** 重磅!应用内置 Hosts 功能,可以让你直连网站。

**[新增]** [@NeKoOuO](https://github.com/NeKoOuO) 提供的部分影片的关键H帧!开启共享帧集就可立即使用!

**[新增]** 本地登入功能。如果登入网页连接失败,等出现错误提示之后,会自动弹出一个登入框,可以从那里尝试登入。
注意:该方法为备用方法,只能保持登入状态 2 个小时,所以还是建议尽可能使用网页登入。

**[新增]** 长按快进有触动反馈了。

**[修复]** 切换简繁字幕无效的问题。

Expand Down Expand Up @@ -266,7 +273,7 @@ GitHub 上的补充。目前是随着版本更新来更新共享关键H帧集,

### v0.10.5

**[新增]** 终于有图标了,感谢 [rurires](https://github.com/rurires) 提供!
**[新增]** 终于有图标了,感谢 [@rurires](https://github.com/rurires) 提供!

**[修复]** 返回主页后总是展开头图。

Expand Down Expand Up @@ -393,5 +400,24 @@ bug 产生,所以建议下载完了再关闭。

------

~~这里应该还有东西,以后再写!~~
## 开发者碎碎念

目前该软件还是我一人维护,我肯定是希望有更多的人参与到这个项目中来。

最近也有点忙,我尽量保证软件能用,其他的有空就整。

我 Cookie 有一块逻辑写错了两年我都没发现,但没想到的是,写错了竟然并不影响正常运行。

### 目前遇到的问题

- 通过分析可知,登入后正确的返回 Cookie 应为三个,分别为
XSRF-TOKEN(校验token)、hanime1_session(代表登入)、remember_web_xxx(记住登入状态),
但是通过本地登入方法,只能获取到 XSRF-TOKEN 和 hanime1_session,而 remember_web_xxx 无法获取,导致登入状态无法保存。
而 hanime1_session 的有效期只有 2 个小时,所以本地登入只能保持 2 个小时的登入状态。
如果有人知道如何正确获取 remember_web_xxx,请告诉我,感激不尽!

- 关键H帧共享起来(指上传到 Github)确实对于一般人来说不太友好,到时候可能整个一键生成 JSON?
你只需要负责 Pull Request。

- CI 构建是个好东西,但我得学一下怎么用...

10 changes: 8 additions & 2 deletions app/src/main/java/com/yenaly/han1meviewer/HanimeManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ internal fun logout() {
CookieManager.getInstance().removeAllCookies(null)
}

internal fun login(cookie: CookieString) {
internal fun login(cookies: String) {
isAlreadyLogin = true
loginCookie = cookie
loginCookie = CookieString(cookies)
}

internal fun login(cookies: List<String>) {
login(cookies.joinToString(";") {
it.substringBefore(';')
})
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/yenaly/han1meviewer/logic/NetworkRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.yenaly.han1meviewer.logic.network.HanimeNetwork
import com.yenaly.han1meviewer.logic.state.PageLoadingState
import com.yenaly.han1meviewer.logic.state.VideoLoadingState
import com.yenaly.han1meviewer.logic.state.WebsiteState
import com.yenaly.han1meviewer.util.CookieString
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
Expand Down Expand Up @@ -308,17 +307,18 @@ object NetworkRepo {

fun login(email: String, password: String) = flow {
// 首先获取token
val loginPage = HanimeNetwork.hanimeService.loginPage()
val loginPage = HanimeNetwork.hanimeService.getLoginPage()
val token = loginPage.body()?.string()?.let(Parse::extractTokenFromLoginPage)

val req = HanimeNetwork.hanimeService.login(token, email, password)
if (req.isSuccessful) {
// 再次获取登录页面,如果失败则返回 cookie
// 因为登录成功再次访问 login 会 404,这是判断是否登录成功的方法
val loginPageAgain = HanimeNetwork.hanimeService.loginPage()
val loginPageAgain = HanimeNetwork.hanimeService.getLoginPage()
if (loginPageAgain.code() == 404) {
// Cookie 會返回 XSRF-TOKEN 和 hanime1_session,我們只需要後者
emit(WebsiteState.Success(CookieString(req.raw().headers["Set-Cookie"]!!)))
// 错误的,还需要 remember_web 字段!但我没找到!
Log.d("login_headers", req.headers().toMultimap().toString())
emit(WebsiteState.Success(req.headers().values("Set-Cookie")))
} else {
emit(WebsiteState.Error(IllegalStateException("賬戶或密碼可能錯誤")))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package com.yenaly.han1meviewer.logic.network

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import com.yenaly.han1meviewer.USER_AGENT
import com.yenaly.yenaly_libs.utils.applicationContext
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.serialization.json.Json
import okhttp3.Cache
import okhttp3.Call
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import java.io.File
import java.io.IOException
import java.util.concurrent.TimeUnit
import kotlin.coroutines.resume
Expand All @@ -25,6 +28,11 @@ object ServiceCreator {
ignoreUnknownKeys = true
}

val cache = Cache(
directory = File(applicationContext.cacheDir, "http_cache"),
maxSize = 10 * 1024 * 1024
)

inline fun <reified T> create(baseUrl: String): T = Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
Expand Down Expand Up @@ -63,6 +71,7 @@ object ServiceCreator {
chain.request().newBuilder().addHeader("User-Agent", USER_AGENT).build()
return@addInterceptor chain.proceed(request)
}
.cache(cache)
.cookieJar(HCookieJar())
.proxySelector(HProxySelector())
.dns(HDns())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ interface HanimeBaseService {
): Response<ResponseBody>

@GET("login")
suspend fun loginPage(): Response<ResponseBody>
suspend fun getLoginPage(): Response<ResponseBody>
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.yenaly.han1meviewer.databinding.ActivityLoginBinding
import com.yenaly.han1meviewer.logic.NetworkRepo
import com.yenaly.han1meviewer.logic.state.WebsiteState
import com.yenaly.han1meviewer.login
import com.yenaly.han1meviewer.util.CookieString
import com.yenaly.yenaly_libs.base.frame.FrameActivity
import com.yenaly.yenaly_libs.utils.SystemStatusUtil
import com.yenaly.yenaly_libs.utils.showShortToast
Expand All @@ -40,7 +39,6 @@ class LoginActivity : FrameActivity() {

private lateinit var binding: ActivityLoginBinding


private val dialog by unsafeLazy { LoginDialog(R.layout.dialog_login) }

override fun setUiStyle() {
Expand Down Expand Up @@ -71,11 +69,11 @@ class LoginActivity : FrameActivity() {
it.setDisplayHomeAsUpEnabled(true)
it.setHomeActionContentDescription(R.string.back)
}
initWebView()
binding.srlLogin.setOnRefreshListener {
binding.wvLogin.loadUrl(HANIME_LOGIN_URL)
}
binding.srlLogin.autoRefresh()
initWebView()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
Expand Down Expand Up @@ -118,7 +116,7 @@ class LoginActivity : FrameActivity() {
val url = request.url
val cookieManager = CookieManager.getInstance().getCookie(url.host)
Log.d("login_cookie", cookieManager.toString())
login(CookieString(cookieManager))
login(cookieManager)
setResult(RESULT_OK)
finish()
return true
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/yenaly/han1meviewer/ui/view/HJzvdStd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.util.AttributeSet
import android.util.Log
import android.view.GestureDetector
import android.view.Gravity
import android.view.HapticFeedbackConstants
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
Expand Down Expand Up @@ -239,6 +240,7 @@ class HJzvdStd @JvmOverloads constructor(
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> {
if (mediaInterface.isPlaying) {
setSpeedInternal(videoSpeed * userDefLongPressSpeedTimes)
textureViewContainer.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS)
isSpeedGestureDetected = true
}
}
Expand All @@ -254,6 +256,7 @@ class HJzvdStd @JvmOverloads constructor(
tvKeyframe = findViewById(R.id.tv_keyframe)
tvTimer = findViewById(R.id.tv_timer)
btnGoHome = findViewById(R.id.go_home)
textureViewContainer.isHapticFeedbackEnabled = true
tvSpeed.setOnClickListener(this)
tvKeyframe.setOnClickListener(this)
tvKeyframe.setOnLongClickListener(this)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/yenaly/han1meviewer/util/Cookies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ internal fun CookieString.toLoginCookieList(domain: String): List<Cookie> {
it += preferencesCookieList(domain)
}
cookie.split(';').forEach { cookie ->
val name = cookie.substringBefore('=').trim()
if (name.equals("hanime1_session", ignoreCase = true)) {
if (cookie.isNotBlank()) {
val name = cookie.substringBefore('=').trim()
val value = cookie.substringAfter('=').trim()
cookieList += Cookie.Builder().domain(domain).name(name).value(value).build()
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/dialog_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
android:paddingHorizontal="16dp"
android:paddingTop="8dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:text="@string/login_tips" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,5 @@
<string name="login_failed">登入失敗</string>
<string name="login_successful">登入成功</string>
<string name="account_or_password_wrong">賬戶或密碼可能錯誤</string>
<string name="login_tips">該方法只能保持登入 2 個小時… 還是推薦使用網頁登入,\n但網頁是不走 Hosts 的,所以可能需要 VPN。</string>
</resources>

0 comments on commit 0d4a8c9

Please sign in to comment.