Search in sources :

Example 1 with Reflection

use of com.sdsmdg.harjot.MusicDNA.annotations.Reflection in project MusicDNA by harjot-oberai.

the class MetalArchives method fromMetaData.

@Reflection
public static Lyrics fromMetaData(String artist, String title) {
    String baseURL = "http://www.metal-archives.com/search/ajax-advanced/searching/songs/?bandName=%s&songTitle=%s&releaseType[]=1&exactSongMatch=1&exactBandMatch=1";
    String urlArtist = artist.replaceAll("\\s", "+");
    String urlTitle = title.replaceAll("\\s", "+");
    String url;
    String text;
    try {
        String response = Net.getUrlAsString(String.format(baseURL, urlArtist, urlTitle));
        JsonObject jsonResponse = new JsonParser().parse(response).getAsJsonObject();
        JsonArray track = jsonResponse.getAsJsonArray("aaData").get(0).getAsJsonArray();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < track.size(); i++) builder.append(track.get(i).getAsString());
        Document trackDocument = Jsoup.parse(builder.toString());
        url = trackDocument.getElementsByTag("a").get(1).attr("href");
        String id = trackDocument.getElementsByClass("viewLyrics").get(0).id().substring(11);
        text = Jsoup.connect("http://www.metal-archives.com/release/ajax-view-lyrics/id/" + id).get().body().html();
    } catch (JsonParseException | IndexOutOfBoundsException e) {
        return new Lyrics(NO_RESULT);
    } catch (Exception e) {
        return new Lyrics(ERROR);
    }
    Lyrics lyrics = new Lyrics(POSITIVE_RESULT);
    lyrics.setArtist(artist);
    lyrics.setTitle(title);
    lyrics.setText(text);
    lyrics.setSource(domain);
    lyrics.setURL(url);
    return lyrics;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) Document(org.jsoup.nodes.Document) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException) JsonParser(com.google.gson.JsonParser) Reflection(com.sdsmdg.harjot.MusicDNA.annotations.Reflection)

Example 2 with Reflection

use of com.sdsmdg.harjot.MusicDNA.annotations.Reflection in project MusicDNA by harjot-oberai.

the class JLyric method fromMetaData.

@Reflection
public static Lyrics fromMetaData(String artist, String song) {
    if ((artist == null) || (song == null))
        return new Lyrics(Lyrics.ERROR);
    String encodedArtist;
    String encodedSong;
    String url;
    try {
        encodedArtist = URLEncoder.encode(artist, "UTF-8");
        encodedSong = URLEncoder.encode(song, "UTF-8");
        Document searchPage = Jsoup.connect(String.format(baseUrl, encodedArtist, encodedSong)).userAgent(Net.USER_AGENT).get();
        if (!searchPage.location().startsWith("http://search.j-lyric.net/"))
            throw new IOException("Redirected to wrong domain " + searchPage.location());
        Elements artistBlocks = searchPage.body().select("div#lyricList");
        //@todo give all results
        if (artistBlocks.first() == null) {
            Lyrics lyrics = new Lyrics(Lyrics.NO_RESULT);
            lyrics.setArtist(artist);
            lyrics.setTitle(song);
            return lyrics;
        }
        url = artistBlocks.first().select("div.title a").attr("href");
    } catch (IOException e) {
        e.printStackTrace();
        return new Lyrics(Lyrics.ERROR);
    }
    return fromURL(url, artist, song);
}
Also used : IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Reflection(com.sdsmdg.harjot.MusicDNA.annotations.Reflection)

Example 3 with Reflection

use of com.sdsmdg.harjot.MusicDNA.annotations.Reflection in project MusicDNA by harjot-oberai.

the class Lololyrics method fromMetaData.

