Bittle

UrbanDictionary.java

Aug 3rd, 2016
209
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. package com.bittle;
  2.  
  3. import android.os.AsyncTask;
  4.  
  5. import java.io.*;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8.  
  9.  
  10. public class UrbanDictionary extends AsyncTask<String, Void, String> {
  11.  
  12.  
  13.     @Override
  14.     protected String doInBackground(String... params) {
  15.         String line = params[0];
  16.         String term2 = line.substring(line.indexOf(" "));
  17.         try {
  18.             try {
  19.                 String top = "!ud " + term2 + "\n\nUrban dictionary\n\n";
  20.                 final String term = term2.replaceAll("\\s+", "+");
  21.                 URL url = new URL("http://www.urbandictionary.com/define.php?term=" + term);
  22.                 //URL url = new URL("file:///Users/oscar/Desktop/inputFile");
  23.  
  24.                 URLConnection con = null;
  25.                 InputStream is = null;
  26.                 try {
  27.                     con = url.openConnection();
  28.                     is = con.getInputStream();
  29.                 } catch (java.net.UnknownHostException ex) {
  30.                     return "Check your internet connection";
  31.                 }
  32.  
  33.                 BufferedReader br = null;
  34.                 try {
  35.                     br = new BufferedReader(new InputStreamReader(is));
  36.                 } catch (java.lang.NullPointerException e) {
  37.                     return "Check your internet connection";
  38.                 }
  39.  
  40.                 //String line = null;
  41.  
  42.                 int numDef = 0;
  43.                 //boolean flag = false;
  44.                 String[] storage = new String[10];
  45.                 //try {
  46.                 while ((line = br.readLine()) != null) {
  47.                     int count = 0;
  48.                     boolean flag = false;
  49.                     try {
  50.                         while (!(line.contains("<div class=\'meaning\'>")) && line != null) {
  51.                             line = br.readLine();
  52.                         }
  53.                     } catch (Exception e) {
  54.                         break;
  55.                     }
  56.                     line = br.readLine();
  57.                     if (line.contains("<p>Can you define it?</p>")) {
  58.                         return "No Definition";
  59.                     }
  60.                     try {
  61.                         while (!line.contains("<div class=\'contributor\'>") && line != null) {
  62.                             //count++;
  63.                             //System.out.println(count);
  64.                             if (line.contains("<p>Can you define it?</p>")) {
  65.                                 return "No Definition";
  66.                             }
  67.                             //if(line.contains("</div><div class='example'>")){count++;}
  68.  
  69.  
  70.                             line += br.readLine();
  71.  
  72.                         }
  73.  
  74.                         line = line.replace("</div><div class='contributor'>", "");
  75.                         line = line.replace("</div><div class='example'>", "\n\nⓔⓧⓐⓜⓟⓛⓔ:\n\n");
  76.  
  77.                         line = line.replace("<br/>", "\n");
  78.                         line = line.replace("&quot;", "\"");
  79.                         line = line.replace("&#39;", "\'");
  80.  
  81.                     } catch (Exception ex3) {
  82.                         break;
  83.                     }
  84.                     String temp = "";
  85.                     if (line.contains("<")) {
  86.                         int l = line.length() - ((line.replaceAll("<", "")).length());
  87.                         for (int xx = 0; xx < l; xx++) {
  88.                             int a = line.indexOf("<");
  89.                             int b = line.indexOf(">");
  90.                             if (a == -1) {
  91.                                 continue;
  92.                             }
  93.                             String t;
  94.                             try {
  95.                                 t = line.substring(a, b + 1);
  96.                             } catch (Exception ex) {
  97.                                 t = line.substring(a, b);
  98.                             }
  99.                             line = line.replace(t, "");
  100.                         }
  101.                         line = line.replace("&lt;", "<");
  102.                         line = line.replace("&gt;", ">");
  103.  
  104.                     }
  105.  
  106.                     //System.out.println(t+"====");
  107.  
  108.                     if (!line.equals(null)) {
  109.                         storage[numDef] = line;
  110.                     }
  111.  
  112.                     numDef++;
  113.                 }
  114.                 br.close();
  115.                 for (int xp = 0; xp < numDef; xp++) {
  116.                     int ran = (int) (Math.random() * numDef);
  117.                     if (!(storage[ran].length() > 2000 - top.length())) {
  118.                         String t = storage[ran].substring(0, storage[ran].length() - 3);
  119.                         if (!t.contains("ⓔⓧⓐⓜⓟⓛⓔ:")) {
  120.                             storage[ran] = storage[ran].replace("ⓔⓧⓐⓜⓟⓛⓔ:\n\n", "ⓝⓞ ⓔⓧⓐⓜⓟⓛⓔ ⓕⓞⓤⓝⓓ");
  121.                         }
  122.                         return top + storage[ran];
  123.  
  124.                     }
  125.                 }
  126.  
  127.  
  128.             } catch (Exception ex) {
  129.                 //return ex.toString();
  130.                 System.out.println(ex.toString());
  131.             }
  132.             //System.out.println("An error has occurred! Please contact Bittle");
  133.         } catch (Exception e) {
  134.             e.printStackTrace();
  135.         }
  136.         return "No Definition found";
  137.     }
  138.  
  139.  
  140.     protected void onPostExecute(String result) {
  141.         super.onPostExecute(result);
  142.         kik.a.e.c.a caller = new kik.a.e.c.a();
  143.  
  144.         caller.sendMessage(result);
  145.     }
  146.  
  147.     public static void main(String[][] args) {
  148.         //This is how you call the async task
  149.         String line = "!ud hi";
  150.         line = line.replace("\\s+", " "); //This incase someone puts double space for some reason
  151.  
  152.  
  153.         if (line.split(" ")[0].equals("!ud")) //To make sure its !ud
  154.             new UrbanDictionary().execute(line);
  155.     }
  156.  
  157. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment