Skip to content

Commit 56d7093

Browse files
+Improved getMostRecentPastes;
+Created getPast, thats give all the paste information (neet to have a pro account); +Added the 'pasteAuthor" field to PastebinPaste: now it's possible to get the name of the user that posted the paste (only working to getPaste and getMostRecentPasts); +Improved some javadocs; +Added GetPaste example under the example directory. Minor: * PastebinPaste uses now identation only with spaces.
1 parent c396bea commit 56d7093

2 files changed

Lines changed: 421 additions & 346 deletions

File tree

src/main/java/org/jpaste/pastebin/Pastebin.java

Lines changed: 98 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.w3c.dom.NodeList;
2323
import org.xml.sax.InputSource;
2424

25+
import com.fasterxml.jackson.core.type.TypeReference;
2526
import com.fasterxml.jackson.databind.ObjectMapper;
2627

2728
/**
@@ -44,11 +45,14 @@ public class Pastebin {
4445
* Used for fetching an user session id
4546
*/
4647
public static final String API_LOGIN_LINK = "https://pastebin.com/api/api_login.php";
47-
4848
/**
4949
* Scraping api. Must be lifetime pro in otrder to use it.
5050
*/
5151
public static final String API_SCRAPING_LINK = "https://pastebin.com/api_scraping.php";
52+
/**
53+
* Scrape paste metadata link. Must be pro lifetime in order to use it.
54+
*/
55+
public static final String SCRAPE_PASTE_METADATA_URL = "https://pastebin.com/api_scrape_item_meta.php?i=";
5256

