Monday, July 8, 2013

no ads (ads free) merriam webster

import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Robot;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;

import com.sun.glass.events.KeyEvent;

public class MWDict {

private static final boolean AUTO_CLOSE = true;
    public static void main(String[] args) throws Exception {
    String word = "sumo";
    display(word);
    playAudio(word);
    System.exit(0);
    }
   
    public static void display(String word) throws IOException{
    String text = Jsoup.connect("http://www.merriam-webster.com/dictionary/"+word.trim()).get().getElementsByClass("d").outerHtml();
    List<String> lines = new ArrayList<String>();
    lines.add("<html>");
    lines.add(text);
    lines.add("</html>");
    FileUtils.write(new File("test.html"), lines);
    Desktop.getDesktop().browse(new File("test.html").toURI());
    }
   
    public static String getAudioURL(String word) throws IOException{
    String text = Jsoup.connect("http://www.merriam-webster.com/dictionary/"+word).get().getElementsByClass("au").outerHtml();
    String[] elements = text.substring(text.indexOf("(")+1,text.indexOf(")")).replaceAll("'","").split(",");
    String url = "http://www.merriam-webster.com/audio.php?file="+elements[0].trim()+"&word="+elements[1].trim();
    System.out.println("url: "+url);
    return url;
    }
   
    public static void playAudio(String word) throws IOException, URISyntaxException, AWTException, InterruptedException{
    Desktop.getDesktop().browse(new URI(getAudioURL(word)));
    //close
    if(AUTO_CLOSE){
        Thread.sleep(2000);
        Robot robot = new Robot();
        robot.setAutoDelay(200);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_W);
        robot.keyRelease(KeyEvent.VK_W);
        robot.keyRelease(KeyEvent.VK_CONTROL);
    }
    }
}

No comments:

Post a Comment