Skip to content

Commit

Permalink
bug fixes. add comment and reply function. add clear cache function.
Browse files Browse the repository at this point in the history
  • Loading branch information
YenalyLiew committed Sep 21, 2022
1 parent 85453bf commit 1c403f2
Show file tree
Hide file tree
Showing 40 changed files with 776 additions and 196 deletions.
7 changes: 6 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ A Hanime1.me Application for Android.

支持高级查询、观看(可切换分辨率与语言)、下载、历史记录以及收藏等功能。

暂不支持评论等功能,且目前可能会存在一系列bug。
支持评论但暂不支持评论点赞点踩等功能,且目前可能会存在一系列bug。

Supports advanced query, watch (be able to switch resolution and language), download, history and add to favorite list.

Comments and some other functions are not supported TEMPORARILY, and there may be a series of bugs at present.
Comment is supported right now but comment like or dislike and some other functions are not supported TEMPORARILY, and there may be a series of bugs at present.

## 存在问题

Expand Down Expand Up @@ -40,6 +40,14 @@ Comments and some other functions are not supported TEMPORARILY, and there may b

## 更新内容

### v0.4.0

新增评论功能,包括影片评论,评论回复,子评论回复,但暂不支持点赞点踩。

新增清理缓存(快取)功能。

优化搜索体验,修复了一些小问题。

### v0.3.0

新增更新功能,不过依赖于 Github 的 API,可能有次数限制。
Expand Down
18 changes: 9 additions & 9 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ plugins {
}

