forked from QuantumBadger/RedReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedditURLParser.java
300 lines (238 loc) · 7.51 KB
/
RedditURLParser.java
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*******************************************************************************
* 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.url;
import android.content.Context;
import android.net.Uri;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.Optional;
import org.quantumbadger.redreader.common.StringUtils;
import org.quantumbadger.redreader.common.UriString;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public class RedditURLParser {
public static final int SUBREDDIT_POST_LISTING_URL = 0;
public static final int USER_POST_LISTING_URL = 1;
public static final int SEARCH_POST_LISTING_URL = 2;
public static final int UNKNOWN_POST_LISTING_URL = 3;
public static final int USER_PROFILE_URL = 4;
public static final int USER_COMMENT_LISTING_URL = 5;
public static final int UNKNOWN_COMMENT_LISTING_URL = 6;
public static final int POST_COMMENT_LISTING_URL = 7;
public static final int MULTIREDDIT_POST_LISTING_URL = 8;
public static final int COMPOSE_MESSAGE_URL = 9;
public static final int OPAQUE_SHARED_URL = 10;
@IntDef({
SUBREDDIT_POST_LISTING_URL,
USER_POST_LISTING_URL,
SEARCH_POST_LISTING_URL,
UNKNOWN_POST_LISTING_URL,
USER_PROFILE_URL,
USER_COMMENT_LISTING_URL,
UNKNOWN_COMMENT_LISTING_URL,
POST_COMMENT_LISTING_URL,
MULTIREDDIT_POST_LISTING_URL,
COMPOSE_MESSAGE_URL,
OPAQUE_SHARED_URL
})
@Retention(RetentionPolicy.SOURCE)
public @interface PathType {
}
private static Optional<Uri> tryGetRedditUri(final Uri uri) {
if(uri == null || uri.getHost() == null || uri.getPath() == null) {
return Optional.empty();
}
if("reddit".equals(uri.getScheme()) && "reddit".equals(uri.getHost())) {
return Optional.of(uri.buildUpon()
.scheme("https")
.authority("reddit.com")
.build());
}
if("reddit.app.link".equals(uri.getHost())) {
final String redirect = uri.getQueryParameter("$og_redirect");
if(redirect != null) {
return Optional.ofNullable(Uri.parse(redirect));
}
}
final String ampPrefix = "/amp/s/amp.reddit.com";
if((("google.com".equals(uri.getHost())
|| uri.getHost().endsWith(".google.com"))
&& uri.getPath().startsWith(ampPrefix))) {
return Optional.ofNullable(Uri.parse(
"https://reddit.com" + uri.getPath().substring(ampPrefix.length())));
}
final String[] hostSegments = StringUtils.asciiLowercase(uri.getHost()).split("\\.");
if(hostSegments.length < 2) {
return Optional.empty();
}
if(hostSegments[hostSegments.length - 1].equals("com")
&& hostSegments[hostSegments.length - 2].equals("reddit")) {
return Optional.of(uri);
}
if(hostSegments[hostSegments.length - 1].equals("it")
&& hostSegments[hostSegments.length - 2].equals("redd")) {
return Optional.of(uri);
}
return Optional.empty();
}
@Nullable
public static RedditURL parse(final Uri rawUri) {
if(rawUri == null) {
return null;
}
final Optional<Uri> optionalUri = tryGetRedditUri(rawUri);
if(optionalUri.isEmpty()) {
return null;
}
final Uri uri = optionalUri.get();
{
final OpaqueSharedURL opaqueSharedURL = OpaqueSharedURL.parse(uri);
if(opaqueSharedURL != null) {
return opaqueSharedURL;
}
}
{
final SubredditPostListURL subredditPostListURL = SubredditPostListURL.parse(uri);
if(subredditPostListURL != null) {
return subredditPostListURL;
}
}
{
final MultiredditPostListURL multiredditPostListURL
= MultiredditPostListURL.parse(uri);
if(multiredditPostListURL != null) {
return multiredditPostListURL;
}
}
{
final SearchPostListURL searchPostListURL = SearchPostListURL.parse(uri);
if(searchPostListURL != null) {
return searchPostListURL;
}
}
{
final UserPostListingURL userPostListURL = UserPostListingURL.parse(uri);
if(userPostListURL != null) {
return userPostListURL;
}
}
{
final UserCommentListingURL userCommentListURL = UserCommentListingURL.parse(
uri);
if(userCommentListURL != null) {
return userCommentListURL;
}
}
{
final PostCommentListingURL commentListingURL = PostCommentListingURL.parse(
uri);
if(commentListingURL != null) {
return commentListingURL;
}
}
{
final UserProfileURL userProfileURL = UserProfileURL.parse(uri);
if(userProfileURL != null) {
return userProfileURL;
}
}
{
final ComposeMessageURL composeMessageURL = ComposeMessageURL.parse(uri);
//noinspection RedundantIfStatement
if(composeMessageURL != null) {
return composeMessageURL;
}
}
return null;
}
public static RedditURL parseProbableCommentListing(final Uri uri) {
final RedditURL matchURL = parse(uri);
if(matchURL != null) {
return matchURL;
}
return new UnknownCommentListURL(uri);
}
@NonNull
public static RedditURL parseProbablePostListing(final Uri uri) {
final RedditURL matchURL = parse(uri);
if(matchURL != null) {
return matchURL;
}
return new UnknownPostListURL(uri);
}
public static abstract class RedditURL {
public abstract Uri generateJsonUri();
public abstract @PathType
int pathType();
public final SubredditPostListURL asSubredditPostListURL() {
return (SubredditPostListURL)this;
}
public final MultiredditPostListURL asMultiredditPostListURL() {
return (MultiredditPostListURL)this;
}
public final SearchPostListURL asSearchPostListURL() {
return (SearchPostListURL)this;
}
public final UserPostListingURL asUserPostListURL() {
return (UserPostListingURL)this;
}
public UserProfileURL asUserProfileURL() {
return (UserProfileURL)this;
}
public PostCommentListingURL asPostCommentListURL() {
return (PostCommentListingURL)this;
}
public UserCommentListingURL asUserCommentListURL() {
return (UserCommentListingURL)this;
}
public ComposeMessageURL asComposeMessageURL() {
return (ComposeMessageURL)this;
}
public String humanReadableName(final Context context, final boolean shorter) {
return humanReadablePath();
}
public String humanReadableUrl() {
return "reddit.com" + humanReadablePath();
}
public String humanReadablePath() {
final Uri src = generateJsonUri();
final StringBuilder builder = new StringBuilder();
for(final String pathElement : src.getPathSegments()) {
if(!pathElement.equals(".json")) {
builder.append("/");
builder.append(pathElement);
}
}
return builder.toString();
}
@NonNull
public UriString browserUrl() {
return new UriString(Constants.Reddit.SCHEME_HTTPS + "://" + humanReadableUrl());
}
@Override
@NonNull
public String toString() {
return generateJsonUri().toString();
}
@NonNull
public UriString toUriString() {
return new UriString(toString());
}
}
}