Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.meli.client.controller; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.meli.client.helper.PropsHandler; import com.meli.client.model.Item; import com.mercadolibre.sdk.Meli; import com.mercadolibre.sdk.MeliException; import com.ning.http.client.FluentStringsMap; import com.ning.http.client.Response; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; /** * * @author Charlie */ public class AppController { private static Properties props = PropsHandler.getPropertiesOb(); private static String apiUrl = props.getProperty("api.url"); private static int minWaitTime = Integer.parseInt(props.getProperty("minWaitTime")); private static int maxWaitTime = Integer.parseInt(props.getProperty("maxWaitTime")); private static boolean allowDuplicates = Boolean.valueOf(props.getProperty("allowDuplicates")); private static void doApiCalls(String query, String countryCode) { List<Item> items = new ArrayList<Item>(); Random r = new Random(); Meli meliOb = new Meli(2072832875526076L, "1VZEFfhbSCy3vDDrh0Dp96NkfgNOWPGq"); try { FluentStringsMap params = new FluentStringsMap(); params.add("q", query); String path = countryCode.isEmpty() ? "/sites/MLA/search/" : "/sites/" + countryCode + "/search"; Response response = meliOb.get(path, params); ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(response.getResponseBody()); JsonNode resultNode = rootNode.findPath("results"); if (resultNode.size() > 0) { JsonNode currNode = null; JsonNode dupNode = null; boolean dupNodeVal = false; CloseableHttpClient httpClient = HttpClientBuilder.create().build(); Item item = null; int randomMins; String checkDupsUrl = null; HttpGet get = null; URIBuilder builder = null; URI uri = null; for (int i = 0; i < resultNode.size(); i++) { currNode = resultNode.get(i); builder = new URIBuilder(); builder.setScheme("http").setHost(apiUrl).setPath("/api/proxy/check") .setParameter("host", "test").setParameter("itemID", currNode.get("id").asText()); uri = builder.build(); get = new HttpGet(uri); get.addHeader("accept", "application/json"); CloseableHttpResponse res = httpClient.execute(get); BufferedReader br = new BufferedReader(new InputStreamReader(res.getEntity().getContent())); String content = "", line; while ((line = br.readLine()) != null) { content = content + line; } if (!content.isEmpty()) { dupNode = objectMapper.readTree(content); dupNodeVal = Boolean.parseBoolean(dupNode.get("isDuplicate").asText()); if (dupNodeVal && !allowDuplicates) continue; item = new Item(query, currNode.get("id").asText(), "", //currNode.get("host").asText(),?? currNode.get("site_id").asText(), currNode.get("title").asText(), currNode.get("permalink").asText(), currNode.get("category_id").asText(), currNode.get("seller").get("id").asText(), "", //currNode.get("seller").get("name").asText() "", //currNode.get("seller").get("link").asText() "", //currNode.get("seller").get("email").asText() currNode.get("price").asText(), "", //currNode.get("auction_price").asText(), "", //currNode.get("currency_id").asText(), currNode.get("thumbnail").asText()); items.add(item); } randomMins = (int) (Math.random() * (maxWaitTime - minWaitTime)) + minWaitTime; Thread.sleep(randomMins); } if (!items.isEmpty()) { HttpPost post = new HttpPost(apiUrl + "/proxy/add"); StringEntity stringEntity = new StringEntity(objectMapper.writeValueAsString(items)); post.setEntity(stringEntity); post.setHeader("Content-type", "application/json"); CloseableHttpResponse postResponse = httpClient.execute(post); System.out.println("this is the reponse of the final request: " + postResponse.getStatusLine().getStatusCode()); } } } catch (MeliException ex) { Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex); } catch (URISyntaxException ex) { Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex); } } private static void processQueryFile(String fileName, String countryCode) { System.out.println("processQueryFile entered.."); File file = new File(fileName); if (!file.isDirectory()) file = file.getParentFile(); if (file != null && file.exists()) { String line; try (InputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8")); BufferedReader br = new BufferedReader(isr);) { while ((line = br.readLine()) != null) { doApiCalls(line, countryCode); } } catch (IOException ex) { Logger.getLogger(AppController.class.getName()).log(Level.SEVERE, null, ex); } } else { System.out.println("wasnt a valid file"); } } public static void main(String[] args) { String query = ""; String fileName = ""; String countryCode = ""; String option = ""; for (int i = 0; i < args.length - 1; i++) { option = args[i]; if (option.length() >= 2) { String argVal = option.substring(0, 2); if (!args[i + 1].isEmpty() && !args[i + 1].matches("-[qfc]")) { if ("-q".equals(argVal)) { query = args[i + 1]; continue; } if ("-f".equals(argVal)) { fileName = args[i + 1]; continue; } if ("-c".equals(argVal)) countryCode = args[i + 1]; } } } if (!fileName.isEmpty()) processQueryFile(fileName, countryCode); if (!query.isEmpty()) doApiCalls(query, countryCode); } }