forked from QuantumBadger/RedReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedditParsedPost.kt
220 lines (170 loc) · 5.68 KB
/
RedditParsedPost.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package org.quantumbadger.redreader.reddit.prepared
import androidx.appcompat.app.AppCompatActivity
import org.quantumbadger.redreader.R
import org.quantumbadger.redreader.common.UriString
import org.quantumbadger.redreader.reddit.PostCommentSort
import org.quantumbadger.redreader.reddit.kthings.RedditIdAndType
import org.quantumbadger.redreader.reddit.kthings.RedditPost
import org.quantumbadger.redreader.reddit.prepared.bodytext.BodyElement
import org.quantumbadger.redreader.reddit.prepared.html.HtmlReader
import org.quantumbadger.redreader.reddit.things.RedditThingWithIdAndType
class RedditParsedPost(
activity: AppCompatActivity,
val src: RedditPost,
parseSelfText: Boolean
) : RedditThingWithIdAndType {
val title: String = src.title?.decoded?.replace('\n', ' ')?.trim() ?: "[null]"
val url: UriString?
val selfText: BodyElement?
val flairText: String?
val domain: String
val permalink = src.permalink.decoded
val isStickied = src.stickied
val thumbnailUrl = UriString.fromNullable(src.thumbnail?.decoded)
val isPreviewEnabled = src.preview?.enabled == true
val isVideoPreview = src.preview?.run {
src.is_video
|| images?.get(0)?.variants?.mp4 != null
|| reddit_video_preview != null
|| when (src.domain?.decoded) {
"v.redd.it", "streamable.com", "gfycat.com" -> true
else -> false
}
} ?: false
val suggestedCommentSort: PostCommentSort?
get() = if (src.suggested_sort == null) {
null
} else PostCommentSort.lookup(src.suggested_sort)
val isArchived = src.archived
val isLocked = src.locked
val canModerate = src.can_mod_post
val author: String?
get() = src.author?.decoded
val distinguished: String?
get() = src.distinguished
val rawSelfTextMarkdown = src.selftext?.decoded
val isSpoiler = src.spoiler
val unescapedSelfText = src.selftext?.decoded
val subreddit = src.subreddit.decoded
// TODO do we still need this? I think it's because we add the score at a later point
val scoreExcludingOwnVote: Int
get() {
var score = src.score
when (src.likes) {
true -> score--
false -> score++
null -> {}
}
return score
}
val commentCount = src.num_comments
val goldAmount = src.gilded
val isCrosspost = src.crosspost_parent
val isNsfw = src.over_18
val createdTimeUTC = src.created_utc.value
val isSelfPost = src.is_self
val upvotePercentage = src.upvote_ratio?.times(100.0)?.toInt()
init {
url = src.findUrl()
selfText = if (parseSelfText && src.selftext_html != null) {
HtmlReader.parse(src.selftext_html.decoded, activity)
} else {
null
}
flairText = if (src.link_flair_text != null && src.link_flair_text.decoded.isNotEmpty()) {
src.link_flair_text.decoded
} else {
null
}
domain = src.domain?.decoded ?: activity.getString(R.string.post_domain_deleted)
}
override fun getIdAlone(): String {
return src.idAlone
}
override fun getIdAndType(): RedditIdAndType {
return src.idAndType
}
fun hasSelfText(): Boolean {
return rawSelfTextMarkdown != null && rawSelfTextMarkdown.length > 1
}
data class ImagePreviewDetails(
@JvmField val url: UriString,
@JvmField val width: Int,
@JvmField val height: Int
)
fun getPreview(minWidth: Int, minHeight: Int) = src.preview?.images?.get(0)?.run {
getPreviewInternal(this, minWidth, minHeight)
}
fun getPreviewMP4(minWidth: Int, minHeight: Int)
= src.preview?.images?.get(0)?.variants?.mp4?.apply {
getPreviewInternal(this, minWidth, minHeight)
}
private fun getPreviewInternal(
image: RedditPost.Preview.ImageBase,
minWidth: Int,
minHeight: Int
): ImagePreviewDetails? {
val resolutions = image.resolutions
if (resolutions.isNullOrEmpty()) {
return null
}
var best: RedditPost.Preview.ImageDetails? = null
val sourceWidth = image.source?.width
val sourceHeight = image.source?.height
for (i in -1 until resolutions.size) {
val resolution = if (i == -1) {
image.source ?: continue
} else {
resolutions[i]
}
if (resolution.width < 50 || resolution.height < 50) {
continue
}
if (sourceWidth != null && sourceHeight != null && sourceWidth > 0) {
val estimatedRealHeight =
(sourceHeight.toDouble() / sourceWidth.toDouble() * resolution.width).toInt()
if (estimatedRealHeight > 3000) {
continue
}
}
val use = if (resolution.height > 3000 || resolution.width > 3000) {
false
} else if (best == null) {
true
} else if ((best.width < minWidth || best.height < minHeight)
&& (resolution.width > best.width || resolution.height > best.height)
) {
true
} else if (resolution.width < best.width
&& resolution.height < best.height
&& resolution.width >= minWidth
&& resolution.height >= minHeight) {
true
} else {
false
}
if (use) {
best = resolution
}
}
return best?.run {
ImagePreviewDetails(UriString(url.decoded), width, height)
}
}
}