Search in sources :

Example 6 with HttpStatusException

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;
}
Also used : Element(org.jsoup.nodes.Element) HttpStatusException(org.jsoup.HttpStatusException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Reflection(com.sdsmdg.harjot.MusicDNA.annotations.Reflection)

Aggregations

IOException (java.io.IOException)6 HttpStatusException (org.jsoup.HttpStatusException)6 Document (org.jsoup.nodes.Document)6 Pattern (java.util.regex.Pattern)4 Matcher (java.util.regex.Matcher)3 Reflection (com.sdsmdg.harjot.MusicDNA.annotations.Reflection)1 Connection (org.jsoup.Connection)1 Element (org.jsoup.nodes.Element)1 Elements (org.jsoup.select.Elements)1 Test (org.junit.Test)1