Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes from #117 #122

Merged
merged 1 commit into from
Apr 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/main/java/com/github/jreddit/action/SubmitActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void switchActor(User new_actor) {
* This function deletes a submission or comment.
*
* @param fullName Full name of the thing
* @param category Category name
*
* @throws ActionFailedException If the action failed
*/
Expand All @@ -64,7 +63,7 @@ public boolean delete(String fullName) throws ActionFailedException {
/**
* This function comments on a submission or comment with the given full name.
*
* @param fullName Full name of the submission or comment
* @param fullname Full name of the submission or comment
* @param text The text to comment (can include markdown)
*/
public boolean comment(String fullname, String text) throws ActionFailedException {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/github/jreddit/captcha/Captcha.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class Captcha {

/**
* Constructor.
* @param restClient
* @param captchaDownloader
* @param restClient RestClient
*/
public Captcha(RestClient restClient) {
this.restClient = restClient;
Expand Down
76 changes: 39 additions & 37 deletions src/main/java/com/github/jreddit/retrieval/Comments.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,19 @@ public List<Comment> parseBreadth(String url) throws RetrievalFailedException, R

// Make sure it is of the correct kind
String kind = safeJsonToString(data.get("kind"));
if (kind.equals(Kind.COMMENT.value())) {

// Contents of the comment
data = ((JSONObject) data.get("data"));

// Create and add the new comment
comment = new Comment(data);
comments.add(comment);

}
if (kind != null) {
if (kind.equals(Kind.COMMENT.value())) {

}
// Contents of the comment
data = ((JSONObject) data.get("data"));

// Create and add the new comment
comment = new Comment(data);
comments.add(comment);

}
}
}

} else {
throw new IllegalArgumentException("Parsing failed because JSON is not from a shallow comment listing.");
Expand All @@ -128,7 +129,6 @@ public List<Comment> parseBreadth(String url) throws RetrievalFailedException, R
* maintaining the order. This parses all comments that are defined with their associated values,
* those that fall outside the (default) limit are omitted.
*
* @param user User
* @param url URL for the request
*
* @return Parsed list of comments.
Expand Down Expand Up @@ -180,33 +180,35 @@ protected void parseRecursive(List<Comment> comments, JSONObject object) throws

// Make sure it is of the correct kind
String kind = safeJsonToString(data.get("kind"));
if (kind.equals(Kind.COMMENT.value())) {

// Contents of the comment
data = ((JSONObject) data.get("data"));

// Create and add the new comment
comment = new Comment(data);
comments.add(comment);

Object o = data.get("replies");
if (o instanceof JSONObject) {

// Dig towards the replies
JSONObject replies = (JSONObject) o;
parseRecursive(comment.getReplies(), replies);
if (kind != null) {
if (kind.equals(Kind.COMMENT.value())) {

}

} else if (kind.equals(Kind.MORE.value())) {

//data = (JSONObject) data.get("data");
//JSONArray children = (JSONArray) data.get("children");
//System.out.println("\t+ More children: " + children);

}
// Contents of the comment
data = ((JSONObject) data.get("data"));

}
// Create and add the new comment
comment = new Comment(data);
comments.add(comment);

Object o = data.get("replies");
if (o instanceof JSONObject) {

// Dig towards the replies
JSONObject replies = (JSONObject) o;
parseRecursive(comment.getReplies(), replies);

}

} else if (kind.equals(Kind.MORE.value())) {

//data = (JSONObject) data.get("data");
//JSONArray children = (JSONArray) data.get("children");
//System.out.println("\t+ More children: " + children);

}
}

}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public ExtendedComments(Comments comments) {
* The result is maximum amount_first_depth * RedditConstants.MAX_LIMIT_COMMENTS
* Which can be quite alot (e.g. 500x500 = 250000 comments)
*
* @param user (Optional, set null if not used) The user as whom to retrieve the comments
* @param submissionId Submission ID36 identifier
* @param sort (Optional, set null if not used) CommentSort enum indicating the type of sorting to be applied (e.g. HOT, NEW, TOP, etc)
* @param amount_first_depth (Optional, set -1 if not used) Integer representing the amount of first depth comments to retrieve
Expand Down Expand Up @@ -64,7 +63,6 @@ public List<Comment> ofSubmission(String submissionId, CommentSort sort, int amo
/**
* Get the comment list of the given user (username).
*
* @param user (Optional, set null if not used) The user as whom to retrieve the comments
* @param username Username of the user you want to retrieve from.
* @param sort (Optional, set null if not used) Sorting method.
* @param time (Optional, set null is not used) Time window
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public List<Submission> get(String redditName, SubmissionSort sort, int amount,
/**
* Get submissions from the specified subreddit, as the specified user, using the given sorting method.
*
* @param user User
* @param redditName The subreddit at which submissions you want to retrieve submissions.
* @param sort Subreddit sorting method
* @return <code>List</code> of submissions on the subreddit.
Expand Down Expand Up @@ -203,10 +202,9 @@ public List<Submission> search(String query, SearchSort sort, TimeSpan time) {
/**
* Get submissions from the specified user.
*
* @param query Search query
* @param category Category
* @param username Username
* @param category UserSubmissionsCategory
* @param sort Search sorting method (e.g. new or top)
* @param time Search time (e.g. day or all)
* @param amount Desired amount which will be attempted. No guarantee! See request limits.
* @param after Submission after which the submissions need to be fetched.
* @return List of the submissions
Expand Down Expand Up @@ -262,12 +260,10 @@ public List<Submission> ofUser(String username, UserSubmissionsCategory category
/**
* Get submissions from the specified user.
*
* @param query Search query
* @param category Category
* @param username Username
* @param category UserSubmissionsCategory
* @param sort Search sorting method (e.g. new or top)
* @param time Search time (e.g. day or all)
* @param amount Desired amount which will be attempted. No guarantee! See request limits.
* @param after Submission after which the submissions need to be fetched.
* @return List of the submissions
*/
public List<Submission> ofUser(String username, UserSubmissionsCategory category, UserOverviewSort sort, int amount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public List<Subreddit> search(String query, int amount, Subreddit after) {
* Search for the subreddit as the given user, using the given query, for the given amount.
* This concatenates several requests to reach the amount.
*
* @param user User
* @param query Query (only simple, Reddit does not yet support field search for subreddits)
* @param amount Amount to retrieve (result <= amount)
* @return List of subreddits matching the search query
Expand Down Expand Up @@ -169,7 +168,6 @@ public List<Subreddit> get(SubredditsView type) {
/**
* Get a Subreddit by its name as a given user.
*
* @param user User
* @param subredditName Name of the subreddit to retrieve
* @return Subreddit object representing the desired subreddit, or NULL if it does not exist
*/
Expand Down
32 changes: 15 additions & 17 deletions src/main/java/com/github/jreddit/retrieval/Submissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ public List<Submission> parse(String url) throws RetrievalFailedException, Reddi

// Make sure it is of the correct kind
String kind = safeJsonToString(data.get("kind"));
if (kind.equals(Kind.LINK.value())) {
// Create and add submission
data = ((JSONObject) data.get("data"));
submission = new Submission(data);
submission.setUser(user);
submissions.add(submission);
}
}
if (kind != null) {
if (kind.equals(Kind.LINK.value())) {

// Create and add submission
data = ((JSONObject) data.get("data"));
submission = new Submission(data);
submission.setUser(user);
submissions.add(submission);
}
}
}

} else {
System.err.println("Cannot cast to JSON Object: '" + response.toString() + "'");
Expand All @@ -142,7 +142,7 @@ public List<Submission> parse(String url) throws RetrievalFailedException, Reddi
* @param limit Maximum amount of submissions that can be returned (0-100, 25 default (see Reddit API))
* @param after The submission after which needs to be retrieved
* @param before The submission before which needs to be retrieved
* @param show_all Show all (disables filters such as "hide links that I have voted on")
* @param show Show all (disables filters such as "hide links that I have voted on")
* @return The linked list containing submissions
*/
protected List<Submission> ofSubreddit(String subreddit, String sort, String count, String limit, String after, String before, String show) throws RetrievalFailedException, RedditError {
Expand Down Expand Up @@ -212,7 +212,7 @@ public List<Submission> ofSubreddit(String subreddit, SubmissionSort sort, int c
* @param limit Maximum amount of submissions that can be returned (0-100, 25 default (see Reddit API))
* @param after The submission after which needs to be retrieved
* @param before The submission before which needs to be retrieved
* @param show_all Show all (disables filters such as "hide links that I have voted on")
* @param show Show all (disables filters such as "hide links that I have voted on")
* @return The linked list containing submissions
*/
protected List<Submission> search(String query, String syntax, String sort, String time, String count, String limit, String after, String before, String show) throws RetrievalFailedException, RedditError {
Expand Down Expand Up @@ -283,7 +283,6 @@ public List<Submission> search(String query, QuerySyntax syntax, SearchSort sort
* @param username Username of the user you want to retrieve from.
* @param category (Optional, set null/empty if not used) Category in the user overview to retrieve submissions from
* @param sort (Optional, set null/empty if not used) Sorting method.
* @param time (Optional, set null/empty is not used) Time window
* @param count (Optional, set null/empty if not used) Number at which the counter starts
* @param limit (Optional, set null/empty if not used) Integer representing the maximum number of comments to return
* @param after (Optional, set null/empty if not used) After which comment to retrieve
Expand Down Expand Up @@ -315,12 +314,11 @@ protected List<Submission> ofUser(String username, String category, String sort,
* @param username Username of the user you want to retrieve from.
* @param category Category in the user overview to retrieve submissions from
* @param sort (Optional, set null if not used) Sorting method.
* @param time (Optional, set null is not used) Time window
* @param count (Optional, set -1 if not used) Number at which the counter starts
* @param limit (Optional, set -1 if not used) Integer representing the maximum number of comments to return
* @param after (Optional, set null if not used) After which comment to retrieve
* @param before (Optional, set null if not used) Before which comment to retrieve
* @param show (Optional, set false if not used) Show parameter ('given' is only acceptable value)
* @param show_given (Optional, set false if not used) Show parameter ('given' is only acceptable value)
*
* @return Submissions of a user.
*/
Expand All @@ -340,7 +338,7 @@ public List<Submission> ofUser(String username, UserSubmissionsCategory category

return ofUser(
username,
(category != null) ? category.value() : "",
category.value(),
(sort != null) ? sort.value() : "",
String.valueOf(count),
String.valueOf(limit),
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/com/github/jreddit/retrieval/Subreddits.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public void switchActor(User new_actor) {
/**
* Parses a JSON feed from the Reddit (URL) into a nice list of Subreddit objects.
*
* @param user User
* @param url URL
* @return Listing of submissions
*/
Expand All @@ -99,16 +98,15 @@ public List<Subreddit> parse(String url) throws RetrievalFailedException, Reddit

// Make sure it is of the correct kind
String kind = safeJsonToString(data.get("kind"));
if (kind.equals(Kind.SUBREDDIT.value())) {

// Create and add subreddit
data = ((JSONObject) data.get("data"));
subreddits.add(new Subreddit(data));

}

}

if (kind != null) {
if (kind.equals(Kind.SUBREDDIT.value())) {

// Create and add subreddit
data = ((JSONObject) data.get("data"));
subreddits.add(new Subreddit(data));
}
}
}
} else {
System.err.println("Cannot cast to JSON Object: '" + response.toString() + "'");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Arrays;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
Expand All @@ -20,6 +19,7 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.simple.parser.ParseException;

import com.github.jreddit.exception.RetrievalFailedException;
Expand All @@ -45,14 +45,14 @@ public class HttpRestClient implements RestClient {
* Response handler instance.
*/
private final ResponseHandler<Response> responseHandler;

/**
* Global request configuration.
*/
private final RequestConfig globalConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.setConnectionRequestTimeout(10000).build();

/**
* Default User Agent
*/
Expand Down Expand Up @@ -153,7 +153,7 @@ public Response post(HttpPostMethodBuilder postMethodBuilder, NameValuePair... p
}

public Response post(HttpPostMethodBuilder postMethodBuilder, List<NameValuePair> params) throws IOException, ParseException {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);

// Assign user agent
postMethodBuilder.withUserAgent(userAgent);
Expand Down