@Reflection
public static Lyrics fromMetaData(String artist, String song) {
    if ((artist == null) || (song == null))
        return new Lyrics(Lyrics.ERROR);
    try {
        String encodedArtist = URLEncoder.encode(artist, "UTF-8");
        String encodedSong = URLEncoder.encode(song, "UTF-8");
        String url = String.format(baseUrl, encodedArtist, encodedSong);
        String body = Jsoup.connect(url).execute().body();
        Document lololyrics = Jsoup.parse(body.replaceAll("(\\n)", "<br />"));
        Element loloResult = lololyrics.select("result").first();
        if (loloResult == null || loloResult.select("status") == null || !"OK".equals(loloResult.select("status").text()))
            return new Lyrics(Lyrics.NO_RESULT);
        if (loloResult.select("response").hasText()) {
            Lyrics lyrics = new Lyrics(Lyrics.POSITIVE_RESULT);
            lyrics.setArtist(artist);
            lyrics.setTitle(song);
            String text = Parser.unescapeEntities(loloResult.select("response").html(), true);
            lyrics.setText(text);
            lyrics.setSource(domain);
            if (loloResult.select("cover").hasText())
                lyrics.setCoverURL(loloResult.select("cover").text());
            String weburl = loloResult.select("url").html();
            lyrics.setURL(weburl);
            return lyrics;
        } else {
            return new Lyrics(Lyrics.NO_RESULT);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return new Lyrics(Lyrics.ERROR);
    }
}
Also used : Element(org.jsoup.nodes.Element) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Reflection(com.sdsmdg.harjot.MusicDNA.annotations.Reflection)

Example 4 with Reflection

use of com.sdsmdg.harjot.MusicDNA.annotations.Reflection in project MusicDNA by harjot-oberai.

the class LyricWiki method search.

@Reflection
public static ArrayList<Lyrics> search(String query) {
    ArrayList<Lyrics> results = new ArrayList<>();
    query = query + " song";
    query = Normalizer.normalize(query, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
    try {
        URL queryURL = new URL(String.format(baseSearchUrl, URLEncoder.encode(query, "UTF-8")));
        Document searchpage = Jsoup.connect(queryURL.toExternalForm()).timeout(0).get();
        Elements searchResults = searchpage.getElementsByClass("Results");
        if (searchResults.size() >= 1) {
            searchResults = searchResults.get(0).getElementsByClass("result");
            for (Element searchResult : searchResults) {
                String[] tags = searchResult.getElementsByTag("h1").text().split(":");
                if (tags.length != 2)
                    continue;
                String url = searchResult.getElementsByTag("a").attr("href");
                Lyrics lyrics = new Lyrics(SEARCH_ITEM);
                lyrics.setArtist(tags[0]);
                lyrics.setTitle(tags[1]);
                lyrics.setURL(url);
                lyrics.setSource(domain);
                results.add(lyrics);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return results;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Net.getUrlAsString(com.sdsmdg.harjot.MusicDNA.utilities.Net.getUrlAsString) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) URL(java.net.URL) Reflection(com.sdsmdg.harjot.MusicDNA.annotations.Reflection)

Example 5 with Reflection

use of com.sdsmdg.harjot.MusicDNA.annotations.Reflection in project MusicDNA by harjot-oberai.

the class LyricWiki method fromMetaData.

@Reflection
public static Lyrics fromMetaData(String artist, String title) {
    if ((artist == null) || (title == null))
        return new Lyrics(ERROR);
    String originalArtist = artist;
    String originalTitle = title;
    String url = null;
    try {
        String encodedArtist = URLEncoder.encode(artist, "UTF-8");
        String encodedSong = URLEncoder.encode(title, "UTF-8");
        JsonObject json = new JsonParser().parse(getUrlAsString(new URL(String.format(baseUrl, encodedArtist, encodedSong))).replace("song = ", "")).getAsJsonObject();
        url = URLDecoder.decode(json.get("url").getAsString(), "UTF-8");
        artist = json.get("artist").getAsString();
        title = json.get("song").getAsString();
        encodedArtist = URLEncoder.encode(artist, "UTF-8");
        encodedSong = URLEncoder.encode(title, "UTF-8");
        json = new JsonParser().parse(getUrlAsString(new URL(String.format(baseAPIUrl, encodedArtist, encodedSong)))).getAsJsonObject().get("result").getAsJsonObject();
        Lyrics lyrics = new Lyrics(POSITIVE_RESULT);
        lyrics.setArtist(artist);
        lyrics.setTitle(title);
        lyrics.setText(json.get("lyrics").getAsString().replaceAll("\n", "<br />"));
        lyrics.setURL(url);
        lyrics.setOriginalArtist(originalArtist);
        lyrics.setOriginalTitle(originalTitle);
        return lyrics;
    } catch (JsonParseException e) {
        return new Lyrics(NO_RESULT);
    } catch (IOException | IllegalStateException | NullPointerException e) {
        return url == null ? new Lyrics(ERROR) : fromURL(url, originalArtist, originalTitle);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) Net.getUrlAsString(com.sdsmdg.harjot.MusicDNA.utilities.Net.getUrlAsString) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) URL(java.net.URL) JsonParser(com.google.gson.JsonParser) 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