Improve urban dictionary parsing
This commit is contained in:
parent
1421d52598
commit
d5664eb646
@ -76,33 +76,22 @@ public class UrbanDictionaryCommand implements MessageCommand
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> htmlMeanings = new ArrayList<>();
|
List<String> contributorsNames = new ArrayList<>();
|
||||||
List<String> htmlExamples = new ArrayList<>();
|
List<String> submissionDates = new ArrayList<>();
|
||||||
|
|
||||||
Elements definitions = doc.getElementsByClass("definition");
|
|
||||||
for(Element element : definitions)
|
|
||||||
{
|
|
||||||
Elements meanings = element.getElementsByClass("meaning");
|
|
||||||
for(Element meaning : meanings)
|
|
||||||
{
|
|
||||||
htmlMeanings.add(meaning.html());
|
|
||||||
break;// just one meaning per definition
|
|
||||||
}
|
|
||||||
|
|
||||||
Elements examples = element.getElementsByClass("example");
|
|
||||||
for(Element example : examples)
|
|
||||||
{
|
|
||||||
htmlExamples.add(example.html());
|
|
||||||
break; // just one example per definition
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> plaintextMeanings = new ArrayList<>();
|
List<String> plaintextMeanings = new ArrayList<>();
|
||||||
List<String> plaintextExamples = new ArrayList<>();
|
List<String> plaintextExamples = new ArrayList<>();
|
||||||
|
|
||||||
for(String htmlMeaning : htmlMeanings)
|
Elements definitions = doc.getElementsByClass("definition");
|
||||||
|
for(Element definition : definitions)
|
||||||
{
|
{
|
||||||
String text = htmlMeaning
|
Elements meaningSingleton = definition.getElementsByClass("meaning");
|
||||||
|
if(meaningSingleton.isEmpty())
|
||||||
|
{
|
||||||
|
plaintextMeanings.add(" ");
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
Element meaning = meaningSingleton.get(0);
|
||||||
|
String text = meaning.html()
|
||||||
.replaceAll("<br\\s*?>", "\n") // keep newlines
|
.replaceAll("<br\\s*?>", "\n") // keep newlines
|
||||||
.replaceAll("<.*?>", ""); // remove all other html tags
|
.replaceAll("<.*?>", ""); // remove all other html tags
|
||||||
// discord only allows 1024 characters for embed fields
|
// discord only allows 1024 characters for embed fields
|
||||||
@ -110,9 +99,15 @@ public class UrbanDictionaryCommand implements MessageCommand
|
|||||||
plaintextMeanings.add(text);
|
plaintextMeanings.add(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(String htmlExample : htmlExamples)
|
Elements exampleSingleton = definition.getElementsByClass("example");
|
||||||
|
|
||||||
|
if(exampleSingleton.isEmpty())
|
||||||
{
|
{
|
||||||
String text = htmlExample
|
plaintextExamples.add(" ");
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
Element example = exampleSingleton.get(0);
|
||||||
|
String text = example.html()
|
||||||
.replaceAll("<br\\s*?>", "\n") // keep newlines
|
.replaceAll("<br\\s*?>", "\n") // keep newlines
|
||||||
.replaceAll("<.*?>", ""); // remove all other html tags
|
.replaceAll("<.*?>", ""); // remove all other html tags
|
||||||
// discord only allows 1024 characters for embed fields
|
// discord only allows 1024 characters for embed fields
|
||||||
@ -120,16 +115,40 @@ public class UrbanDictionaryCommand implements MessageCommand
|
|||||||
plaintextExamples.add(text);
|
plaintextExamples.add(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Elements contributorSingleton = definition.getElementsByClass("contributor");
|
||||||
|
if(contributorSingleton.isEmpty())
|
||||||
|
{
|
||||||
|
contributorsNames.add("Unknown");
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
Element contributor = contributorSingleton.get(0);
|
||||||
|
|
||||||
|
String htmlContributor = contributor.html();
|
||||||
|
String htmlContributorName = contributor.select("a").html();
|
||||||
|
String htmlSubmitDate = htmlContributor.substring(
|
||||||
|
htmlContributor.indexOf("</a>") + 4);
|
||||||
|
|
||||||
|
contributorsNames.add(htmlContributorName
|
||||||
|
.replaceAll("<.*?>", "")); // remove all html tags;
|
||||||
|
|
||||||
|
submissionDates.add(htmlSubmitDate
|
||||||
|
.replaceAll("<.*?>", "")); // remove all html tags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// make it nice to look at, compared to the html value
|
// make it nice to look at, compared to the html value
|
||||||
term = term.replaceAll("\\+", " ");
|
term = term.replaceAll("\\+", " ");
|
||||||
term = WordUtils.capitalizeFully(term);
|
term = WordUtils.capitalizeFully(term);
|
||||||
|
|
||||||
EmbedBuilder embedBuilder = new EmbedBuilder();
|
EmbedBuilder embedBuilder = new EmbedBuilder();
|
||||||
embedBuilder.setColor(Cache.getBotColor());
|
embedBuilder.setColor(Cache.getBotColor());
|
||||||
embedBuilder.setTitle("Urban Dictionary: " + term);
|
embedBuilder.setTitle(term + ", on Urban Dictionary", url);
|
||||||
embedBuilder.setAuthor(event.getAuthor().getAsTag(), null, event.getAuthor().getAvatarUrl());
|
embedBuilder.setAuthor(event.getAuthor().getAsTag(), null, event.getAuthor().getAvatarUrl());
|
||||||
embedBuilder.addField("Definition", plaintextMeanings.get(0), false);
|
embedBuilder.addField("Definition", plaintextMeanings.get(0), false);
|
||||||
embedBuilder.addField("Example", plaintextExamples.get(0), false);
|
embedBuilder.addField("Example", plaintextExamples.get(0), false);
|
||||||
|
embedBuilder.addField("Submission",
|
||||||
|
"*sent by " + contributorsNames.get(0) + " on " + submissionDates.get(0) + "*",
|
||||||
|
false);
|
||||||
|
|
||||||
event.getChannel().sendMessageEmbeds(embedBuilder.build()).queue();
|
event.getChannel().sendMessageEmbeds(embedBuilder.build()).queue();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user