mirror of
https://github.com/seven332/EhViewer
synced 2026-11-26 17:18:06 +08:00
Add gallery comments parsing
This commit is contained in:
1 parent
abda29dd17
commit
2c90238b2d
5 files changed
+189
-9
No files matched your search
@@ -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<Comment> {
|
||||
override fun createFromParcel(parcel: Parcel): Comment {
|
||||
return Comment(parcel)
|
||||
}
|
||||
|
||||
override fun newArray(size: Int): Array<Comment?> {
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Comment> {
|
||||
val comments = mutableListOf<Comment>()
|
||||
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")
|
||||
|
||||
@@ -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()
|
||||
@@ -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" +
|
||||
"<a href=\"https://exhentai.org/g/1042775/eb45af810d/\">https://exhentai.org/g/1042775/eb45af810d/</a>", 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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
<title>(CiNDERELLA ☆ STAGE 5 STEP) [LAMINARIA (Shiokonbu)] Bergamo (THE IDOLM@STER CINDERELLA GIRLS) [English] - ExHentai.org</title>
|
||||
<link rel="stylesheet" type="text/css" href="https://exhentai.org/z/0331/x.css" />
|
||||
|
||||
</head>
|
||||
<body style="text-align:center">
|
||||
<script type="text/javascript">
|
||||
function popUp(URL,w,h) {
|
||||
window.open(URL,"_pu"+(Math.random()+"").replace(/0\./,""),"toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+w+",height="+h+",left="+((screen.width-w)/2)+",top="+((screen.height-h)/2));
|
||||
return false;
|
||||
}
|
||||
</script>
|
||||
<p id="nb"><img src="https://exhentai.org/img/mr.gif" /> <a href="https://exhentai.org/">Front Page</a><img src="https://exhentai.org/img/mr.gif" /> <a href="https://exhentai.org/torrents.php">Torrents</a><img src="https://exhentai.org/img/mr.gif" /> <a href="https://exhentai.org/favorites.php">Favorites</a><img src="https://exhentai.org/img/mr.gif" /> <a href="https://exhentai.org/uconfig.php">Settings</a><img src="https://exhentai.org/img/mr.gif" /> <a href="https://exhentai.org/upload/manage.php">My Galleries</a></p>
|
||||
<script type="text/javascript">
|
||||
|
||||
var base_url = "https://exhentai.org/";
|
||||
var api_url = "https://exhentai.org/api.php";
|
||||
var gid = 1099541;
|
||||
var token = "03c1ebd0b4";
|
||||
var apiuid = 1363542;
|
||||
var apikey = "f4b5407ab1727b9d08d7";
|
||||
var average_rating = 3.97;
|
||||
var display_rating = 3.97;
|
||||
var popbase = base_url+"gallerypopups.php?gid=1099541&t=03c1ebd0b4&act=";
|
||||
function pop_fav(){return popUp(popbase+"addfav",675,415);}
|
||||
function pop_exp(){return popUp(popbase+"expunge",675,415);}
|
||||
function pop_ren(){return popUp(popbase+"rename",675,415);}
|
||||
</script>
|
||||
<div class="gm"><div id="gleft"><div id="gd1"><div style="width:250px; height:350px; background:transparent url(https://exhentai.org/t/17/28/1728342b8e98d293c55094f351bf783bff155c21-1916997-2590-3624-jpg_250.jpg) 0 0 no-repeat"></div></div></div><div id="gd2"><h1 id="gn">(CiNDERELLA ☆ STAGE 5 STEP) [LAMINARIA (Shiokonbu)] Bergamo (THE IDOLM@STER CINDERELLA GIRLS) [English]</h1><h1 id="gj">(シンデレラ☆ステージ5STEP) [LAMINARIA (しおこんぶ)] Bergamo (アイドルマスター シンデレラガールズ) [英訳]</h1></div><div id="gright"><div id="gd5"><p class="g3 gsp"><img src="https://exhentai.org/img/mr.gif" /> <a href="https://exhentai.org/g/1099541/03c1ebd0b4/?report=select">Report Gallery</a></p><p class="g2 gsp"><img src="https://exhentai.org/img/mr.gif" /> <a href="#" onclick="return popUp('https://exhentai.org/archiver.php?gid=1099541&token=03c1ebd0b4&or=417339--47cd08fc4392c88e73a3b8756ff5592041a8c218',480,320)">Archive Download</a></p><p class="g2"><img src="https://exhentai.org/img/mr.gif" /> <a href="#" onclick="return popUp('https://exhentai.org/gallerytorrents.php?gid=1099541&t=03c1ebd0b4',610,590)">Torrent Download ( 1 )</a></p><p class="g2 gsp"><img src="https://exhentai.org/img/mr.gif" /> <a id="expungelink" href="#" onclick="return pop_exp()">Petition to Expunge</a></p><p class="g2"><img src="https://exhentai.org/img/mr.gif" /> <a id="renamelink" href="#" onclick="return pop_ren()">Petition to Rename</a></p></div></div><div id="gmid"><div id="gd3"><div id="gdc"><a href="https://exhentai.org/doujinshi"><img src="https://exhentai.org/img/c/doujinshi.png" alt="doujinshi" class="ic" /></a></div><div id="gdn"><a href="https://exhentai.org/uploader/doggerhotter">doggerhotter</a></div><div id="gdd"><table><tr><td class="gdt1">Posted:</td><td class="gdt2">2017-08-11 02:20</td></tr><tr><td class="gdt1">Parent:</td><td class="gdt2">None</td></tr><tr><td class="gdt1">Visible:</td><td class="gdt2">Yes</td></tr><tr><td class="gdt1">Language:</td><td class="gdt2">English <span class="halp" title="This gallery has been translated from the original language text.">TR</span></td></tr><tr><td class="gdt1">File Size:</td><td class="gdt2">346.7 MB</td></tr><tr><td class="gdt1">Length:</td><td class="gdt2">23 pages</td></tr><tr><td class="gdt1">Favorited:</td><td class="gdt2" id="favcount">40 times</td></tr></table></div><div id="gdr" onmouseout="rating_reset()"><table><tr><td id="grt1">Rating:</td><td id="grt2"><div id="rating_image" class="ir" style="background-position:-16px -1px"><img src="https://exhentai.org/img/blank.gif" usemap="#rating" /></div></td><td id="grt3"><span id="rating_count">19</span></td></tr><tr><td id="rating_label" colspan="3">Average: 3.97</td></tr></table><map name="rating"><area shape="rect" coords="0,0,7,16" onclick="rating_set(1)" onmouseover="rating_show(1)" /><area shape="rect" coords="8,0,15,16" onclick="rating_set(2)" onmouseover="rating_show(2)" /><area shape="rect" coords="16,0,23,16" onclick="rating_set(3)" onmouseover="rating_show(3)" /><area shape="rect" coords="24,0,31,16" onclick="rating_set(4)" onmouseover="rating_show(4)" /><area shape="rect" coords="32,0,39,16" onclick="rating_set(5)" onmouseover="rating_show(5)" /><area shape="rect" coords="40,0,47,16" onclick="rating_set(6)" onmouseover="rating_show(6)" /><area shape="rect" coords="48,0,55,16" onclick="rating_set(7)" onmouseover="rating_show(7)" /><area shape="rect" coords="56,0,63,16" onclick="rating_set(8)" onmouseover="rating_show(8)" /><area shape="rect" coords="64,0,71,16" onclick="rating_set(9)" onmouseover="rating_show(9)" /><area shape="rect" coords="72,0,79,16" onclick="rating_set(10)" onmouseover="rating_show(10)" /></map></div><div id="gdf" onclick="return pop_fav()"><div style="float:left; cursor:pointer" id="fav"></div><div style="float:left"> <a id="favoritelink" href="#" onclick="return false"><img src="https://exhentai.org/img/mr.gif" /> Add to Favorites</a></div><div class="c"></div></div></div><div id="gd4"><div id="taglist"><table><tr><td class="tc">language:</td><td><div id="td_language:english" class="gt" style="opacity:1.0"><a id="ta_language:english" href="https://exhentai.org/tag/language%3Aenglish" class="" onclick="return toggle_tagmenu('language:english',this)">english</a></div><div id="td_language:translated" class="gt" style="opacity:1.0"><a id="ta_language:translated" href="https://exhentai.org/tag/language%3Atranslated" class="" onclick="return toggle_tagmenu('language:translated',this)">translated</a></div></td></tr><tr><td class="tc">parody:</td><td><div id="td_parody:the_idolmaster" class="gt" style="opacity:1.0"><a id="ta_parody:the_idolmaster" href="https://exhentai.org/tag/parody%3Athe+idolmaster" class="" onclick="return toggle_tagmenu('parody:the idolmaster',this)">the idolmaster</a></div></td></tr><tr><td class="tc">character:</td><td><div id="td_character:rin_shibuya" class="gt" style="opacity:1.0"><a id="ta_character:rin_shibuya" hLine truncated
|
||||
<p id="frontpage">[<a href="https://exhentai.org/">Front Page</a>]</p>
|
||||
<script type="text/javascript" src="https://exhentai.org/z/0331/ehg_gallery.c.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user