Toy Example App that downloads Google Play APKs using Java and APKLeecher service

In the toy example below everything is done with string manipulation to extract the actual APK URL and proceed with the download.
The following can be improved by using a DOM parser and searching for the specific elements of interest.

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;


public class APKLeech {

    public static void main (String args[])
	{
		String url = "http://apkleecher.com/download/?dl=com.aleksirantonen.clayhuntfree";
		try {
			leech(url);
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	
	public static void leech(String pageURL) throws IOException
	{
		URL url = new URL(pageURL);
        URLConnection yc = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader( yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            if(inputLine.indexOf("setTimeout('location.href=\"../apps")!=-1)
            {
            	String apkURL = getAPKlocation(inputLine,pageURL);
            	if(apkURL==null) break;
            	System.out.println("Downloading from:" + apkURL);
            	DownloadAPK(apkURL);
            	break;
            }
        in.close();
	}
	
	public static String getAPKlocation(String timeOutLine, String originalPageURL)
	{
		int trimLoc = timeOutLine.indexOf("../apps");
		if(trimLoc == -1) return null;
		
		String result = timeOutLine.substring(trimLoc);
		trimLoc = result.indexOf(".apk\"");
		result = result.substring(0,trimLoc+4);
		
		String htmlPageUrlDir = originalPageURL.substring(0, originalPageURL.lastIndexOf("/")+1);
		return htmlPageUrlDir+result;		
	}
	
	//http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java
	public static void DownloadAPK(String APKurl) throws IOException
	{
		URL website = new URL(APKurl);
		ReadableByteChannel rbc = Channels.newChannel(website.openStream());
		FileOutputStream fos = new FileOutputStream(APKurl.substring(APKurl.lastIndexOf("/")+1));
		fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);		
	}
	
}



Menelaos Bakopoulos

Mr. Menelaos Bakopoulos is currently pursuing his PhD both at Center for TeleInFrastruktur (CTiF) at Aalborg University (AAU) in Denmark and Athens Information Technology (AIT) in Athens, Greece. He received a Master in Information Technology and Telecommunications Systems from Athens Information Technology and a B.Sc. in Computer Science & Management Information Systems from the American College of Thessaloniki. Since April 2008 he has been a member of the Multimedia, Knowledge, and Web Technologies Group.

More Posts