diff --git a/examples/GetPaste.java b/examples/GetPaste.java new file mode 100644 index 0000000..50c2526 --- /dev/null +++ b/examples/GetPaste.java @@ -0,0 +1,18 @@ +import org.jpaste.pastebin.Pastebin; +import org.jpaste.pastebin.PastebinLink; +import org.jpaste.pastebin.exceptions.ParseException; +/** + * This class shows how to use getPaste. + * In order to get a paste, it's necessary to have the IP white-listed. + * See more at Pastebin Scraping API + * + * @author Felipe + */ +public class GetPaste { + public static void main(String[] args) throws ParseException { + String key = "pastebinKey"; + PastebinLink pastebinLink = Pastebin.getPaste(key); + pastebinLink.fetchContent(); + System.out.println(pastebinLink.getPaste().getContents()); + } +} \ No newline at end of file diff --git a/src/main/java/org/jpaste/pastebin/Pastebin.java b/src/main/java/org/jpaste/pastebin/Pastebin.java index c3738fe..82aeafb 100644 --- a/src/main/java/org/jpaste/pastebin/Pastebin.java +++ b/src/main/java/org/jpaste/pastebin/Pastebin.java @@ -22,6 +22,7 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; /** @@ -44,11 +45,14 @@ public class Pastebin { * Used for fetching an user session id */ public static final String API_LOGIN_LINK = "https://pastebin.com/api/api_login.php"; - /** * Scraping api. Must be lifetime pro in otrder to use it. */ - public static final String API_SCRAPING_LINK = "https://pastebin.com/api_scraping.php"; + public static final String API_SCRAPING_LINK = "https://scrape.pastebin.com/api_scraping.php"; + /** + * Scrape paste metadata link. Must be pro lifetime in order to use it. + */ + public static final String SCRAPE_PASTE_METADATA_URL = "https://scrape.pastebin.com/api_scrape_item_meta.php"; /** * Fetches a paste text from pastebin @@ -195,22 +199,27 @@ public static PastebinLink[] getTrending(String developerKey) throws ParseExcept } - throw new ParseException("Failed to parse pastes: " + response); + throw new ParseException("Failed to parse paste: " + response); } /** * Gets the most recent pastes. In order to use it, it's necessary to have a - * lifetime pro account and white-list your IP. Se more on + * pro account and white-list + * your IP. Se more on * Pastebin Scraping Api - * - * Note: unfortunably, it's not possible to get the number of hits. - * - * The options for the post are: limit: up to 500. default is 50. lang: any - * of the syntaxes allowed for Pastebin. - * + * + * The options for the post are: + * + * + * @author Felipe * @param post * the Post with the options * @return the pastes. + * @throws ParseException */ public static PastebinLink[] getMostRecent(Post post) throws ParseException { String url = API_SCRAPING_LINK; @@ -221,32 +230,17 @@ public static PastebinLink[] getMostRecent(Post post) throws ParseException { String response = Web.getContents(url); if (response == null || response.isEmpty() - || !(response.charAt(0) == '[' && response.charAt(response.length() - 2) == ']')) { + || !(response.charAt(0) == '[' && response.charAt(response.length() - 1) == ']')) { throw new ParseException("Failed to parse pastes: " + response); } - - ArrayList listData = getJSonData(response); - - ArrayList listPastebinLink = new ArrayList<>(listData.size()); - for (Object object : listData) { - Map tempMap = (Map) object; - PastebinPaste pastebinPaste = new PastebinPaste(); - pastebinPaste.setPasteFormat(tempMap.get("syntax").toString()); - String pasteTitle = tempMap.get("title").toString(); - pastebinPaste.setPasteTitle(pasteTitle == null ? "" : pasteTitle); - long pasteExpireDate = Long.parseLong(tempMap.get("expire").toString()); - long pasteDate = Long.parseLong(tempMap.get("date").toString()); - pastebinPaste.setPasteExpireDate(pasteExpireDate == 0L ? PasteExpireDate.NEVER - : PasteExpireDate.getExpireDate((int) (pasteExpireDate - pasteDate))); - pastebinPaste.setVisibility(PastebinPaste.VISIBILITY_PUBLIC); - // All the pastes retrieved from this api are public. - + ArrayList> listData = getListJSonData(response); + ArrayList listPastebinLink = new ArrayList(); + for (Map tempMap : listData) { PastebinLink pastebinLink = null; try { - pastebinLink = new PastebinLink(pastebinPaste, new URL(tempMap.get("full_url").toString()), - new Date(pasteDate * 1000)); + pastebinLink = jSonMapToPastebinLink(tempMap); } catch (MalformedURLException e) { - e.printStackTrace(); + } if (pastebinLink != null) { @@ -257,16 +251,99 @@ public static PastebinLink[] getMostRecent(Post post) throws ParseException { return listPastebinLink.toArray(new PastebinLink[listPastebinLink.size()]); } - private static ArrayList getJSonData(String response) { + /** + * Converts the Map with the json informations into a + * PastebinLink. + * + * Important: can't retrieve visibility and, once most of the technics from + * API only alow parse public pasts, the resultant paste is tagged as + * Public. + * + * @author Felipe + * @param tempMap + * the Json Map + * @return PastebinLink with all the informations + * @throws MalformedURLException + * if there are problems with the paste's url + */ + private static PastebinLink jSonMapToPastebinLink(Map tempMap) throws MalformedURLException { + PastebinPaste pastebinPaste = new PastebinPaste(); + pastebinPaste.setPasteFormat(tempMap.get("syntax").toString()); + String pasteTitle = tempMap.get("title").toString(); + pastebinPaste.setPasteTitle(pasteTitle == null ? "" : pasteTitle); + String pasteAuthor = tempMap.get("user").toString(); + pastebinPaste.setPasteAuthor(pasteAuthor == null ? "" : pasteAuthor); + long pasteExpireDate = Long.parseLong(tempMap.get("expire").toString()); + long pasteDate = Long.parseLong(tempMap.get("date").toString()); + pastebinPaste.setPasteExpireDate(pasteExpireDate == 0L ? PasteExpireDate.NEVER + : PasteExpireDate.getExpireDate((int) (pasteExpireDate - pasteDate))); + pastebinPaste.setVisibility(PastebinPaste.VISIBILITY_PUBLIC); + // All the pastes retrieved from this api are public. + + PastebinLink pastebinLink = null; + try { + pastebinLink = new PastebinLink(pastebinPaste, new URL(tempMap.get("full_url").toString()), + new Date(pasteDate * 1000)); + Object hits = tempMap.get("hits"); + String hitsText = hits == null ? "" : hits.toString(); + pastebinLink.setHits(!hitsText.isEmpty() ? Integer.parseInt(hitsText) : 0); + } catch (MalformedURLException e) { + throw e; + } + return pastebinLink; + } + + /** + * Returns the JSON response as a ArrayList of /code>Map + * + * @author Felipe + * @param response + * the pastebin JSON response + * @return the list of maps with the content + */ + private static ArrayList> getListJSonData(String response) { ObjectMapper mapper = new ObjectMapper(); try { - ArrayList data = mapper.readValue(response, ArrayList.class); + ArrayList> data = mapper.readValue(response, + new TypeReference>>() { + }); return data; } catch (IOException e) { - e.printStackTrace(); } return null; } -} + /** + * Get all available informations from a paste, not only its contents, + * generating a PastebinLink. Part of the + * Scraping API. Must be + * Pro user to use. The pastebinKey + * is the 7-8 size identifier of any paste. + * + * @author Felipe + * @param key + * the pastebin key + * @return the PastebinLink with the informations. + * @throws ParseException + */ + public static PastebinLink getPaste(String key) throws ParseException { + String url = SCRAPE_PASTE_METADATA_URL + key; + String response = Web.getContents(url); + + if (response == null || response.isEmpty() + || !(response.charAt(0) == '[' && response.charAt(response.length() - 1) == ']')) { + throw new ParseException("Failed to parse paste: " + response); + } + + ArrayList> listData = getListJSonData(response); + + Map tempMap = listData.get(0); + PastebinLink pastebinLink = null; + try { + pastebinLink = jSonMapToPastebinLink(tempMap); + } catch (MalformedURLException e) { + } + return pastebinLink; + } +} \ No newline at end of file diff --git a/src/main/java/org/jpaste/pastebin/PastebinPaste.java b/src/main/java/org/jpaste/pastebin/PastebinPaste.java index 737c309..65f710d 100644 --- a/src/main/java/org/jpaste/pastebin/PastebinPaste.java +++ b/src/main/java/org/jpaste/pastebin/PastebinPaste.java @@ -1,312 +1,323 @@ -package org.jpaste.pastebin; - -import java.net.MalformedURLException; -import java.net.URL; - -import org.jpaste.AbstractPaste; -import org.jpaste.exceptions.PasteException; -import org.jpaste.pastebin.account.PastebinAccount; -import org.jpaste.utils.web.Post; -import org.jpaste.utils.web.Web; - -/** - * - * A representation of a new or existing paste. - * - *

- * This class holds the contents of the paste itself. You can get and modify - * settings and then 'push' this paste onto pastebin. - *

- * - *

- * This class has been programmed with the help of the pastebin API manual. - *

- * - * @author Brian B - * - */ -public class PastebinPaste extends AbstractPaste { - /** - * Makes a paste public. - */ - public static final int VISIBILITY_PUBLIC = 0; - /** - * Makes a paste unlisted. - */ - public static final int VISIBILITY_UNLISTED = 1; - /** - * Makes a paste private. - *

- * Requires an {@link PastebinAccount - * org.jpaste.pastebin.account.PastebinAccount PastebinAccount} - *

- */ - public static final int VISIBILITY_PRIVATE = 2; - private String developerKey; - private PastebinAccount account; - private String pasteTitle; - private String pasteFormat; - private PasteExpireDate expireDate; - private int visibility; - - /** - * Creates a new empty PastebinPaste instance. - */ - public PastebinPaste() { - this(null, null, null); - } - - /** - * Creates a new PastebinPaste instance. - * - * @param contents - * the paste contents - */ - public PastebinPaste(String contents) { - this(null, contents, null); - } - - /** - * Creates a new PastebinPaste instance. - * - * @param account - * a pastebin account - */ - public PastebinPaste(PastebinAccount account) { - this(account.getDeveloperKey(), null, account); - } - - /** - * Creates a new PastebinPaste instance. - * - * @param developerKey - * a developer key which can be fetched from the pastebin API - * page - * @param contents - * the contents of the paste - */ - public PastebinPaste(String developerKey, String contents) { - this(developerKey, contents, null); - } - - /** - * Creates a new PastebinPaste instance. - * - * @param developerKey - * a developer key which can be fetched from the pastebin API - * page - * @param contents - * the contents of the paste - * @param account - * a pastebin account - */ - public PastebinPaste(String developerKey, String contents, - PastebinAccount account) { - super(contents); - this.developerKey = developerKey; - this.account = account; - } - - /** - * Sets the pastebin account If you set an account the pastes will be listed - * on your account. - * - * @param account - * a pastebin account - */ - public void setAccount(PastebinAccount account) { - this.account = account; - } - - /** - * Gets the pastebin account - * - * @return pastebin account - */ - public PastebinAccount getAccount() { - return this.account; - } - - /** - * Sets the developer key The developer key is required to paste contents on - * pastebin - * - * @param developerKey - * a developer key which can be fetched from the pastebin API - * page - */ - public void setDeveloperKey(String developerKey) { - if (developerKey == null || developerKey.isEmpty()) { - throw new IllegalArgumentException( - "Developer key can not be null or empty."); - } - this.developerKey = developerKey; - } - - /** - * Sets the paste expire date - * - * @param date - * date when the paste will be removed - */ - public void setPasteExpireDate(PasteExpireDate date) { - this.expireDate = date; - } - - /** - * Gets the developer key - * - * @return developer key - */ - public String getDeveloperKey() { - return this.developerKey; - } - - /** - * Sets the paste title - * - * @param title - * title of the paste - */ - public void setPasteTitle(String title) { - this.pasteTitle = title; - } - - /** - * Gets paste title - * - * @return paste title - */ - public String getPasteTitle() { - return this.pasteTitle; - } - - /** - * Gets paste expire date - * @return paste expire date - */ - public PasteExpireDate getPasteExpireDate() { - return this.expireDate; - } - - /** - * Sets the paste format The format is used for syntax highlighting - * - * @see available syntax highlighting - * formats - * @param format - * format of the paste - */ - public void setPasteFormat(String format) { - this.pasteFormat = format; - } - - /** - * Gets paste format - * - * @return paste format - */ - public String getPasteFormat() { - return this.pasteFormat; - } - - /** - * Makes this paste private, unlisted or public Default visibility is public - *

- * Valid visibilities - *

- *

- * {@link PastebinPaste#VISIBILITY_PUBLIC} - *

- *

- * {@link PastebinPaste#VISIBILITY_UNLISTED} - *

- *

- * {@link PastebinPaste#VISIBILITY_PRIVATE} - *

- * - * @param visibility - * the paste it's visibility - */ - public void setVisibility(int visibility) { - if (visibility < 0 || visibility > 2) { - throw new IllegalArgumentException("Unknown visibility: " - + visibility); - } - this.visibility = visibility; - } - - /** - * Gets this paste visibility - *

- * Valid visibilities - *

- *

- * {@link PastebinPaste#VISIBILITY_PUBLIC} - *

- *

- * {@link PastebinPaste#VISIBILITY_UNLISTED} - *

- *

- * {@link PastebinPaste#VISIBILITY_PRIVATE} - *

- * - * @return visibility of this paste - */ - public int getVisibility() { - return this.visibility; - } - - /** - * {@inheritDoc} - */ - @Override - public PastebinLink paste() throws PasteException { - if (getContents() == null || getContents().isEmpty()) { - throw new IllegalStateException("Paste can not be null or empty."); - } - if (getDeveloperKey() == null || getDeveloperKey().isEmpty()) { - throw new IllegalStateException("Developer key is missing."); - } - - Post post = new Post(); - - // required parameters - post.put("api_dev_key", getDeveloperKey()); - post.put("api_option", "paste"); - post.put("api_paste_code", getContents()); - - // optional parameters - if (this.account != null && this.account.getUserSessionId() != null) { - post.put("api_user_key", this.account.getUserSessionId()); - } - if (this.pasteTitle != null) { - post.put("api_paste_name", getPasteTitle()); - } - if (this.pasteFormat != null) { - post.put("api_paste_format", getPasteFormat()); - } - post.put("api_paste_private", Integer.toString(getVisibility())); - if (this.expireDate != null) { - post.put("api_paste_expire_date", expireDate.getValue()); - } - - try { - String pageResponse = Web.getContents(Pastebin.API_POST_LINK, post); - if (pageResponse.startsWith("http")) { - // success - PastebinLink result = new PastebinLink(this, new URL( - pageResponse)); - return result; - } - throw new PasteException("Failed to generate paste: " - + pageResponse); - } catch (MalformedURLException e) { - // shouldn't happen - throw new PasteException("Failed to generate paste: " + e); - } - } - -} +package org.jpaste.pastebin; + +import java.net.MalformedURLException; +import java.net.URL; + +import org.jpaste.AbstractPaste; +import org.jpaste.exceptions.PasteException; +import org.jpaste.pastebin.account.PastebinAccount; +import org.jpaste.utils.web.Post; +import org.jpaste.utils.web.Web; + +/** + * + * A representation of a new or existing paste. + * + *

+ * This class holds the contents of the paste itself. You can get and modify + * settings and then 'push' this paste onto + * pastebin. + *

+ * + *

+ * This class has been programmed with the help of the + * pastebin API manual. + *

+ * + * @author Brian B + * + */ +public class PastebinPaste extends AbstractPaste { + /** + * Makes a paste public. + */ + public static final int VISIBILITY_PUBLIC = 0; + /** + * Makes a paste unlisted. + */ + public static final int VISIBILITY_UNLISTED = 1; + /** + * Makes a paste private. + *

+ * Requires an {@link PastebinAccount + * org.jpaste.pastebin.account.PastebinAccount PastebinAccount} + *

+ */ + public static final int VISIBILITY_PRIVATE = 2; + private String developerKey; + private PastebinAccount account; + private String pasteTitle; + /** + * @return the pasteAuthor + */ + public String getPasteAuthor() { + return pasteAuthor; + } + + /** + * @param pasteAuthor the pasteAuthor to set + */ + public void setPasteAuthor(String pasteAuthor) { + this.pasteAuthor = pasteAuthor; + } + + private String pasteAuthor; + private String pasteFormat; + private PasteExpireDate expireDate; + private int visibility; + + /** + * Creates a new empty PastebinPaste instance. + */ + public PastebinPaste() { + this(null, null, null); + } + + /** + * Creates a new PastebinPaste instance. + * + * @param contents + * the paste contents + */ + public PastebinPaste(String contents) { + this(null, contents, null); + } + + /** + * Creates a new PastebinPaste instance. + * + * @param account + * a pastebin account + */ + public PastebinPaste(PastebinAccount account) { + this(account.getDeveloperKey(), null, account); + } + + /** + * Creates a new PastebinPaste instance. + * + * @param developerKey + * a developer key which can be fetched from the pastebin API + * page + * @param contents + * the contents of the paste + */ + public PastebinPaste(String developerKey, String contents) { + this(developerKey, contents, null); + } + + /** + * Creates a new PastebinPaste instance. + * + * @param developerKey + * a developer key which can be fetched from the pastebin API + * page + * @param contents + * the contents of the paste + * @param account + * a pastebin account + */ + public PastebinPaste(String developerKey, String contents, PastebinAccount account) { + super(contents); + this.developerKey = developerKey; + this.account = account; + } + + /** + * Sets the pastebin account If you set an account the pastes will be listed + * on your account. + * + * @param account + * a pastebin account + */ + public void setAccount(PastebinAccount account) { + this.account = account; + } + + /** + * Gets the pastebin account + * + * @return pastebin account + */ + public PastebinAccount getAccount() { + return this.account; + } + + /** + * Sets the developer key The developer key is required to paste contents on + * pastebin + * + * @param developerKey + * a developer key which can be fetched from the pastebin API + * page + */ + public void setDeveloperKey(String developerKey) { + if (developerKey == null || developerKey.isEmpty()) { + throw new IllegalArgumentException("Developer key can not be null or empty."); + } + this.developerKey = developerKey; + } + + /** + * Sets the paste expire date + * + * @param date + * date when the paste will be removed + */ + public void setPasteExpireDate(PasteExpireDate date) { + this.expireDate = date; + } + + /** + * Gets the developer key + * + * @return developer key + */ + public String getDeveloperKey() { + return this.developerKey; + } + + /** + * Sets the paste title + * + * @param title + * title of the paste + */ + public void setPasteTitle(String title) { + this.pasteTitle = title; + } + + /** + * Gets paste title + * + * @return paste title + */ + public String getPasteTitle() { + return this.pasteTitle; + } + + /** + * Gets paste expire date + * + * @return paste expire date + */ + public PasteExpireDate getPasteExpireDate() { + return this.expireDate; + } + + /** + * Sets the paste format The format is used for syntax highlighting + * + * @see available syntax highlighting + * formats + * @param format + * format of the paste + */ + public void setPasteFormat(String format) { + this.pasteFormat = format; + } + + /** + * Gets paste format + * + * @return paste format + */ + public String getPasteFormat() { + return this.pasteFormat; + } + + /** + * Makes this paste private, unlisted or public Default visibility is public + *

+ * Valid visibilities + *

+ *

+ * {@link PastebinPaste#VISIBILITY_PUBLIC} + *

+ *

+ * {@link PastebinPaste#VISIBILITY_UNLISTED} + *

+ *

+ * {@link PastebinPaste#VISIBILITY_PRIVATE} + *

+ * + * @param visibility + * the paste it's visibility + */ + public void setVisibility(int visibility) { + if (visibility < 0 || visibility > 2) { + throw new IllegalArgumentException("Unknown visibility: " + visibility); + } + this.visibility = visibility; + } + + /** + * Gets this paste visibility + *

+ * Valid visibilities + *

+ *

+ * {@link PastebinPaste#VISIBILITY_PUBLIC} + *

+ *

+ * {@link PastebinPaste#VISIBILITY_UNLISTED} + *

+ *

+ * {@link PastebinPaste#VISIBILITY_PRIVATE} + *

+ * + * @return visibility of this paste + */ + public int getVisibility() { + return this.visibility; + } + + /** + * {@inheritDoc} + */ + @Override + public PastebinLink paste() throws PasteException { + if (getContents() == null || getContents().isEmpty()) { + throw new IllegalStateException("Paste can not be null or empty."); + } + if (getDeveloperKey() == null || getDeveloperKey().isEmpty()) { + throw new IllegalStateException("Developer key is missing."); + } + + Post post = new Post(); + + // required parameters + post.put("api_dev_key", getDeveloperKey()); + post.put("api_option", "paste"); + post.put("api_paste_code", getContents()); + + // optional parameters + if (this.account != null && this.account.getUserSessionId() != null) { + post.put("api_user_key", this.account.getUserSessionId()); + } + if (this.pasteTitle != null) { + post.put("api_paste_name", getPasteTitle()); + } + if (this.pasteFormat != null) { + post.put("api_paste_format", getPasteFormat()); + } + post.put("api_paste_private", Integer.toString(getVisibility())); + if (this.expireDate != null) { + post.put("api_paste_expire_date", expireDate.getValue()); + } + + try { + String pageResponse = Web.getContents(Pastebin.API_POST_LINK, post); + if (pageResponse.startsWith("http")) { + // success + PastebinLink result = new PastebinLink(this, new URL(pageResponse)); + return result; + } + throw new PasteException("Failed to generate paste: " + pageResponse); + } catch (MalformedURLException e) { + // shouldn't happen + throw new PasteException("Failed to generate paste: " + e); + } + } + +}