android {
compileSdk = 32
compileSdk = 33

defaultConfig {
applicationId = "com.yenaly.han1meviewer"
minSdk = 24
targetSdk = 32
targetSdk = 33
versionCode = createVersionCode()
versionName = createVersionName(major = 0, minor = 3, patch = 0)
versionName = createVersionName(major = 0, minor = 4, patch = 0)

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -48,8 +48,8 @@ dependencies {
implementation(project(":yenaly_libs"))

// android related
implementation("androidx.core:core-ktx:1.8.0")
implementation("androidx.appcompat:appcompat:1.5.0")
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.appcompat:appcompat:1.5.1")
implementation("com.google.android.material:material:1.6.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1")
Expand All @@ -59,10 +59,10 @@ dependencies {
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.room:room-runtime:2.4.3")
implementation("androidx.room:room-ktx:2.4.3")
implementation("androidx.navigation:navigation-fragment:2.5.1")
implementation("androidx.navigation:navigation-fragment-ktx:2.5.1")
implementation("androidx.navigation:navigation-ui:2.5.1")
implementation("androidx.navigation:navigation-ui-ktx:2.5.1")
implementation("androidx.navigation:navigation-fragment:2.5.2")
implementation("androidx.navigation:navigation-fragment-ktx:2.5.2")
implementation("androidx.navigation:navigation-ui:2.5.2")
implementation("androidx.navigation:navigation-ui-ktx:2.5.2")
implementation("androidx.preference:preference-ktx:1.2.0")
implementation("androidx.work:work-runtime:2.7.1")
implementation("androidx.work:work-runtime-ktx:2.7.1")
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/yenaly/han1meviewer/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const val COMMENT_TYPE = "COMMENT_TYPE"

const val DATE_CODE = "DATE_CODE"

const val CSRF_TOKEN = "CSRF_TOKEN"

// Result Code

const val LOGIN_TO_MAIN_ACTIVITY = 0
Expand Down
208 changes: 126 additions & 82 deletions app/src/main/java/com/yenaly/han1meviewer/logic/NetworkRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -471,86 +471,6 @@ object NetworkRepo {
)
}

fun getHanimeVideoComment(type: String, code: String) = websiteIOFlow(
request = { HanimeNetwork.hanimeService.getComment(type, code) }
) { commentRawBody ->
val jsonObject = JSONObject(commentRawBody)
val commentBody = jsonObject.get("comments").toString()
val parseBody = Jsoup.parse(commentBody).body()
val commentList = mutableListOf<VideoCommentModel>()
val allCommentsClass = parseBody.getElementById("comment-start")
allCommentsClass?.let {
val avatarClasses = it.select("a > img")
val contentClasses = it.getElementsByClass("comment-index-text") // 偶數是日期和作者,奇數是内容
val replyClasses = it.select("div[id=comment-like-form-wrapper]")
for (i in replyClasses.indices) {
val avatarUrl = avatarClasses[i].absUrl("src")
val replyClass = replyClasses[i].select("div > div")
val thumbUp = try {
val up = replyClass[0].child(1).text()
if (up.isInt()) {
up
} else {
throw IndexOutOfBoundsException("應該throw NumberFormatException,但這樣能統一catch")
}
} catch (e: IndexOutOfBoundsException) {
replyClasses[i].select("span[class=comment-like-btn-wrapper] > button > span")[1].text()
}
val hasMoreReplies =
replyClasses[i].select("div[class~=load-replies-btn]").first() != null
val id = replyClasses[i].select("div[id~=reply-section-wrapper]")[0].id()
.substringAfterLast('-')
val usernameAndDateClass = contentClasses[i * 2]
val username = usernameAndDateClass.select("a")[0].ownText()
val date = usernameAndDateClass.select("a > span")[0].ownText()
val content = contentClasses[i * 2 + 1].text()
commentList.add(
VideoCommentModel(
avatar = avatarUrl, username = username, date = date,
content = content, thumbUp = thumbUp, hasMoreReplies = hasMoreReplies,
id = id
)
)
}
}
return@websiteIOFlow WebsiteState.Success(commentList)
}

fun getCommentReply(commentId: String) = websiteIOFlow(
request = { HanimeNetwork.hanimeService.getCommentReply(commentId) }
) { replyRawBody ->
val jsonObject = JSONObject(replyRawBody)
val replyBody = jsonObject.get("replies").toString()
val replyList = mutableListOf<VideoCommentModel>()
val parseBody = Jsoup.parse(replyBody).body()
val replyStart = parseBody.select("div[id*=reply-start]").first()
replyStart?.let {
val allRepliesClass = it.children()
for (i in 0 until (allRepliesClass.size / 2)) {
val avatarUrl = allRepliesClass[i * 2].select("img")[0].absUrl("src")
val content =
allRepliesClass[i * 2].getElementsByClass("comment-index-text")[1].text()
val usernameClass =
allRepliesClass[i * 2].getElementsByClass("comment-index-text")[0]
val username = usernameClass.select("div > a")[0].ownText()
val date = usernameClass.select("div > a > span")[0].ownText().trim()
val thumbAndReplyClass = allRepliesClass[i * 2 + 1]
val thumbUp = with(thumbAndReplyClass.select("span")) {
if (get(1).text().isInt()) get(1).text() else get(2).text()
}

replyList.add(
VideoCommentModel(
avatar = avatarUrl, username = username,
date = date, content = content, thumbUp = thumbUp
)
)
}
}

return@websiteIOFlow WebsiteState.Success(replyList)
}

fun getHanimePreview(date: String) = websiteIOFlow(
request = { HanimeNetwork.hanimeService.getHanimePreview(date) }
) { previewBody ->
Expand Down Expand Up @@ -697,14 +617,138 @@ object NetworkRepo {
) = websiteIOFlow(
request = {
HanimeNetwork.hanimeService.addToMyFavVideo(
videoCode, likeStatus.value, token, token, currentUserId
videoCode, likeStatus.value, token, currentUserId
)
}
) {
Log.d("add_to_fav_body", it)
return@websiteIOFlow WebsiteState.Success(Unit)
}

// ------ COMMENT ------ //

fun getComment(type: String, code: String) = websiteIOFlow(
request = { HanimeNetwork.commentService.getComment(type, code) }
) { commentRawBody ->
val jsonObject = JSONObject(commentRawBody)
val commentBody = jsonObject.get("comments").toString()
val parseBody = Jsoup.parse(commentBody).body()
val csrfToken = parseBody.select("input[name=_token]").first()?.attr("value")
val currentUserId = parseBody.select("input[name=comment-user-id]").first()?.attr("value")
val commentList = mutableListOf<VideoCommentModel.VideoComment>()
val allCommentsClass = parseBody.getElementById("comment-start")
allCommentsClass?.let {
val avatarClasses = it.select("a > img")
val contentClasses = it.getElementsByClass("comment-index-text") // 偶數是日期和作者,奇數是内容
val replyClasses = it.select("div[id=comment-like-form-wrapper]")
for (i in replyClasses.indices) {
val avatarUrl = avatarClasses[i * 2].absUrl("src")
val replyClass = replyClasses[i].select("div > div")
val thumbUp = try {
val up = replyClass[0].child(1).text()
if (up.isInt()) {
up
} else {
throw IndexOutOfBoundsException("應該throw NumberFormatException,但這樣能統一catch")
}
} catch (e: IndexOutOfBoundsException) {
replyClasses[i].select("span[class=comment-like-btn-wrapper] > button > span")[1].text()
}
val hasMoreReplies =
replyClasses[i].select("div[class~=load-replies-btn]").first() != null
val id = replyClasses[i].select("div[id~=reply-section-wrapper]")[0].id()
.substringAfterLast('-')
val usernameAndDateClass = contentClasses[i * 2]
val username = usernameAndDateClass.select("a")[0].ownText()
val date = usernameAndDateClass.select("a > span")[0].ownText()
val content = contentClasses[i * 2 + 1].text()
commentList.add(
VideoCommentModel.VideoComment(
avatar = avatarUrl, username = username, date = date,
content = content, thumbUp = thumbUp, hasMoreReplies = hasMoreReplies,
id = id, isChildComment = false
)
)
}
}
return@websiteIOFlow WebsiteState.Success(
VideoCommentModel(
commentList,
currentUserId,
csrfToken
)
)
}

fun getCommentReply(commentId: String) = websiteIOFlow(
request = { HanimeNetwork.commentService.getCommentReply(commentId) }
) { replyRawBody ->
val jsonObject = JSONObject(replyRawBody)
val replyBody = jsonObject.get("replies").toString()
val replyList = mutableListOf<VideoCommentModel.VideoComment>()
val parseBody = Jsoup.parse(replyBody).body()
val replyStart = parseBody.select("div[id*=reply-start]").first()
replyStart?.let {
val allRepliesClass = it.children()
for (i in 0 until (allRepliesClass.size / 2)) {
val avatarUrl = allRepliesClass[i * 2].select("img")[0].absUrl("src")
val content =
allRepliesClass[i * 2].getElementsByClass("comment-index-text")[1].text()
val usernameClass =
allRepliesClass[i * 2].getElementsByClass("comment-index-text")[0]
val username = usernameClass.select("div > a")[0].ownText()
val date = usernameClass.select("div > a > span")[0].ownText().trim()
val thumbAndReplyClass = allRepliesClass[i * 2 + 1]
val thumbUp = with(thumbAndReplyClass.select("span")) {
if (get(1).text().isInt()) get(1).text() else get(2).text()
}

replyList.add(
VideoCommentModel.VideoComment(
avatar = avatarUrl, username = username,
date = date, content = content, thumbUp = thumbUp,
isChildComment = true
)
)
}
}

return@websiteIOFlow WebsiteState.Success(VideoCommentModel(replyList))
}

fun postComment(
csrfToken: String?,
currentUserId: String,
targetUserId: String,
type: String,
text: String
) = websiteIOFlow(
request = {
HanimeNetwork.commentService.postComment(
csrfToken, currentUserId,
type, targetUserId, text
)
}
) {
Log.d("post_comment_body", it)
return@websiteIOFlow WebsiteState.Success(Unit)
}

fun postCommentReply(
csrfToken: String?,
replyCommentId: String,
text: String
) = websiteIOFlow(
request = {
HanimeNetwork.commentService.postCommentReply(
csrfToken, replyCommentId, text
)
}
) {
Log.d("post_comment_reply_body", it)
return@websiteIOFlow WebsiteState.Success(Unit)
}

// ------ VERSION ------ //

fun getLatestVersion() = flow {
Expand Down Expand Up @@ -788,7 +832,7 @@ object NetworkRepo {
e.printStackTrace()
emit(VideoLoadingState.Error(IndexOutOfBoundsException("可能這個網址解析起來不大一樣...")))
}
is Exception -> {
else -> {
e.printStackTrace()
emit(VideoLoadingState.Error(e))
}
Expand Down
Loading

0 comments on commit 1c403f2

Please sign in to comment.