diff --git a/app/src/main/java/com/hippo/ehviewer/client/data/Comment.kt b/app/src/main/java/com/hippo/ehviewer/client/data/Comment.kt new file mode 100644 index 00000000..14bed448 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/client/data/Comment.kt @@ -0,0 +1,71 @@ +/* + * Copyright 2017 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.ehviewer.client.data + +import android.os.Parcel +import android.os.Parcelable + +/* + * Created by Hippo on 2017/8/11. + */ + +class Comment() : Parcelable { + + // 0 for uploader comment. can't vote + var id: Long = 0 + var date: Long = 0 + var user: String? = null + var comment: String? = null + var score: Int = 0 + var votedUp: Boolean = false + var votedDown: Boolean = false + var voteState: String? = null + + constructor(parcel: Parcel) : this() { + id = parcel.readLong() + date = parcel.readLong() + user = parcel.readString() + comment = parcel.readString() + score = parcel.readInt() + votedUp = parcel.readByte() != 0.toByte() + votedDown = parcel.readByte() != 0.toByte() + voteState = parcel.readString() + } + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.writeLong(id) + parcel.writeLong(date) + parcel.writeString(user) + parcel.writeString(comment) + parcel.writeInt(score) + parcel.writeByte(if (votedUp) 1 else 0) + parcel.writeByte(if (votedDown) 1 else 0) + parcel.writeString(voteState) + } + + override fun describeContents(): Int = 0 + + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel): Comment { + return Comment(parcel) + } + + override fun newArray(size: Int): Array { + return arrayOfNulls(size) + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt index d3f6d73e..8b7616a6 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt @@ -19,6 +19,7 @@ package com.hippo.ehviewer.client.parser import com.hippo.ehviewer.client.CATEGORY_UNKNOWN import com.hippo.ehviewer.client.FAV_CAT_UNKNOWN import com.hippo.ehviewer.client.categoryValue +import com.hippo.ehviewer.client.data.Comment import com.hippo.ehviewer.client.data.GalleryInfo import com.hippo.ehviewer.client.lang import com.hippo.ehviewer.client.parser.url.parseArchiveUrl @@ -28,6 +29,7 @@ import com.hippo.ehviewer.exception.ParseException import com.hippo.ehviewer.util.strip import org.jsoup.Jsoup import org.jsoup.nodes.Document +import org.jsoup.nodes.Element import java.util.regex.Pattern /* @@ -54,7 +56,7 @@ fun parseGalleryDetail(document: Document): GalleryInfo? { info.coverRatio = width.toFloat() / height.toFloat() } - matcher.group(3).unescape().also { url -> + matcher.group(3).unescape()?.also { url -> info.coverUrl = url info.coverFingerprint = url.parseCoverUrl() } @@ -68,7 +70,7 @@ fun parseGalleryDetail(document: Document): GalleryInfo? { document.elementById("gd5")?.html()?.also { html -> PATTERN_ARCHIVE.matcher(html).takeIf { it.find() }?.also { matcher -> - matcher.group(1).unescape().parseArchiveUrl()?.also { (gid, token, archiveKey) -> + matcher.group(1).unescape()?.parseArchiveUrl()?.also { (gid, token, archiveKey) -> if (info.gid == gid && info.token == token) { info.archiveKey = archiveKey } @@ -85,8 +87,8 @@ fun parseGalleryDetail(document: Document): GalleryInfo? { document.elementById("gdd")?.elementByTag("tbody")?.children()?.forEach { element -> element.children().takeIf { it.size >= 2 }?.also { children -> - val key = children[0].unescape() - val value = children[1].ownText().unescape() + val key = children[0].unescape() ?: "" + val value = children[1].ownText().unescape() ?: "" if (key.startsWith("Posted")) { info.date = value.date() @@ -121,9 +123,9 @@ fun parseGalleryDetail(document: Document): GalleryInfo? { document.elementById("taglist")?.elementByTag("tbody")?.children()?.forEach { element -> element.children().takeIf { it.size >= 2 }?.also { children -> - children[0].unescape().trim(':').takeIf { it.isNotEmpty() }?.also { namespace -> + children[0].unescape()?.trim(':')?.takeIf { it.isNotEmpty() }?.also { namespace -> children[1].children().forEach { div -> - div.unescape().substringBefore("|").strip().takeIf { it.isNotEmpty() }?.also { info.tags.add(namespace, it) } + div.unescape()?.substringBefore("|")?.strip()?.takeIf { it.isNotEmpty() }?.also { info.tags.add(namespace, it) } } } } @@ -150,6 +152,45 @@ private fun String.parseSize(): Long { } } +fun parseComments(document: Document): List { + val comments = mutableListOf() + document.elementById("cdiv")?.children()?.forEach { element -> + if (element.className() == "c1") { + val comment = parseComment(element) + if (comment != null) { + comments.add(comment) + } + } + } + return comments +} + +private fun parseComment(element: Element): Comment? { + val id = element.previousElementSibling()?.attr("name")?.takeIf { it.length > 1 }?.substring(1)?.toLongOrNull() ?: return null + + val comment = Comment() + comment.id = id + comment.comment = element.elementByClass("c6")?.html() + + element.elementByClass("c3")?.let { c3 -> + comment.date = c3.ownText().unescape()?.substringAfter("Posted on")?.substringBefore("by:")?.strip()?.commentDate() ?: 0 + comment.user = c3.firstChild()?.unescape() + } + + comment.score = element.elementByClass("c5")?.firstChild()?.integer() ?: 0 + + element.elementByClass("c4")?.children()?.let { children -> + if (children.size == 2) { + comment.votedUp = children[0].attr("style").strip().isNotEmpty() + comment.votedDown = children[1].attr("style").strip().isNotEmpty() + } + } + + comment.voteState = element.elementByClass("c7")?.unescape() + + return comment +} + fun parseGalleryDetail(body: String): GalleryInfo { val document = Jsoup.parse(body) return parseGalleryDetail(document) ?: throw ParseException("Can't parse gallery detail") diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/ParseUtils.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/ParseUtils.kt index 49d9a28a..50cba094 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/ParseUtils.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/ParseUtils.kt @@ -33,16 +33,20 @@ import java.util.TimeZone private val DATE_FORMAT: DateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.ENGLISH).apply { timeZone = TimeZone.getTimeZone("UTC") } +private val COMMENT_DATE_FORMAT: DateFormat = SimpleDateFormat("dd MMMMM yyyy, HH:mm z", Locale.ENGLISH) + fun String.integer(): Int? = dropWhile { it == ',' || Character.isSpaceChar(it) || Character.isWhitespace(it) }.toIntOrNull() fun String.float(): Float? = dropWhile { it == ',' || Character.isSpaceChar(it) || Character.isWhitespace(it) }.toFloat() -fun String.unescape(): String = replaceEach( +fun String.unescape(): String? = replaceEach( arrayOf("&", "<", ">", """, "'", "×", " "), - arrayOf("&", "<", ">", "\"", "'", "×", "\u00a0")).strip() + arrayOf("&", "<", ">", "\"", "'", "×", "\u00a0")).strip().let { if (it.isEmpty()) null else it } fun String.date(): Long = try { DATE_FORMAT.parse(this).time } catch (e: ParseException) { 0 } +fun String.commentDate(): Long = try { COMMENT_DATE_FORMAT.parse(this).time } catch (e: ParseException) { 0 } + inline fun Element.elementById(id: String): Element? = getElementById(id) inline fun Element.elementByClass(className: String): Element? = getElementsByClass(className).first() @@ -61,6 +65,6 @@ inline fun Element.integer(): Int? = text().integer() inline fun Element.float(): Float? = text().float() -inline fun Element.unescape(): String = text().unescape() +inline fun Element.unescape(): String? = text().unescape() inline fun Element.date(): Long = text().date() diff --git a/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryDetailParserTest.kt b/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryDetailParserTest.kt index 4cfd5a8a..50287ae5 100644 --- a/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryDetailParserTest.kt +++ b/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryDetailParserTest.kt @@ -88,4 +88,33 @@ class GalleryDetailParserTest { "female" to listOf("females only") ), info.tags) } + + @Test + fun testParseCommentsExHentai() { + val source = Okio.source(javaClass.classLoader.getResourceAsStream("gallery_comments_exhentai.html")) + val bufferedSource = Okio.buffer(source) + val body = bufferedSource.readUtf8() + val comments = parseComments(Jsoup.parse(body)) + + assertEquals(2, comments.size) + + assertEquals(0, comments[0].id) + assertEquals("11 August 2017, 02:20 UTC".commentDate(), comments[0].date) + assertEquals("doggerhotter", comments[0].user) + assertEquals("raw: \n" + + "https://exhentai.org/g/1042775/eb45af810d/", comments[0].comment) + assertEquals(0, comments[0].score) + assertEquals(false, comments[0].votedUp) + assertEquals(false, comments[0].votedDown) + assertEquals(null, comments[0].voteState) + + assertEquals(1864159, comments[1].id) + assertEquals("11 August 2017, 03:39 UTC".commentDate(), comments[1].date) + assertEquals("Momo_Yuki", comments[1].user) + assertEquals("Over 300+MB....", comments[1].comment) + assertEquals(7, comments[1].score) + assertEquals(false, comments[1].votedUp) + assertEquals(false, comments[1].votedDown) + assertEquals("Base +7", comments[1].voteState) + } } diff --git a/app/src/test/resources/gallery_comments_exhentai.html b/app/src/test/resources/gallery_comments_exhentai.html new file mode 100644 index 00000000..40672554 --- /dev/null +++ b/app/src/test/resources/gallery_comments_exhentai.html @@ -0,0 +1,35 @@ + + + +(CiNDERELLA ☆ STAGE 5 STEP) [LAMINARIA (Shiokonbu)] Bergamo (THE IDOLM@STER CINDERELLA GIRLS) [English] - ExHentai.org + + + + + +

Front Page Torrents Favorites Settings My Galleries

+ +

(CiNDERELLA ☆ STAGE 5 STEP) [LAMINARIA (Shiokonbu)] Bergamo (THE IDOLM@STER CINDERELLA GIRLS) [English]

(シンデレラ☆ステージ5STEP) [LAMINARIA (しおこんぶ)] Bergamo (アイドルマスター シンデレラガールズ) [英訳]

doujinshi
Posted:2017-08-11 02:20
Parent:None
Visible:Yes
Language:English  TR
File Size:346.7 MB
Length:23 pages
Favorited:40 times
Rating:
19
Average: 3.97

Showing 1 - 23 of 23 images

<1>
4 rows
10 rows
20 rows
40 rows
Normal
Large
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<1>
Posted on 11 August 2017, 02:20 UTC by:   doggerhotter
Uploader Comment
Posted on 11 August 2017, 03:39 UTC by:   Momo_Yuki
[Vote+]   [Vote-]
Score +7
Over 300+MB....
+

[Front Page]

+ + +