import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class GetAllDeveloperEmails {
private static String masheryV2ApiKey = ""; //TODO: Enter your Mashery V2 API Key
private static String masheryV2ApiSecret = ""; //TODO: Enter your Mashery V2 API Secret
private static String siteIdFromDashboard = ""; //TODO: Enter your Mashery Site ID (from Dashboard)
/**
* @param args
*/
public static void main(String[] args) {
try {
int total_pages = 1;
HashMap<String, String> membersFromJson = new HashMap<String,String>();
for (int i = 1; i < total_pages +1 ; i++) {
String jsonString = "{\"method\":\"object.query\",\"id\":1,\"params\":[\"SELECT username, email FROM members PAGE "+i+" ITEMS 100\"]}";
String jsonResponse = callMasheryAPI(jsonString);
membersFromJson.putAll(getMembersFromJson(jsonResponse));
total_pages = getResultTotalPages(jsonResponse);
}
for (Map.Entry<String,String> entry : membersFromJson.entrySet()) {
String username = entry.getKey();
String email = entry.getValue();
System.out.println(username + "," + email);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static HashMap<String, String> getMembersFromJson(String jsonResponse) throws Exception {
HashMap<String, String> membersFromJson = new HashMap<String,String>();
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readValue(jsonResponse, JsonNode.class);
JsonNode itemsNodes = rootNode.path("result").path("items");
for (JsonNode node : itemsNodes) {
membersFromJson.put(node.path("username").textValue(), node.path("email").textValue());
}
return membersFromJson;
}
private static int getResultTotalPages(String jsonResponse) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readValue(jsonResponse, JsonNode.class);
JsonNode totalNode = rootNode.path("result").path("total_pages");
return Integer.parseInt(totalNode.toString());
}
private static String callMasheryAPI(String jsonString) throws Exception
{
long epoch = System.currentTimeMillis()/1000;
String timestamp = Long.toString(epoch);
String sig = MD5(masheryV2ApiKey + masheryV2ApiSecret + timestamp);
String urlStr = "http://api.mashery.com/v2/json-rpc";
String urlParams ="apikey=" + masheryV2ApiKey + "&sig=" + sig;
urlStr = urlStr + "/" + siteIdFromDashboard +"?" + urlParams;
URL url = new URL(urlStr);
URLConnection connection = url.openConnection();
connection.setDoOutput(true); // Triggers POST.
connection.setDoInput(true);
connection.setReadTimeout(0);
connection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
connection.setRequestProperty("Content-Type", "application/json");
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(jsonString);
wr.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer buf = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
buf.append(inputLine);
}
wr.close();
in.close();
return buf.toString();
}
private static String callMasheryReportingAPI(String reportingMethod, String reportingParams) throws Exception
{
long epoch = System.currentTimeMillis()/1000;
String timestamp = Long.toString(epoch);
String sig = MD5(masheryV2ApiKey + masheryV2ApiSecret + timestamp);
String urlStr = "http://api.mashery.com/v2/rest";
String urlParams ="apikey=" + masheryV2ApiKey + "&sig=" + sig;
urlStr = urlStr + "/" + siteIdFromDashboard + "/" + reportingMethod +"?" + reportingParams + "&" + urlParams;
URL url = new URL(urlStr);
URLConnection connection = url.openConnection();
connection.setDoInput(true);
connection.setReadTimeout(0);
connection.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
connection.setRequestProperty("Content-Type", "application/json");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer buf = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
buf.append(inputLine);
buf.append("\n");
}
in.close();
return buf.toString();
}
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
private static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance("MD5");
byte[] md5hash = new byte[32];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
md5hash = md.digest();
return convertToHex(md5hash);
}
}