use of org.jsoup.HttpStatusException in project MusicDNA by harjot-oberai.
the class LyricsMania method fromURL.
@Reflection
public static Lyrics fromURL(String url, String artist, String title) {
String text;
try {
Document document = Jsoup.connect(url).userAgent(Net.USER_AGENT).get();
Element lyricsBody = document.getElementsByClass("lyrics-body").get(0);
// lyricsBody.select("div").last().remove();
text = Jsoup.clean(lyricsBody.html(), "", Whitelist.basic().addTags("div"));
text = text.substring(text.indexOf("</strong>") + 10, text.lastIndexOf("</div>"));
String[] keywords = document.getElementsByTag("meta").attr("name", "keywords").get(0).attr("content").split(",");
if (artist == null)
artist = document.getElementsByClass("lyrics-nav-menu").get(0).getElementsByTag("a").get(0).text();
if (title == null)
title = keywords[0];
} catch (HttpStatusException | IndexOutOfBoundsException e) {
return new Lyrics(Lyrics.NO_RESULT);
} catch (IOException e) {
return new Lyrics(ERROR);
}
if (text.startsWith("Instrumental"))
return new Lyrics(NEGATIVE_RESULT);
Lyrics lyrics = new Lyrics(POSITIVE_RESULT);
lyrics.setArtist(artist);
lyrics.setTitle(title);
lyrics.setURL(url);
lyrics.setSource(domain);
lyrics.setText(text.trim());
return lyrics;
}
Aggregations