5357
/**
5458
* Fetches a paste text from pastebin
@@ -195,22 +199,25 @@ public static PastebinLink[] getTrending(String developerKey) throws ParseExcept
195199

196200
}
197201

198-
throw new ParseException("Failed to parse pastes: " + response);
202+
throw new ParseException("Failed to parse paste: " + response);
199203
}
200204

201205
/**
202206
* Gets the most recent pastes. In order to use it, it's necessary to have a
203-
* <i>lifetime pro</i> account and white-list your IP. Se more on
207+
* <i><a href="https://pastebin.com/pro">pro</a></i> account and white-list your IP. Se more on
204208
* <a href="https://pastebin.com/api_scraping_faq">Pastebin Scraping Api</a>
205-
*
206-
* Note: unfortunably, it's not possible to get the number of hits.
207-
*
208-
* The options for the post are: limit: up to 500. default is 50. lang: any
209-
* of the syntaxes allowed for Pastebin.
210-
*
209+
*
210+
* The options for the post are:
211+
* <ul>
212+
* <li>limit: up to 250 (default is 50)</li>
213+
* <li>lang: <a href="https://pastebin.com/languages">all the pastebin accepted formates</a></li>
214+
* </ul>
215+
*
216+
* @author Felipe
211217
* @param post
212218
* the <code>Post</code> with the options
213219
* @return the pastes.
220+
* @throws ParseException
214221
*/
215222
public static PastebinLink[] getMostRecent(Post post) throws ParseException {
216223
String url = API_SCRAPING_LINK;
@@ -225,29 +232,11 @@ public static PastebinLink[] getMostRecent(Post post) throws ParseException {
225232
throw new ParseException("Failed to parse pastes: " + response);
226233
}
227234

228-
ArrayList<Object> listData = getJSonData(response);
235+
ArrayList<Map<String, Object>> listData = getListJSonData(response);
229236

230237
ArrayList<PastebinLink> listPastebinLink = new ArrayList<>(listData.size());
231-
for (Object object : listData) {
232-
Map<String, Object> tempMap = (Map<String, Object>) object;
233-
PastebinPaste pastebinPaste = new PastebinPaste();
234-
pastebinPaste.setPasteFormat(tempMap.get("syntax").toString());
235-
String pasteTitle = tempMap.get("title").toString();
236-
pastebinPaste.setPasteTitle(pasteTitle == null ? "" : pasteTitle);
237-
long pasteExpireDate = Long.parseLong(tempMap.get("expire").toString());
238-
long pasteDate = Long.parseLong(tempMap.get("date").toString());
239-
pastebinPaste.setPasteExpireDate(pasteExpireDate == 0L ? PasteExpireDate.NEVER
240-
: PasteExpireDate.getExpireDate((int) (pasteExpireDate - pasteDate)));
241-
pastebinPaste.setVisibility(PastebinPaste.VISIBILITY_PUBLIC);
242-
// All the pastes retrieved from this api are public.
243-
244-
PastebinLink pastebinLink = null;
245-
try {
246-
pastebinLink = new PastebinLink(pastebinPaste, new URL(tempMap.get("full_url").toString()),
247-
new Date(pasteDate * 1000));
248-
} catch (MalformedURLException e) {
249-
e.printStackTrace();
250-
}
238+
for (Map<String, Object> tempMap : listData) {
239+
PastebinLink pastebinLink = jSonMapToPastebinLink(tempMap);
251240

252241
if (pastebinLink != null) {
253242
listPastebinLink.add(pastebinLink);
@@ -257,16 +246,91 @@ public static PastebinLink[] getMostRecent(Post post) throws ParseException {
257246
return listPastebinLink.toArray(new PastebinLink[listPastebinLink.size()]);
258247
}
259248

260-
private static ArrayList<Object> getJSonData(String response) {
249+
/**
250+
* Converts the <code>Map</code> with the json informations into a
251+
* <code>PastebinLink</code>.
252+
*
253+
* Important: can't retrieve visibility and, once most of the technics from
254+
* API only alow parse public pasts, the resultant paste is tagged as
255+
* Public.
256+
*
257+
* @author Felipe
258+
* @param tempMap
259+
* the Json Map
260+
* @return PastebinLink with all the informations
261+
*/
262+
private static PastebinLink jSonMapToPastebinLink(Map<String, Object> tempMap) {
263+
PastebinPaste pastebinPaste = new PastebinPaste();
264+
pastebinPaste.setPasteFormat(tempMap.get("syntax").toString());
265+
String pasteTitle = tempMap.get("title").toString();
266+
pastebinPaste.setPasteTitle(pasteTitle == null ? "" : pasteTitle);
267+
pastebinPaste.setPasteAuthor(tempMap.get("user").toString());
268+
long pasteExpireDate = Long.parseLong(tempMap.get("expire").toString());
269+
long pasteDate = Long.parseLong(tempMap.get("date").toString());
270+
pastebinPaste.setPasteExpireDate(pasteExpireDate == 0L ? PasteExpireDate.NEVER
271+
: PasteExpireDate.getExpireDate((int) (pasteExpireDate - pasteDate)));
272+
pastebinPaste.setVisibility(PastebinPaste.VISIBILITY_PUBLIC);
273+
// All the pastes retrieved from this api are public.
274+
275+
PastebinLink pastebinLink = null;
276+
try {
277+
pastebinLink = new PastebinLink(pastebinPaste, new URL(tempMap.get("full_url").toString()),
278+
new Date(pasteDate * 1000));
279+
String hits = tempMap.get("hits").toString();
280+
pastebinLink.setHits(hits != null && !hits.isEmpty() ? Integer.parseInt(hits) : 0);
281+
} catch (MalformedURLException e) {
282+
e.printStackTrace();
283+
}
284+
return pastebinLink;
285+
}
286+
287+
/**
288+
* Returns the JSON response as a <code>ArrayList</code> of /code>Map</code>
289+
*
290+
* @author Felipe
291+
* @param response
292+
* the pastebin JSON response
293+
* @return the list of maps with the content
294+
*/
295+
private static ArrayList<Map<String, Object>> getListJSonData(String response) {
261296
ObjectMapper mapper = new ObjectMapper();
262297
try {
263-
ArrayList<Object> data = mapper.readValue(response, ArrayList.class);
298+
ArrayList<Map<String, Object>> data = mapper.readValue(response,
299+
new TypeReference<ArrayList<Map<String, Object>>>() {
300+
});
264301
return data;
265302
} catch (IOException e) {
266-
267303
e.printStackTrace();
268304
}
269305
return null;
270306
}
271307

272-
}
308+
/**
309+
* Get all available informations from a paste, not only its contents,
310+
* generating a <code>PastebinLink</code>. Part of the
311+
* <a href="https://pastebin.com/api_scraping_faq">Scraping API</a>. Must be
312+
* <a href="https://pastebin.com/pro">Pro user</a> to use. The pastebinKey is the 7-8 size identifier of any
313+
* paste.
314+
*
315+
* @author Felipe
316+
* @param key
317+
* the pastebin key
318+
* @return the <code>PastebinLink</code> with the informations.
319+
* @throws ParseException
320+
*/
321+
public static PastebinLink getPaste(String key) throws ParseException {
322+
String url = SCRAPE_PASTE_METADATA_URL + key;
323+
String response = Web.getContents(url);
324+
325+
if (response == null || response.isEmpty()
326+
|| !(response.charAt(0) == '[' && response.charAt(response.length() - 1) == ']')) {
327+
throw new ParseException("Failed to parse paste: " + response);
328+
}
329+
330+
ArrayList<Map<String, Object>> listData = getListJSonData(response);
331+
332+
Map<String, Object> tempMap = listData.get(0);
333+
PastebinLink pastebinLink = jSonMapToPastebinLink(tempMap);
334+
return pastebinLink;
335+
}
336+
}

0 commit comments

Comments
 (0)