Search in sources :

Example 6 with Reflection

use of com.sdsmdg.harjot.MusicDNA.annotations.Reflection 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

Reflection (com.sdsmdg.harjot.MusicDNA.annotations.Reflection)6 IOException (java.io.IOException)5 Document (org.jsoup.nodes.Document)5 Element (org.jsoup.nodes.Element)3 JsonObject (com.google.gson.JsonObject)2 JsonParseException (com.google.gson.JsonParseException)2 JsonParser (com.google.gson.JsonParser)2 Net.getUrlAsString (com.sdsmdg.harjot.MusicDNA.utilities.Net.getUrlAsString)2 URL (java.net.URL)2 Elements (org.jsoup.select.Elements)2 JsonArray (com.google.gson.JsonArray)1 ArrayList (java.util.ArrayList)1 HttpStatusException (org.jsoup.HttpStatusException)1