rahul0611

Google Image CC

Aug 18th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. import java.awt.Image;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedReader;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.DataInputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import java.net.URLConnection;
  15.  
  16. import javax.imageio.ImageIO;
  17.  
  18. import net.sf.json.JSONArray;
  19. import net.sf.json.JSONObject;
  20. import net.sf.json.JSONSerializer;
  21.  
  22.  
  23. public class GoogleApi {
  24.  
  25.     /**
  26.      * @param args
  27.      * @throws IOException
  28.      */
  29.     public static void main(String[] args) throws IOException {
  30.         // TODO Auto-generated method stub
  31.  
  32.         try{
  33.         FileInputStream fstream = new FileInputStream("C:\\Users\\Rahul Akula\\workspace\\index\\src\\botanical-names.txt");
  34.           // Get the object of DataInputStream
  35.         String NotfoundIamges = null;
  36.           int countNotFoiund =0;
  37.         File fdemo = new File("Demo");
  38.           DataInputStream in = new DataInputStream(fstream);
  39.           BufferedReader br = new BufferedReader(new InputStreamReader(in));
  40.           String strLine;
  41.           //Read File Line By Line
  42.           while ((strLine = br.readLine()) != null)   {
  43.           // Print the content on the console
  44.           System.out.println (strLine);
  45.          
  46.          
  47.           String DirName = strLine;
  48.           strLine=strLine.replaceAll(" ", "%20");
  49.          
  50.          
  51.            System.out.println (strLine);
  52.          
  53.          
  54.           URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q="+strLine+"&as_rights=%28cc_publicdomain|cc_attribute|cc_sharealike%29.-%28cc_noncommercial|cc_nonderived%29");
  55.           URLConnection connection = url.openConnection();
  56.           //connection.addRequestProperty("Referer", /* Enter the URL of your site here */);
  57.  
  58.           String line;
  59.           StringBuilder builder = new StringBuilder();
  60.           BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  61.           while((line = reader.readLine()) != null) {
  62.           builder.append(line);
  63.           }
  64.  
  65.           JSONObject json = (JSONObject)JSONSerializer.toJSON(builder.toString());
  66.  
  67.           System.out.println(json);
  68.  
  69.           JSONArray results = json.getJSONObject("responseData").getJSONArray("results");
  70.          
  71.           if(results.size()>0){
  72.           JSONObject result1 = results.getJSONObject(0);
  73.          
  74.           String url_escape= result1.getString("unescapedUrl");
  75.          
  76.          
  77.           System.out.println(url_escape);
  78.          
  79.          
  80.            
  81.            
  82.          
  83.          
  84.           new File(DirName).mkdir();
  85.  
  86.           System.out.println(fdemo.getAbsoluteFile().getParent()+"\\"+DirName);
  87.           File f = new File(fdemo.getAbsoluteFile().getParent()+"\\"+DirName);
  88.          
  89.          
  90.           try {
  91.                 URL url1 = new URL(url_escape);
  92.                 InputStream in1 = new BufferedInputStream(url1.openStream());
  93.                 ByteArrayOutputStream out = new ByteArrayOutputStream();
  94.                 byte[] buf = new byte[1024];
  95.                 int n = 0;
  96.                 while (-1!=(n=in1.read(buf)))
  97.                 {
  98.                    out.write(buf, 0, n);
  99.                 }
  100.                 out.close();
  101.                 in1.close();
  102.                 byte[] response = out.toByteArray();
  103.  
  104.                
  105.                 FileOutputStream fos = new FileOutputStream(fdemo.getAbsoluteFile().getParent()+"\\"+DirName.trim()+"\\"+DirName.trim()+".jpg");
  106.                 fos.write(response);
  107.                 fos.close();
  108.  
  109.                
  110.                
  111.                 } catch(IOException ioe) {
  112.                 ioe.printStackTrace();
  113.                 }
  114.           } else {
  115.              
  116.              
  117.               NotfoundIamges= NotfoundIamges+DirName+ "---";
  118.               countNotFoiund++;
  119.              
  120.           }
  121.          
  122.          
  123.          
  124.           }
  125.          
  126.          
  127.           System.out.println(NotfoundIamges);
  128.           System.out.println(countNotFoiund);
  129.           //Close the input stream
  130.           in.close();
  131.             }catch (Exception e){//Catch exception if any
  132.           System.err.println("Error: " + e.getMessage());
  133.           }
  134.        
  135.        
  136.        
  137.        
  138.        
  139.     }
  140.  
  141. }
Add Comment
Please, Sign In to add comment