Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.86 KB | None | 0 0
  1. package com.john.itunes;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. import java.util.ArrayList;
  9.  
  10. import org.jsoup.Jsoup;
  11. import org.jsoup.nodes.Document;
  12. import org.jsoup.nodes.Element;
  13. import org.jsoup.select.Elements;
  14.  
  15. public class iTunesScrapper {
  16.  
  17.     public static void main (String [] args) throws IOException, InterruptedException {
  18.         ArrayList<String> links = new LinkRetriever().getLinks();
  19.         for (String link : links) {
  20.             new iTunesScrapper().parseData(link);
  21.             // add a delay so itunes wont block our ip, not proven though
  22.             Thread.sleep(60000);
  23.         }
  24.     }
  25.  
  26.     public void parseData(String url) throws FileNotFoundException, InterruptedException {
  27.         // get the url and parse the page
  28.         String category = url.split("games-")[1].split("/")[0].trim();
  29.         Document doc = null;
  30.         Elements eles = null;
  31.         PrintWriter pw = new PrintWriter("C:/Users/MarionMariah/Desktop/"+category+".txt");
  32.         while (true) {
  33.             try {
  34.                 for (Integer letterIndex = 0; letterIndex < 27; letterIndex++) {
  35.                     doc = Jsoup.connect(url).get();
  36.                     Elements letterElems = doc.select(".list.alpha a");
  37.                     doc = Jsoup.connect(letterElems.get(letterIndex).attr("href")).get();
  38.                     while (true) {
  39.                         eles = doc.select("#selectedcontent div");
  40.                         for (Element elements : eles) {
  41.                             Elements elems = elements.select("ul li");
  42.                             for (Element e : elems) {
  43.                                 String link = e.select("a").attr("href");
  44.                                 System.out.println("now parsing... " + link);
  45.  
  46.                                 // now for every url get the required details
  47.                                 while (true) {
  48.                                     try {
  49.                                         Document docdetails = Jsoup.connect(link).get();
  50.                                         for (Integer i = 0; i < 5 && docdetails.select(".loadingbox").size() != 0; i++) {
  51.                                             Thread.sleep(60000);
  52.                                             docdetails = Jsoup.connect(link).get();
  53.                                         }
  54.                                         String image = docdetails.select("#left-stack").get(0).select("img").attr("src-swap");
  55.                                         String title = docdetails.select("#title .left h1").html().trim();
  56.                                         String developer = docdetails.select("#title .left h2").html().trim().replace("By", "").trim();
  57.                                         String description = docdetails.select(".product-review").first().html();
  58.                                         description = description.replace("<h4> ", "").replace(" </h4>", "").replace("<p>", "").replace("<br />", "\n").replace("</p>", "");
  59.                                         description = description.replace("&quot;", "\"").replace("&amp;", "&").replace("&nbsp;", " ").replace("&gt;", ">");
  60.                                         ArrayList<String> screenshots = new ArrayList<String>();
  61.  
  62.                                         Elements els = docdetails.select(".content.iphone-screen-shots .lockup img");
  63.                                         for (Element el : els) {
  64.                                             screenshots.add(el.attr("src"));
  65.                                         }
  66.  
  67.                                         System.out.println("Title: " + title);
  68.                                         System.out.println("Developer: " + developer);
  69.                                         System.out.println("<img href=\"" + image + "\"/>");
  70.                                         System.out.println(description);
  71.  
  72.                                         // print the details in the file
  73.                                         pw.println("Title: " + title);
  74.                                         pw.println("Developer: " + developer);
  75.                                         pw.println("<img href=\"" + image + "\"/>");
  76.                                         pw.println(description);
  77.  
  78.                                         // add the hyperlink
  79.                                         link = link + "&uo=4&at=10lGio";
  80.                                         pw.println(link);
  81.  
  82.                                         // adding images as string instead of image
  83.                                         String links = "";
  84.                                         for (String s : screenshots) {
  85.                                             links = links + "<img href=\"" + s + "\"/>\n";
  86.                                         }
  87.                                         System.out.println(links);
  88.                                         pw.println("\nScreenShots: \n" + links);
  89.  
  90.                                         break;
  91.  
  92.                                     } catch (IOException | IndexOutOfBoundsException ex) {
  93.                                         System.out.println("error encountered, continuing..");
  94.                                         ex.printStackTrace();
  95.                                         continue;
  96.                                     }
  97.                                 }
  98.  
  99.                                 System.out.println("\n###############################################################################\n");
  100.                                 pw.println("\n###############################################################################\n");
  101.                             }
  102.                         }
  103.                         pw.close();
  104.                         Elements nextPage = doc.select(".paginate-more");
  105.                         if (nextPage.size() != 0) {
  106.                             Thread.sleep(60000);
  107.                             doc = Jsoup.connect(nextPage.get(0).attr("href")).get();
  108.                             continue;
  109.                         } else {
  110.                             break;
  111.                         }
  112.                     }
  113.                 }
  114.             } catch (IOException e) {
  115.                 System.out.println("error encountered, continuing...");
  116.                 e.printStackTrace();
  117.                 continue;
  118.             }
  119.         }
  120.     }
  121. }
  122.  
  123. class LinkRetriever {
  124.     BufferedReader br = null;
  125.     public ArrayList<String> getLinks() throws IOException {
  126.         ArrayList<String> links = new ArrayList<String>();
  127.         br = new BufferedReader(new FileReader("C:/Users/MarionMariah/Desktop/sampleFile.txt"));
  128.         String s;
  129.         while ((s = br.readLine()) != null) {
  130.             links.add(s);
  131.             System.out.println(s);
  132.         }
  133.         return links;
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement