Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.github.jreddit.submissions;
  
 
This class represents a vote on a link submission on reddit.

Author(s):
Omer Elnour
 
 public class Submission extends Thing {
 
     /* This is the user that will vote on a submission. */
     private User user;
 
     /* The path to this submission */
     private String url;
 
     private double createdUTC;
     private String author;
     private String title;
     private Boolean nsfw;
     private long commentCount;
     private String subreddit;
     private long upVotes;
     private long downVotes;
     private long score;
 
     public Submission() {
     }

    
Create a Submission from a JSONObject

Parameters:
obj The JSONObject to load Submission data from
 
     public Submission(JSONObject obj) {
 
         try {
             setAuthor((Stringobj.get("author"));
             setTitle((Stringobj.get("title"));
             setNSFW((Booleanobj.get("over_18"));
             setCreatedUTC((Doubleobj.get("created_utc"));
             setDownVotes((Longobj.get("downs"));
             setFullName((Stringobj.get("name"));
             setScore((Longobj.get("score"));
             setUpVotes((Longobj.get("ups"));
             setCommentCount((Longobj.get("num_comments"));
             setUrl((Stringobj.get("url"));
         } catch (Exception e) {
             ..println("Error creating Submission");
         }
     }
 
     public void setUpVotes(long upVotes) {
         this. = upVotes;
     }
 
     public void setScore(long score) {
         this. = score;
     }
 
     public void setAuthor(String author) {
         this. = author;
     }
 
     public void setCreatedUTC(double createdUTC) {
         this. = createdUTC;
     }
 
     public void setDownVotes(long downVotes) {
         this. = downVotes;
     }
 
     public void setCommentCount(long commentCount) {
         this. = commentCount;
     }
 
     public void setNSFW(boolean nsfw) {
         this. = nsfw;
     }
 
     public void setSubreddit(String subreddit) {
         this. = subreddit;
     }
 
     public void setTitle(String title) {
         this. = title;
     }
 
     public void setUrl(String url) {
        this. = url;
    }
    public void setFullName(String fullName) {
        this. = fullName;
    }
    public User getUser() {
        return ;
    }
    public void setUser(User user) {
        this. = user;
    }
    public String getFullName() {
        return ;
    }
    public String getURL() {
        return ;
    }
    public Submission(User userString fullNameString url) {
        // TODO: Refactor this to use TypePrefix enum
        if (fullName.startsWith("t3_"))
            fullName = fullName.replaceFirst("t3_""");
        this. = user;
        this. = "t3_" + fullName;
        this. = url;
    }

    
This function comments on this submission saying the comment specified in text (CAN INCLUDE MARKDOWN)

Parameters:
text The text to comment
Throws:
java.io.IOException If connection fails
org.json.simple.parser.ParseException If JSON parsing fails
    public void comment(String textthrows IOExceptionParseException {
        JSONObject object = Utils.post("thing_id=" +  + "&text=" + text
                + "&uh=" + .getModhash(), "/api/comment".getCookie());
        if (object.toJSONString().contains(".error.USER_REQUIRED"))
            throw new InvalidCookieException("Cookie not present");
        else
            ..println("Commented on thread id " + 
                    + " saying: \"" + text + "\"");
    }

    
This function returns the name of the author of this submission.

Returns:
The author's name
Throws:
java.io.IOException If connection fails
org.json.simple.parser.ParseException If JSON parsing fails
    public String getAuthor() throws IOExceptionParseException {
        if ( != null) {
            return ;
        }
        if ( == null)
            throw new IOException("URL needs to be present");
        return info().get("author").toString();
    }

    
This function returns the title of this submission.

Returns:
The title
Throws:
java.io.IOException If connection fails
org.json.simple.parser.ParseException If JSON parsing fails
    public String getTitle() throws IOExceptionParseException {
        if ( != null) {
            return ;
        }
        if ( == null)
            throw new IOException("URL needs to be present");
        return info().get("title").toString();
    }

    
This function returns the name of the subreddit that this submission was submitted to.

Returns:
The name of the subreddit that this submission was submitted to
Throws:
java.io.IOException If the connection fails
org.json.simple.parser.ParseException If JSON parsing fails
    public String getSubreddit() throws IOExceptionParseException {
        if ( != null) {
            return ;
        }
        if ( == null)
            throw new IOException("URL needs to be present");
        return info().get("subreddit").toString();
    }

    
This function returns the score of this sumbission (issues a new GET request to Reddit.com).

Returns:
The score of this submission
Throws:
java.io.IOException If the connection fails
org.json.simple.parser.ParseException If the JSON parsing fails
    public int score() throws IOExceptionParseException {
        if ( == null)
            throw new IOException("URL needs to be present");
        return Integer.parseInt(info().get("score").toString());
    }

    
This function returns the number of upvotes of this sumbission.

Returns:
The number of upvotes of this submission
Throws:
java.io.IOException If the connection fails
org.json.simple.parser.ParseException If the JSON parsing fails
    public int upVotes() throws IOExceptionParseException {
        if ( == null)
            throw new IOException("URL needs to be present");
        return Integer.parseInt(info().get("ups").toString());
    }

    
This function returns the number of downvotes of this sumbission.

Returns:
The number of downvotes of this submission
Throws:
java.io.IOException If the connection fails
org.json.simple.parser.ParseException If the JSON parsing fails
    public int downVotes() throws IOExceptionParseException {
        if ( == null)
            throw new IOException("URL needs to be present");
        return Integer.parseInt(info().get("downs").toString());
    }

    
This function returns true if this submission is marked as NSFW (18+)

Returns:
This submission is NSFW
Throws:
java.io.IOException If connection fails
org.json.simple.parser.ParseException If JSON parsing fails
    public boolean isNSFW() throws IOExceptionParseException {
        if (this. == null) {
            if ( == null)
                throw new IOException("URL needs to be present");
            return Boolean.parseBoolean(info().get("over_18").toString());
        } else {
            return this.;
        }
    }
    public void markNSFW() throws IOExceptionParseException {
        Utils.post(
                "id=" +  + "&uh=" + .getModhash(),
                "/api/marknsfw".getCookie());
    }
    public void unmarkNSFW() throws IOExceptionParseException {
        Utils.post(
                "id=" +  + "&uh=" + .getModhash(),
                "/api/unmarknsfw".getCookie());
    }

    
This function returns true if this submission is a self-post

Returns:
This submission is a self post
Throws:
java.io.IOException If connection fails
org.json.simple.parser.ParseException If JSON parsing fails
    public boolean isSelfPost() throws IOExceptionParseException {
        if ( == null)
            throw new IOException("URL needs to be present");
        return Boolean.parseBoolean(info().get("is_self").toString());
    }

    
This function returns the number of comments in this sumbission.

Returns:
The number of comments in this submission
Throws:
java.io.IOException If the connection fails
org.json.simple.parser.ParseException If the JSON parsing fails
    public int commentCount() throws IOExceptionParseException {
        if ( == null)
            throw new IOException("URL needs to be present");
        return Integer.parseInt(info().get("num_comments").toString());
    }

    
This function upvotes this submission.

Throws:
java.io.IOException If connection fails
org.json.simple.parser.ParseException If JSON parsing fails
    public void upVote() throws IOExceptionParseException {
        JSONObject object = voteResponse(1);
        if (!(object.toJSONString().length() > 2))
            // Will return "{}"
            ..println("Successful upboat!");
        else
            ..println(object.toJSONString());
    }

    
This function rescinds, or normalizes this submission.
(i.e Removes a downvote or upvote)

Throws:
java.io.IOException If connection fails
org.json.simple.parser.ParseException If JSON parsing fails
    public void rescind() throws IOExceptionParseException {
        JSONObject object = voteResponse(0);
        if (!(object.toJSONString().length() > 2))
            // Will return "{}"
            ..println("Successful rescind!");
        else
            ..println(object.toJSONString());
    }

    
This function downvotes this submission.

Throws:
java.io.IOException If connection fails
org.json.simple.parser.ParseException If JSON parsing fails
    public void downVote() throws IOExceptionParseException {
        JSONObject object = voteResponse(-1);
        if (!(object.toJSONString().length() > 2))
            // Will return "{}"
            ..println("Successful downvote!");
        else
            ..println(object.toJSONString());
    }
    private JSONObject voteResponse(int dirthrows IOExceptionParseException {
        return Utils.post(
                "id=" +  + "&dir=" + dir + "&uh=" + .getModhash(),
                "/api/vote".getCookie());
    }
    private JSONObject info(String urlPaththrows IOExceptionParseException {
        urlPath += "/info.json";
        Object object = Utils.get(urlPath.getCookie());
        JSONArray array = (JSONArrayobject;
        JSONObject obj = (JSONObjectarray.get(0);
        obj = (JSONObjectobj.get("data");
        array = (JSONArrayobj.get("children");
        obj = (JSONObjectarray.get(0);
        return (JSONObjectobj.get("data");
    }
    public double getCreatedUTC() throws IOExceptionParseException {
         = Double.parseDouble(info().get("created_utc").toString());
        return ;
    }
//    @Override
//    public String toString() {
//        try {
//            return "(" + score() + ") " + getTitle();
//        } catch (Exception exception) {
//            exception.printStackTrace();
//        }
//
//        return super.toString();
//    }
New to GrepCode? Check out our FAQ X