This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathLink.swift
371 lines (357 loc) · 12 KB
/
Link.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//
// Link.swift
// reddift
//
// Created by generator.rb via from https://github.com/reddit/reddit/wiki/JSON
// Created at 2015-04-15 11:23:32 +0900
//
import Foundation
import HTMLSpecialCharacters
/**
Returns string by replacing NOT ASCII characters with a percent escaped string using UTF8.
If an argument is nil, returns vacant string.
*/
private func convertObjectToEscapedURLString(_ object: AnyObject?) -> String {
if let urlstring = object as? String {
return urlstring.addPercentEncoding
} else {
return ""
}
}
/**
Link content.
*/
public struct Link: Thing, Created, Votable {
/// identifier of Thing like 15bfi0.
public let id: String
/// name of Thing, that is fullname, like t3_15bfi0.
public let name: String
/// type of Thing, like t3.
public static let kind = "t3"
/**
example: self.redditdev
*/
public let domain: String
/**
example:
*/
public let bannedBy: String
/**
Used for streaming video. Technical embed specific information is found here.
example: {}
*/
public let mediaEmbed: MediaEmbed?
/**
subreddit of thing excluding the /r/ prefix. "pics"
example: redditdev
*/
public let subreddit: String
/**
the formatted escaped HTML text. this is the HTML formatted version of the marked up text. Items that are boldened by ** or *** will now have <em> or *** tags on them. Additionally, bullets and numbered lists will now be in HTML list format. NOTE: The HTML string will be escaped. You must unescape to get the raw HTML. Null if not present.
example: <!-- SC_OFF --><div class="md"><p>So this is the code I ran:</p>
<pre><code>r = praw.Reddit(&quot;/u/habnpam sflkajsfowifjsdlkfj test test test&quot;)
for c in praw.helpers.comment_stream(reddit_session=r, subreddit=&quot;helpmefind&quot;, limit=500, verbosity=1):
print(c.author)
</code></pre>
<hr/>
<p>From what I understand, comment_stream() gets the most recent comments. So if we specify the limit to be 100, it will initially get the 100 newest comment, and then constantly update to get new comments. It seems to works appropriately for every subreddit except <a href="/r/helpmefind">/r/helpmefind</a>. For <a href="/r/helpmefind">/r/helpmefind</a>, it fetches around 30 comments, regardless of the limit.</p>
</div><!-- SC_ON -->
*/
public let selftextHtml: String
/**
the raw text. this is the unformatted text which includes the raw markup characters such as ** for bold. <, >, and & are escaped. Empty if not present.
example: So this is the code I ran:
r = praw.Reddit("/u/habnpam sflkajsfowifjsdlkfj test test test")
for c in praw.helpers.comment_stream(reddit_session=r, subreddit="helpmefind", limit=500, verbosity=1):
print(c.author)
---
From what I understand, comment_stream() gets the most recent comments. So if we specify the limit to be 100, it will initially get the 100 newest comment, and then constantly update to get new comments. It seems to works appropriately for every subreddit except /r/helpmefind. For /r/helpmefind, it fetches around 30 comments, regardless of the limit.
*/
public let selftext: String
/**
how the logged-in user has voted on the link - True = upvoted, False = downvoted, null = no vote
example:
*/
public let likes: VoteDirection
/**
example: []
*/
public let userReports: [AnyObject]
/**
example:
*/
public let secureMedia: AnyObject?
/**
the text of the link's flair.
example:
*/
public let linkFlairText: String
/**
example: 0
*/
public let gilded: Int
/**
example: false
*/
public let archived: Bool
/**
probably always returns false
example: false
*/
public let clicked: Bool
/**
example:
*/
public let reportReasons: [AnyObject]
/**
the account name of the poster. null if this is a promotional link
example: habnpam
*/
public let author: String
/**
the number of comments that belong to this link. includes removed comments.
example: 10
*/
public let numComments: Int
/**
the net-score of the link. note: A submission's score is simply the number of upvotes minus the number of downvotes. If five users like the submission and three users don't it will have a score of 2. Please note that the vote numbers are not "real" numbers, they have been "fuzzed" to prevent spam bots etc. So taking the above example, if five users upvoted the submission, and three users downvote it, the upvote/downvote numbers may say 23 upvotes and 21 downvotes, or 12 upvotes, and 10 downvotes. The points score is correct, but the vote totals are "fuzzed".
example: 2
*/
public let score: Int
/**
example:
*/
public let approvedBy: String
/**
true if the post is tagged as NSFW. False if otherwise
example: false
*/
public let over18: Bool
/**
true if the post is hidden by the logged in user. false if not logged in or not hidden.
example: false
*/
public let hidden: Bool
/**
full URL to the thumbnail for this link; "self" if this is a self post; "default" if a thumbnail is not available
example:
*/
public let thumbnail: String
/**
the id of the subreddit in which the thing is located
example: t5_2qizd
*/
public let subredditId: String
/**
example: false
*/
public let edited: Bool
/**
the CSS class of the link's flair.
example:
*/
public let linkFlairCssClass: String
/**
the CSS class of the author's flair. subreddit specific
example:
*/
public let authorFlairCssClass: String
/**
example: 0
*/
public let downs: Int
/**
example: []
*/
public let modReports: [AnyObject]
/**
example:
*/
public let secureMediaEmbed: AnyObject?
/**
true if this post is saved by the logged in user
example: false
*/
public let saved: Bool
/**
true if this link is a selfpost
example: true
*/
public let isSelf: Bool
/**
relative URL of the permanent link for this link
example: /r/redditdev/comments/32wnhw/praw_comment_stream_messes_up_when_getting/
*/
public let permalink: String
/**
true if the post is set as the sticky in its subreddit.
example: false
*/
public let stickied: Bool
/**
example: 1429292148
*/
public let created: Int
/**
the link of this post. the permalink if this is a self-post
example: http://www.reddit.com/r/redditdev/comments/32wnhw/praw_comment_stream_messes_up_when_getting/
*/
public let url: String
/**
the text of the author's flair. subreddit specific
example:
*/
public let authorFlairText: String
/**
the title of the link. may contain newlines for some reason
example: [PRAW] comment_stream() messes up when getting comments from a certain subreddit.
*/
public let title: String
/**
example: 1429263348
*/
public let createdUtc: Int
/**
example: 2
*/
public let ups: Int
/**
example: 0.75
*/
public let upvoteRatio: Double
/**
Used for streaming video. Detailed information about the video and it's origins are placed here
example:
*/
public let media: Media?
/**
example: false
*/
public let visited: Bool
/**
example: 0
*/
public let numReports: Int
/**
How the link is distinguished from other comments.
example: .admin
*/
public let distinguished: DistinguishType
public init(id: String) {
self.id = id
self.name = "\(Link.kind)_\(self.id)"
domain = ""
bannedBy = ""
subreddit = ""
selftextHtml = ""
selftext = ""
likes = .none
linkFlairText = ""
gilded = 0
archived = false
clicked = false
author = ""
numComments = 0
score = 0
approvedBy = ""
over18 = false
hidden = false
thumbnail = ""
subredditId = ""
edited = false
linkFlairCssClass = ""
authorFlairCssClass = ""
downs = 0
saved = false
isSelf = false
permalink = ""
stickied = false
created = 0
url = ""
authorFlairText = ""
title = ""
createdUtc = 0
ups = 0
upvoteRatio = 0
visited = false
numReports = 0
distinguished = .none
media = Media(json: [:])
mediaEmbed = MediaEmbed(json: [:])
userReports = []
secureMedia = nil
reportReasons = []
modReports = []
secureMediaEmbed = nil
}
/**
Parse t3 object.
- parameter data: Dictionary, must be generated parsing "t3".
- returns: Link object as Thing.
*/
public init(json data: JSONDictionary) {
id = data["id"] as? String ?? ""
domain = data["domain"] as? String ?? ""
bannedBy = data["banned_by"] as? String ?? ""
subreddit = data["subreddit"] as? String ?? ""
let tempSelftextHtml = data["selftext_html"] as? String ?? ""
selftextHtml = tempSelftextHtml.unescapeHTML
let tempSelftext = data["selftext"] as? String ?? ""
selftext = tempSelftext.unescapeHTML
if let temp = data["likes"] as? Bool {
likes = temp ? .up : .down
} else {
likes = .none
}
linkFlairText = data["link_flair_text"] as? String ?? ""
gilded = data["gilded"] as? Int ?? 0
archived = data["archived"] as? Bool ?? false
clicked = data["clicked"] as? Bool ?? false
author = data["author"] as? String ?? ""
numComments = data["num_comments"] as? Int ?? 0
score = data["score"] as? Int ?? 0
approvedBy = data["approved_by"] as? String ?? ""
over18 = data["over_18"] as? Bool ?? false
hidden = data["hidden"] as? Bool ?? false
thumbnail = convertObjectToEscapedURLString(data["thumbnail"])
subredditId = data["subreddit_id"] as? String ?? ""
edited = data["edited"] as? Bool ?? false
linkFlairCssClass = data["link_flair_css_class"] as? String ?? ""
authorFlairCssClass = data["author_flair_css_class"] as? String ?? ""
downs = data["downs"] as? Int ?? 0
saved = data["saved"] as? Bool ?? false
isSelf = data["is_self"] as? Bool ?? false
let tempName = data["name"] as? String ?? ""
name = tempName.unescapeHTML
permalink = data["permalink"] as? String ?? ""
stickied = data["stickied"] as? Bool ?? false
created = data["created"] as? Int ?? 0
let tempUrl = data["url"] as? String ?? ""
url = tempUrl.unescapeHTML
authorFlairText = data["author_flair_text"] as? String ?? ""
let tempTitle = data["title"] as? String ?? ""
title = tempTitle.unescapeHTML
createdUtc = data["created_utc"] as? Int ?? 0
ups = data["ups"] as? Int ?? 0
upvoteRatio = data["upvote_ratio"] as? Double ?? 0
visited = data["visited"] as? Bool ?? false
numReports = data["num_reports"] as? Int ?? 0
if let distinguishedString = data["distinguished"] as? String {
switch distinguishedString {
case "admin": distinguished = .admin
case "moderator": distinguished = .moderator
case "special": distinguished = .special
default: distinguished = .none
}
} else {
distinguished = .none
}
media = Media(json: data["media"] as? JSONDictionary ?? [:])
mediaEmbed = MediaEmbed(json: data["media_embed"] as? JSONDictionary ?? [:])
userReports = []
secureMedia = nil
reportReasons = []
modReports = []
secureMediaEmbed = nil
}
}