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;
}
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);
}
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);
}
}
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;
}
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);
}
}
Aggregations