import java.awt.*; import java.net.*; import java.io.*; import java.util.regex.*; public class MDownload extends java.applet.Applet { public String msg = "Saving..."; public void init() { this.msg = download(getParameter("db"), getParameter("num"), filename("Save as", getParameter("filename"))); } public void paint(Graphics g) { g.drawString(this.msg, 25, 25); } /** * Pops up a File Save dialog, and returns the file name */ public static String filename(String prompt, String selection) { Frame frame = new Frame(); FileDialog fd = new FileDialog(frame, prompt, FileDialog.SAVE); fd.setFile(selection); fd.show(); String file = fd.getFile(); if (file != null && !file.equals("")) { file = fd.getDirectory() + fd.getFile(); } else { file = null; } return file; } /** * Given 2 strings: url and User Agent, connects to the URL and returns the content as a string */ public static String url2text(String urlstr, String userAgent) { StringBuffer text = new StringBuffer(); try { URL url = new URL(urlstr); URLConnection con = url.openConnection(); con.setRequestProperty("User-Agent", userAgent); con.connect(); BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream())); String line; while ((line = r.readLine())!= null) { text.append(line); text.append("\n"); } r.close(); } catch (Exception e) {} return text.toString(); } /** * Given 3 strings: url, User Agent and filename, connects to the URL and dumps the content into the file. */ public static void url2file(String urlstr, String userAgent, String file) { InputStream in = null; OutputStream out = null; try { URL url = new URL(urlstr); URLConnection con = url.openConnection(); con.setRequestProperty("User-Agent", userAgent); con.connect(); in = con.getInputStream(); out = new BufferedOutputStream(new FileOutputStream(file)); byte[] buffer = new byte[1024]; int numRead; while((numRead=in.read(buffer))!=-1){ out.write(buffer, 0, numRead); } } catch (Exception e) { } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch(IOException e) {} } } /** */ public static String download(String db, String num, String file) { String real = "RMA/1.0 (compatible; RealMedia)"; String firefox = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11"; String smil = ""; String msg = file; if (db.equals("M")) { smil = url2text("http://www.musicindiaonline.com/g/r/" + num + "/play2.smil", firefox); Pattern pattern = Pattern.compile("content=\"([^\"]*)\".*audio src=\"([^\"]*)\"", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); Matcher matcher = pattern.matcher(smil); if (matcher.find()) { url2file(matcher.group(1) + matcher.group(2), real, file); } else { msg = "Couldn't match " + db + " in " + smil; } } else if (db.equals("S")) { smil = url2text("http://ww.smashits.com/player/ra/ondemand/playlist.asp?6;giftsToIndia1.rm;1," + num, real); Pattern pattern = Pattern.compile("\"([^\"]*/song.asp[^\"]*)\""); Matcher matcher = pattern.matcher(smil); if (matcher.find()) { url2file(matcher.group(1), real, file); } else { msg = "Couldn't match " + db + " in " + smil; } } else if (db.equals("O")) { smil = url2text("http://www.oosai.com/oosai_plyr/playlist.cfm?sng_id=" + num, real); Pattern pattern = Pattern.compile("(\\S+)\\s*$"); Matcher matcher = pattern.matcher(smil); if (matcher.find()) { url2file(matcher.group(1), real, file); } else { msg = "Couldn't match " + db + " in " + smil; } } else { file ="db is not M/S/O"; } return msg; } public static void main(String args[]) { String msg = ""; msg = download("O", "2254", "D:/temp/killme.rm"); // msg = download("M", "dJfgI5.zhd.As1NMvHdW", filename("Save as", "Album.Thatthalikkudhe.rm")); // msg = download("S", "42693", filename("Save as")); System.out.println(msg); } }