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
#![allow(unknown_lints, wrong_self_convention, new_ret_no_self)]
use client::RedditClient;
use options::{ListingOptions, TimeFilter, LinkPost, SelfPost};
use structures::listing::Listing;
use responses::listing;
use traits::Created;
use errors::APIError;
use structures::listing::PostStream;
/// The `Subreddit` struct represents a subreddit and allows access to post listings
/// and data about the subreddit.
pub struct Subreddit<'a> {
/// The name of the subreddit represented by this struct.
pub name: String,
client: &'a RedditClient,
}
impl<'a> PartialEq for Subreddit<'a> {
fn eq(&self, other: &Subreddit) -> bool {
self.name == other.name
}
}
impl<'a> Subreddit<'a> {
fn get_feed(&self, ty: &str, opts: ListingOptions) -> Result<Listing, APIError> {
// We do not include the after/before parameter here so the pagination can adjust it later
// on.
let uri = format!("/r/{}/{}limit={}", self.name, ty, opts.batch);
let full_uri = format!("{}&{}", uri, opts.anchor);
self.client.get_json::<listing::Listing>(&full_uri, false).and_then(|res| {
Ok(Listing::new(self.client, uri, res.data))
})
}
/// Creates a `Subreddit` from a client and the subreddit's name. Do not use this directly -
/// use `Client.subreddit(NAME)` instead.
pub fn create_new(client: &'a RedditClient, name: &str) -> Subreddit<'a> {
Subreddit {
client: client,
name: name.to_owned()
}
}
/// Gets a listing of the hot feed for this subreddit. The first page may include some sticky
/// posts in addtion to the expected posts.
/// # Examples
/// ```
/// use rawr::client::RedditClient;
/// use rawr::options::ListingOptions;
/// use rawr::auth::AnonymousAuthenticator;
/// let client = RedditClient::new("rawr", AnonymousAuthenticator::new());
/// let sub = client.subreddit("askreddit");
/// let hot = sub.hot(ListingOptions::default());
/// ```
pub fn hot(&self, opts: ListingOptions) -> Result<Listing, APIError> {
self.get_feed("hot?", opts)
}
/// Gets a `PostStream` of the new posts in the subreddit. The iterator will yield values
/// forever, unless it is manually ended at some point. For tips, check the `PostStream` class.
/// # Examples
/// ```rust,no_run
/// use rawr::prelude::*;
/// let client = RedditClient::new("rawr", AnonymousAuthenticator::new());
/// let askreddit = client.subreddit("askreddit");
/// for post in askreddit.new_stream() {
///
/// }
/// ```
pub fn new_stream(self) -> PostStream<'a> {
let url = format!("/r/{}/new?limit=5", self.name);
PostStream::new(&self.client, url)
}
/// Gets a listing of the new feed for this subreddit.
/// # Examples
/// ```
/// use rawr::client::RedditClient;
/// use rawr::options::ListingOptions;
/// use rawr::traits::Content;
/// use rawr::auth::AnonymousAuthenticator;
/// let client = RedditClient::new("rawr", AnonymousAuthenticator::new());
/// let sub = client.subreddit("programming");
/// let mut new = sub.new(ListingOptions::default()).expect("Could not get new feed");
/// assert_eq!(new.next().unwrap().subreddit().name, "programming");
/// ```
pub fn new(&self, opts: ListingOptions) -> Result<Listing, APIError> {
self.get_feed("new?", opts)
}
/// Gets a listing of the rising feed for this subreddit. Usually much shorter than the other
/// listings; may be empty.
/// # Examples
/// ```
/// use rawr::client::RedditClient;
/// use rawr::options::ListingOptions;
/// use rawr::auth::AnonymousAuthenticator;
/// let client = RedditClient::new("rawr", AnonymousAuthenticator::new());
/// let sub = client.subreddit("thanksobama");
/// let rising = sub.rising(ListingOptions::default()).unwrap();
/// assert_eq!(rising.count(), 0);
/// ```
pub fn rising(&self, opts: ListingOptions) -> Result<Listing, APIError> {
self.get_feed("rising?", opts)
}
/// Gets a listing of the top feed for this subreddit. Also requires a time filter (
/// `rawr::options::TimeFilter`) which is equivalent to the "links from: all time" dropdown
/// on the website.
/// # Examples
/// ```
/// use rawr::client::RedditClient;
/// use rawr::options::{ListingOptions, TimeFilter};
/// use rawr::auth::AnonymousAuthenticator;
/// let client = RedditClient::new("rawr", AnonymousAuthenticator::new());
/// let sub = client.subreddit("thanksobama");
/// let mut top = sub.top(ListingOptions::default(), TimeFilter::AllTime)
/// .expect("Request failed");
/// assert_eq!(top.next().unwrap().title(), "Thanks Me");
/// ```
pub fn top(&self, opts: ListingOptions, time: TimeFilter) -> Result<Listing, APIError> {
let path = format!("top?{}&", time);
self.get_feed(&path, opts)
}
/// Gets a listing of the controversial feed for this subreddit. Also requires a time filter (
/// `rawr::options::TimeFilter`) which is equivalent to the "links from: all time" dropdown
/// on the website.
pub fn controversial(&self, opts: ListingOptions, time: TimeFilter) -> Result<Listing, APIError> {
let path = format!("controversial?{}&", time);
self.get_feed(&path, opts)
}
/// Submits a link post to this subreddit using the specified parameters. If the link has
/// already been posted, this will fail unless you specifically allow reposts.
/// # Examples
/// ## Allowing a link to be reposted
/// ```
/// use rawr::options::LinkPost;
/// let post = LinkPost::new("rawr!", "http://example.com").resubmit();
/// ```
/// ## Submitting a post
/// ```rust,no_run
/// use rawr::auth::PasswordAuthenticator;
/// use rawr::client::RedditClient;
/// use rawr::options::LinkPost;
/// let client = RedditClient::new("rawr", PasswordAuthenticator::new("a", "b", "c", "d"));
/// let sub = client.subreddit("rust");
/// let post = LinkPost::new("rawr!", "http://example.com");
/// sub.submit_link(post).expect("Posting failed!");
/// ```
pub fn submit_link(&self, post: LinkPost) -> Result<(), APIError> {
let body = format!("api_type=json&extension=json&kind=link&resubmit={}&sendreplies=true&sr={}&title={}&url={}",
post.resubmit, self.name, self.client.url_escape(post.title.to_owned()), self.client.url_escape(post.link.to_owned()));
self.client.post_success("/api/submit", &body, false)
}
/// Submits a text post (self post) to this subreddit using the specified title and body.
/// # Examples
/// ## Submitting a post
/// ```rust,no_run
/// use rawr::auth::PasswordAuthenticator;
/// use rawr::client::RedditClient;
/// use rawr::options::SelfPost;
/// let client = RedditClient::new("rawr", PasswordAuthenticator::new("a", "b", "c", "d"));
/// let sub = client.subreddit("rust");
/// let post = SelfPost::new("I love rawr!", "You should download it *right now*!");
/// sub.submit_text(post).expect("Posting failed!");
/// ```
pub fn submit_text(&self, post: SelfPost) -> Result<(), APIError> {
let body = format!("api_type=json&extension=json&kind=self&sendreplies=true&sr={}&title={}&text={}",
self.name, self.client.url_escape(post.title), self.client.url_escape(post.text));
self.client.post_success("/api/submit", &body, false)
}
/// Fetches information about a subreddit such as subscribers, active users and sidebar
/// information.
/// # Examples
/// ```
/// use rawr::client::RedditClient;
/// use rawr::auth::AnonymousAuthenticator;
/// let client = RedditClient::new("rawr", AnonymousAuthenticator::new());
/// let learn_programming = client.subreddit("learnprogramming").about()
/// .expect("Could not fetch 'about' data");
/// assert_eq!(learn_programming.display_name(), "learnprogramming");
/// ```
pub fn about(&self) -> Result<SubredditAbout, APIError> {
let url = format!("/r/{}/about", self.name);
self.client.get_json::<listing::SubredditAbout>(&url, false)
.and_then(|res| Ok(SubredditAbout::new(res.data)))
}
}
/// Information about a subreddit such as subscribers, sidebar text and active users.
pub struct SubredditAbout {
data: listing::SubredditAboutData
}
impl Created for SubredditAbout {
fn created(&self) -> i64 {
self.data.created
}
fn created_utc(&self) -> i64 {
self.data.created_utc
}
}
impl SubredditAbout {
/// Creates a new `SubredditAbout` instance. Use `Subreddit.about()` instead to get
/// information about a subreddit.
pub fn new(data: listing::SubredditAboutData) -> SubredditAbout {
SubredditAbout {
data: data
}
}
/// The number of subscribers to this subreddit.
pub fn subscribers(&self) -> u64 {
self.data.subscribers
}
/// The number of logged-in users who have viewed this subreddit in the last 15
/// minutes.
pub fn active_users(&self) -> u64 {
self.data.accounts_active
}
/// Returns `true` if the subreddit is visible to the public (i.e. not invitation only)
pub fn public(&self) -> bool {
self.data.public_traffic
}
/// The display name of the subreddit, not including leading /r/
pub fn display_name(&self) -> &str {
&self.data.display_name
}
}