Skip to content

Instantly share code, notes, and snippets.

@schwartzadev
Created December 16, 2016 03:27
Show Gist options
  • Save schwartzadev/75169f641f5b3a508d63c572393359f9 to your computer and use it in GitHub Desktop.
Save schwartzadev/75169f641f5b3a508d63c572393359f9 to your computer and use it in GitHub Desktop.
Retrieve Play Store Icon Images
import java.util.List;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.URL;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/* @author Andrew Schwartz
* @date December 2016
* This class uses a csv file, test.csv that contains Google Play URLs. This then retrieves
* the number of reviews for the given app and then if the app has at least *long max* reviews (line 57)
* it will download the icon for the app into the folder specified by imgDestination. */
public class Run {
private static String imgDestination = "C:\\Users\\werdn\\workspace\\IconRequests\\img\\";
public static void main(String args[]) {
print("running...");
Document document;
String[] url = {};
List<String> newUrl = new ArrayList<String>();
String line = "";
// Make String array from CSV
try (BufferedReader br = new BufferedReader(new FileReader("test.csv"))) {
while ((line = br.readLine()) != null) {
url = line.split(",");
}
} catch (IOException e) {
e.printStackTrace();
}
int urlLength = url.length;
for (int i = 0; i < urlLength; i++) { // for every given URL from the csv
try {
//Connect to given Play Store link
document = Jsoup.connect(url[i]).get();
String title = (document.title().substring(0, document.title().length() - 30)); // Get title of app, without the information @ Google Play
print("[" + i + "/" + urlLength + "] " + title); //Print title.
Element ratings = document.select("div > div > div > div > div > div > div > div > div > div > div > div > span > span").first(); //Get # of reviews
int ratingCount = Integer.parseInt(ratings.text().replace(",", "")); // # ratings to int
long max = 75000; // specified maximum amount of reviews to filer out apps with low reviews
if (ratingCount > max) {
System.out.print("YES ");
newUrl.add(url[i]); // add url to newUrl string list
Element img = document.select("img").first(); // Get image URL
downloadImage(img.absUrl("src"), title); // save image
}
else { // if # of reviews is less than specified max, say no
System.out.println("NO");
}
print(""); // console spacing
} catch (HttpStatusException h) { // catch 404 error, print it for the given URL
print("[" + i + "/" + url.length + "] 404 ERROR");
print("NO");
print("");
} catch (IOException e) {
e.printStackTrace();
}
}
// Print to console and CSV
try {
PrintWriter writer = new PrintWriter("bigRequests.csv");
for (String s : newUrl) {
writer.println(s);
print(s);
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
print("\ndone");
}
public static void print(String string) {
System.out.println(string);
}
private static void downloadImage(String strImageURL, String t){
//get file name from image path
String strImageName = t + ".png";
try {
//open the stream from URL
URL urlImage = new URL(strImageURL);
InputStream in = urlImage.openStream();
byte[] buffer = new byte[4096];
int n = -1;
OutputStream os = new FileOutputStream(imgDestination + strImageName);
//write bytes to the output stream
while ( (n = in.read(buffer)) != -1 ){
os.write(buffer, 0, n);
}
os.close();
print("Saved");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment