package com.bittle;
import android.os.AsyncTask;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class UrbanDictionary extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String line = params[0];
String term2 = line.substring(line.indexOf(" "));
try {
try {
String top = "!ud " + term2 + "\n\nUrban dictionary\n\n";
final String term = term2.replaceAll("\\s+", "+");
URL url = new URL("http://www.urbandictionary.com/define.php?term=" + term);
//URL url = new URL("file:///Users/oscar/Desktop/inputFile");
URLConnection con = null;
InputStream is = null;
try {
con = url.openConnection();
is = con.getInputStream();
} catch (java.net.UnknownHostException ex) {
return "Check your internet connection";
}
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(is));
} catch (java.lang.NullPointerException e) {
return "Check your internet connection";
}
//String line = null;
int numDef = 0;
//boolean flag = false;
String[] storage = new String[10];
//try {
while ((line = br.readLine()) != null) {
int count = 0;
boolean flag = false;
try {
while (!(line.contains("<div class=\'meaning\'>")) && line != null) {
line = br.readLine();
}
} catch (Exception e) {
break;
}
line = br.readLine();
if (line.contains("<p>Can you define it?</p>")) {
return "No Definition";
}
try {
while (!line.contains("<div class=\'contributor\'>") && line != null) {
//count++;
//System.out.println(count);
if (line.contains("<p>Can you define it?</p>")) {
return "No Definition";
}
//if(line.contains("</div><div class='example'>")){count++;}
line += br.readLine();
}
line = line.replace("</div><div class='contributor'>", "");
line = line.replace("</div><div class='example'>", "\n\nⓔⓧⓐⓜⓟⓛⓔ:\n\n");
line = line.replace("<br/>", "\n");
line = line.replace(""", "\"");
line = line.replace("'", "\'");
} catch (Exception ex3) {
break;
}
String temp = "";
if (line.contains("<")) {
int l = line.length() - ((line.replaceAll("<", "")).length());
for (int xx = 0; xx < l; xx++) {
int a = line.indexOf("<");
int b = line.indexOf(">");
if (a == -1) {
continue;
}
String t;
try {
t = line.substring(a, b + 1);
} catch (Exception ex) {
t = line.substring(a, b);
}
line = line.replace(t, "");
}
line = line.replace("<", "<");
line = line.replace(">", ">");
}
//System.out.println(t+"====");
if (!line.equals(null)) {
storage[numDef] = line;
}
numDef++;
}
br.close();
for (int xp = 0; xp < numDef; xp++) {
int ran = (int) (Math.random() * numDef);
if (!(storage[ran].length() > 2000 - top.length())) {
String t = storage[ran].substring(0, storage[ran].length() - 3);
if (!t.contains("ⓔⓧⓐⓜⓟⓛⓔ:")) {
storage[ran] = storage[ran].replace("ⓔⓧⓐⓜⓟⓛⓔ:\n\n", "ⓝⓞ ⓔⓧⓐⓜⓟⓛⓔ ⓕⓞⓤⓝⓓ");
}
return top + storage[ran];
}
}
} catch (Exception ex) {
//return ex.toString();
System.out.println(ex.toString());
}
//System.out.println("An error has occurred! Please contact Bittle");
} catch (Exception e) {
e.printStackTrace();
}
return "No Definition found";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
kik.a.e.c.a caller = new kik.a.e.c.a();
caller.sendMessage(result);
}
public static void main(String[][] args) {
//This is how you call the async task
String line = "!ud hi";
line = line.replace("\\s+", " "); //This incase someone puts double space for some reason
if (line.split(" ")[0].equals("!ud")) //To make sure its !ud
new UrbanDictionary().execute(line);
}
}
Comments
0 B
|👍
/👎