Skip to content
Open
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
18 changes: 18 additions & 0 deletions examples/GetPaste.java
Original file line number Diff line number Diff line change
@@ -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 <a href="https://pastebin.com/api_scraping_faq">Pastebin Scraping API</a>
*
* @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());
}
}
147 changes: 112 additions & 35 deletions src/main/java/org/jpaste/pastebin/Pastebin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down Expand Up @@ -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
* <i>lifetime pro</i> account and white-list your IP. Se more on
* <i><a href="https://pastebin.com/pro">pro</a></i> account and white-list
* your IP. Se more on
* <a href="https://pastebin.com/api_scraping_faq">Pastebin Scraping Api</a>
*
* 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:
* <ul>
* <li>limit: up to 250 (default is 50)</li>
* <li>lang: <a href="https://pastebin.com/languages">all the pastebin
* accepted formates</a></li>
* </ul>
*
* @author Felipe
* @param post
* the <code>Post</code> with the options
* @return the pastes.
* @throws ParseException
*/
public static PastebinLink[] getMostRecent(Post post) throws ParseException {
String url = API_SCRAPING_LINK;
Expand All @@ -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<Object> listData = getJSonData(response);

ArrayList<PastebinLink> listPastebinLink = new ArrayList<>(listData.size());
for (Object object : listData) {
Map<String, Object> tempMap = (Map<String, Object>) 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<Map<String, Object>> listData = getListJSonData(response);
ArrayList<PastebinLink> listPastebinLink = new ArrayList<PastebinLink>();
for (Map<String, Object> 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) {
Expand All @@ -257,16 +251,99 @@ public static PastebinLink[] getMostRecent(Post post) throws ParseException {
return listPastebinLink.toArray(new PastebinLink[listPastebinLink.size()]);
}

private static ArrayList<Object> getJSonData(String response) {
/**
* Converts the <code>Map</code> with the json informations into a
* <code>PastebinLink</code>.
*
* 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<String, Object> 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 <code>ArrayList</code> of /code>Map</code>
*
* @author Felipe
* @param response
* the pastebin JSON response
* @return the list of maps with the content
*/
private static ArrayList<Map<String, Object>> getListJSonData(String response) {
ObjectMapper mapper = new ObjectMapper();
try {
ArrayList<Object> data = mapper.readValue(response, ArrayList.class);
ArrayList<Map<String, Object>> data = mapper.readValue(response,
new TypeReference<ArrayList<Map<String, Object>>>() {
});
return data;
} catch (IOException e) {

e.printStackTrace();
}
return null;
}

}
/**
* Get all available informations from a paste, not only its contents,
* generating a <code>PastebinLink</code>. Part of the
* <a href="https://pastebin.com/api_scraping_faq">Scraping API</a>. Must be
* <a href="https://pastebin.com/pro">Pro user</a> to use. The pastebinKey
* is the 7-8 size identifier of any paste.
*
* @author Felipe
* @param key
* the pastebin key
* @return the <code>PastebinLink</code> 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<Map<String, Object>> listData = getListJSonData(response);

Map<String, Object> tempMap = listData.get(0);
PastebinLink pastebinLink = null;
try {
pastebinLink = jSonMapToPastebinLink(tempMap);
} catch (MalformedURLException e) {
}
return pastebinLink;
}
}
Loading