This repository was archived by the owner on Oct 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathContentType.swift
325 lines (276 loc) · 10.8 KB
/
ContentType.swift
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// ContentType.swift
// Slide for Apple Watch Extension
//
// Created by Carlos Crane on 9/25/18.
// Copyright © 2018 Haptic Apps. All rights reserved.
//
import Foundation
/**
* Created by ccrama on 5/26/2015.
*/
class ContentType {
/**
* Checks if {@code host} is contains by any of the provided {@code bases}
* <p/>
* For example "www.youtube.com" contains "youtube.com" but not "notyoutube.com" or
* "youtube.co.uk"
*
* @param host A hostname from e.g. {@link URI#getHost()}
* @param bases Any number of hostnames to compare against {@code host}
* @return If {@code host} contains any of {@code bases}
*/
public static func hostContains(host: String?, bases: [String]?) -> Bool {
if host == nil || (host?.isEmpty)! {
return false
}
for b in bases! {
if b.isEmpty {
continue
}
if (host?.hasSuffix("." + b))! || (host == b) { return true }
}
return false
}
public static func isExternal(_ uri: URL) -> Bool {
return false
}
public static func isSpoiler(uri: URL) -> Bool {
let urlString = uri.absoluteString
if !urlString.hasPrefix("//") && ((urlString.hasPrefix("/") && urlString.length < 4)
|| urlString.hasPrefix("#spoil")
|| urlString.hasPrefix("/spoil")
|| urlString.hasPrefix("#s-")
|| urlString == ("#s")
|| urlString == ("#ln")
|| urlString == ("#b")
|| urlString == ("#sp")) {
return true
}
return false
}
public static func isTable(uri: URL) -> Bool {
let urlString = uri.absoluteString
if urlString.contains("http://view.table/") {
return true
}
return false
}
public static func isGifLoadInstantly(uri: URL) -> Bool {
let host = uri.host?.lowercased()
let path = uri.path.lowercased()
return hostContains(host: host, bases: ["gfycat.com", "redgifs.com", "v.redd.it"]) || ((hostContains(host: host, bases: ["preview.redd.it", "external-preview.redd.it"]) && uri.absoluteString.contains("format=mp4"))) || (hostContains(host: host, bases: ["redditmedia.com", "imgur.com"]) && path.endsWith(".gif") || path.endsWith(".gifv") || path.endsWith(".webm")) || path.endsWith(".mp4")
}
public static func isGif(uri: URL) -> Bool {
let host = uri.host?.lowercased()
let path = uri.path.lowercased()
return hostContains(host: host, bases: ["gfycat.com", "redgifs.com", "v.redd.it"]) || path.hasSuffix(".gif") || path.hasSuffix(
".gifv") || path.hasSuffix(".webm") || path.hasSuffix(".mp4")
}
public static func isGfycat(uri: URL) -> Bool {
let host = uri.host?.lowercased()
return hostContains(host: host, bases: ["gfycat.com"])
}
public static func isImage(uri: URL) -> Bool {
let host = uri.host?.lowercased()
let path = uri.path.lowercased()
return host == ("i.reddituploads.com") || path.hasSuffix(".png") || path.hasSuffix(
".jpg") || path.hasSuffix(".jpeg")
}
public static func isImgurImage(uri: URL) -> Bool {
let host = uri.host?.lowercased()
let path = uri.path.lowercased()
return (host!.contains("imgur.com") || host!.contains("bildgur.de")) && ((path.hasSuffix(
".png") || path.hasSuffix(".jpg") || path.hasSuffix(".jpeg")))
}
public static func isImgurHash(uri: URL) -> Bool {
let host = uri.host?.lowercased()
let path = uri.path.lowercased()
return (host!.contains("imgur.com")) && !(path.hasSuffix(".png") && !path.hasSuffix(".jpg") && !path.hasSuffix(".jpeg"))
}
public static func isAlbum(uri: URL) -> Bool {
let host = uri.host?.lowercased()
let path = uri.path.lowercased()
return hostContains(host: host, bases: ["imgur.com", "bildgur.de"]) && (path.hasPrefix("/a/")
|| path.hasPrefix("/gallery/")
|| path.hasPrefix("/g/")
|| path.contains(","))
}
public static func isGallery(uri: URL) -> Bool {
let host = uri.host?.lowercased()
let path = uri.path.lowercased()
return hostContains(host: host, bases: ["reddit.com"]) && (path.hasPrefix("/gallery/"))
}
public static func isVideo(uri: URL) -> Bool {
let host = uri.host?.lowercased()
let path = uri.path.lowercased()
return hostContains(host: host, bases: ["youtu.be", "youtube.com", "youtube.co.uk"]) && !path.contains("/user/") && !path.contains("/channel/")
}
public static func isImgurLink(uri: URL) -> Bool {
let host = uri.host?.lowercased()
return hostContains(host: host, bases: ["imgur.com", "bildgur.de"]) && !isAlbum(uri: uri) && !isGif(uri: uri) && !isImage(uri: uri)
}
/**
* Attempt to determine the content type of a link from the URL
*
* @param url URL to get ContentType from
* @return ContentType of the URL
*/
public static func getContentType(baseUrl: URL?) -> CType {
if baseUrl == nil || baseUrl!.absoluteString.isEmpty() {
return CType.NONE
}
var urlString = baseUrl!.absoluteString
if urlString.hasPrefix("applewebdata:") {
urlString = baseUrl!.path
}
if !urlString.hasPrefix("//") && ((urlString.hasPrefix("/") && urlString.length < 4)
|| urlString.hasPrefix("#spoil")
|| urlString.hasPrefix("/spoil")
|| urlString.hasPrefix("#s-")
|| urlString == ("#s")
|| urlString == ("#ln")
|| urlString == ("#b")
|| urlString == ("#sp")) {
return CType.SPOILER
}
if urlString.contains("http://view.table/") {
return CType.TABLE
}
if urlString.hasPrefix("//") { urlString = "https:" + urlString }
if urlString.hasPrefix("/") { urlString = "reddit.com" + urlString }
if !urlString.contains("://") { urlString = "http://" + urlString }
let url = URL.init(string: urlString)
let host = url?.host?.lowercased()
let scheme = url?.scheme?.lowercased()
if ContentType.isExternal(url!) {
return .EXTERNAL
}
if host == nil || scheme == nil || !(scheme == ("http") || scheme == ("https")) {
return CType.EXTERNAL
}
if isVideo(uri: url!) {
return CType.YOUTUBE
}
if isGif(uri: url!) {
return CType.GIF
}
if isImage(uri: url!) {
return CType.IMAGE
}
if isAlbum(uri: url!) {
return CType.ALBUM
}
if isGallery(uri: url!) {
return CType.REDDIT_GALLERY
}
if hostContains(host: host, bases: ["imgur.com", "bildgur.de"]) {
return CType.IMGUR
}
if hostContains(host: host, bases: ["xkcd.com"]) && !(host == ("imgs.xkcd.com")) && !(host == ("what-if.xkcd.com")) {
return CType.XKCD
}
/* Currently doesn't work
if hostContains(host: host, bases: ["tumblr.com"]) && (url?.path.contains("post"))! {
return CType.TUMBLR
}*/
if hostContains(host: host, bases: ["reddit.com", "redd.it"]) {
return CType.REDDIT
}
if hostContains(host: host, bases: ["vid.me"]) {
return CType.VID_ME
}
if hostContains(host: host, bases: ["deviantart.com"]) {
return CType.DEVIANTART
}
if hostContains(host: host, bases: ["streamable.com"]) {
return CType.STREAMABLE
}
return CType.LINK
}
/**
* Attempts to determine the content of a submission, mostly based on the URL
*
* @param submission Submission to get the content type from
* @return Content type of the Submission
* @see #getContentType(String)
*/
public static func getContentType(dict: NSDictionary) -> CType {
let url = URL(string: dict["url"] as? String ?? "")
if url == nil {
return .NONE
}
let basicType = getContentType(baseUrl: url)
if (dict["is_self"] as? Bool ?? false)! {
return CType.SELF
}
// TODO: - Decide whether internal youtube links should be EMBEDDED or LINK
/* if (basicType == (CType.LINK) && submission?.mediaEmbed != nil && !submission?.mediaEmbed!.content.isEmpty{
return CType.EMBEDDED;
}*/
return basicType
}
public static func displayImage(t: CType) -> Bool {
switch t {
case CType.ALBUM, CType.REDDIT_GALLERY, CType.DEVIANTART, CType.IMAGE, CType.XKCD, CType.TUMBLR, CType.IMGUR, CType.SELF:
return true
default:
return false
}
}
public static func displayVideo(t: CType) -> Bool {
switch t {
case CType.STREAMABLE, CType.VID_ME, CType.YOUTUBE, CType.GIF:
return true
default:
return false
}
}
public static func imageType(t: CType) -> Bool {
return (t == .IMAGE || t == .IMGUR)
}
public static func fullImage(t: CType) -> Bool {
switch t {
case CType.ALBUM, CType.REDDIT_GALLERY, CType.DEVIANTART, CType.GIF, CType.IMAGE, CType.IMGUR, CType.STREAMABLE, CType.TUMBLR, CType.XKCD, CType.YOUTUBE, CType.SELF, CType.VID_ME:
return true
case CType.EMBEDDED, CType.EXTERNAL, CType.LINK, CType.NONE, CType.REDDIT, CType.SPOILER, CType.TABLE, CType.UNKNOWN:
return false
}
}
public static func mediaType(t: CType) -> Bool {
switch t {
case CType.ALBUM, CType.REDDIT_GALLERY, CType.DEVIANTART, CType.GIF, CType.IMAGE, CType.TUMBLR, CType.XKCD, CType.IMGUR, CType.STREAMABLE, CType.VID_ME:
return true
default:
return false
}
}
/**
* Returns a string identifier for a submission e.g. Link, GIF, NSFW Image
*
* @param submission Submission to get the description for
* @return the String identifier
*/
enum CType {
case UNKNOWN
case ALBUM
case DEVIANTART
case EMBEDDED
case EXTERNAL
case GIF
case IMAGE
case IMGUR
case LINK
case NONE
case SPOILER
case REDDIT_GALLERY
case REDDIT
case SELF
case STREAMABLE
case YOUTUBE
case XKCD
case TUMBLR
case VID_ME
case TABLE
}
}