package TESTAPI; import java.io.*; import java.net.URL; import java.nio.charset.Charset; import java.util.*; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import org.json.JSONException; import org.json.JSONObject; public class SimilarWEB { public static void main(String[] args) { try { System.out.println("Welcome to the Similar Web API tool designed to save you 250k"); //read list from file and make hash map list, HashMap domainschecked = new HashMap(); String fileName = "C:///JAVA/Testdata.txt"; File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); Iterator iter = domainschecked.entrySet().iterator(); boolean runonce = true; int counter = 0; String line; while((line = br.readLine()) != null){ //process the line String domain = line; //System.out.println("Domain is:" + line); //check if valid domain if(isValidURL2(domain)) //IF VALID DOMAIN ADD TO HASH ELSE SKIP not passing the validator check yet { if(counter < 10) //limit to one API call right now { System.out.println("Checking..." + domain); //System.out.println("Domain is:" + domain); //JSONObject json = readJsonFromUrl("https://api.similarweb.com/v1/website/torontosun.com/category-rank/category-rank?api_key=24838848f6c54299ab8640ca92d8b579&format=json"); JSONObject json = readJsonFromUrl("https://api.similarweb.com/v1/website/"+domain+"/category-rank/category-rank?api_key=24838848f6c54299ab8640ca92d8b579&format=json"); // System.out.println(json.toString()); try { // System.out.println("Categroy is: " +json.get("category")); domainschecked.put(domain, (String) json.get("category")); } catch(Exception eer) { domainschecked.put(domain, "Not found"); } counter++; //add catagroy to site in hash } } } System.out.println("Results"); //System.out.println("Printing hash map"); //loop through hash list calling Similar web API for (String name: domainschecked.keySet()){ String key = name.toString(); String value = domainschecked.get(name).toString(); System.out.println(key + " " + value); //we would write back to file here } //System.out.println("[Key] : " + key + " [Value] : " + value)); //take returned value and update Arraylist to include Catagory type } catch(Exception e) { //blah blah blah } } private static String readAll(Reader rd) throws IOException { StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } //System.out.println(sb.toString()); return sb.toString(); } public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = new JSONObject(jsonText); return json; } finally { is.close(); } } //this will call the similar web api with a site param public static boolean isValidURL2(String url) { if(url.isBlank()) { return false; } return true; } /* Returns true if url is valid */ public static boolean isValid(String url) { /* Try creating a valid URL */ try { new URL(url).toURI(); return true; } // If there was an Exception // while creating URL object catch (Exception e) { return false; } } }