Advertisement
Mouamle

Meh

Nov 17th, 2017
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package mouamle.project.autoTranslater;
  2.  
  3. import java.awt.Toolkit;
  4. import java.awt.datatransfer.DataFlavor;
  5. import java.net.URL;
  6. import java.net.URLEncoder;
  7. import java.util.concurrent.LinkedBlockingQueue;
  8.  
  9. import org.jsoup.Jsoup;
  10. import org.jsoup.nodes.Document;
  11. import org.jsoup.nodes.Element;
  12.  
  13. import lc.kra.system.keyboard.GlobalKeyboardHook;
  14. import lc.kra.system.keyboard.event.GlobalKeyAdapter;
  15. import lc.kra.system.keyboard.event.GlobalKeyEvent;
  16.  
  17. public class KeyListener {
  18.  
  19.     @SuppressWarnings("deprecation")
  20.     public static TranslationResult getTranslation(String text) {
  21.         try {
  22.             Document doc = Jsoup.parse(new URL(String.format("https://www.urbandictionary.com/define.php?term=%s", URLEncoder.encode(text))), 5000);
  23.             Element el = doc.select(".def-panel").get(0);
  24.             TranslationResult res = new TranslationResult(el.select(".def-header").text().trim(), el.select(".meaning").text().trim(), el.select(".example").text().trim());
  25.             System.out.println(res);
  26.             return res;
  27.         } catch (Exception e) {}
  28.  
  29.         return null;
  30.     }
  31.  
  32.     public static void showTranslate(String text) {
  33.         getTranslation(text);
  34.     }
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook(true);
  38.  
  39.         LinkedBlockingQueue<String> toTranslate = new LinkedBlockingQueue<>(10);
  40.  
  41.         keyboardHook.addKeyListener(new GlobalKeyAdapter() {
  42.             public void keyReleased(GlobalKeyEvent e) {
  43.                 if (e.getVirtualKeyCode() == GlobalKeyEvent.VK_C && e.isControlPressed()) {
  44.                     try {
  45.                         toTranslate.put((String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor));
  46.                     } catch (Exception e2) {}
  47.                 } else if (e.getVirtualKeyCode() == GlobalKeyEvent.VK_ESCAPE) {
  48.                     keyboardHook.shutdownHook();
  49.                     System.exit(0);
  50.                 }
  51.             }
  52.         });
  53.  
  54.         new Thread(() -> {
  55.             while (true) {
  56.                 String word = toTranslate.poll();
  57.                 if (word != null) {
  58.                     showTranslate(word);
  59.                 }
  60.             }
  61.         }).start();
  62.     }
  63.  
  64. }
  65.  
  66. class TranslationResult {
  67.     public String title, desc, example;
  68.  
  69.     public TranslationResult() {}
  70.  
  71.     public TranslationResult(String title, String desc, String example) {
  72.         this.title = title;
  73.         this.desc = desc;
  74.         this.example = example;
  75.     }
  76.  
  77.     @Override
  78.     public String toString() {
  79.         return title + "\n" + desc + "\n\n" + example;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement