Search in sources :

Example 81 with JsonParser

use of com.google.gson.JsonParser in project MusicDNA by harjot-oberai.

the class Genius method search.

public static ArrayList<Lyrics> search(String query) {
    ArrayList<Lyrics> results = new ArrayList<>();
    query = Normalizer.normalize(query, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
    JsonObject response = null;
    try {
        URL queryURL = new URL(String.format("http://api.genius.com/search?q=%s", URLEncoder.encode(query, "UTF-8")));
        Connection connection = Jsoup.connect(queryURL.toExternalForm()).header("Authorization", "Bearer " + Config.GENIUS).timeout(0).ignoreContentType(true);
        Document document = connection.userAgent(Net.USER_AGENT).get();
        response = new JsonParser().parse(document.text()).getAsJsonObject();
    } catch (JsonSyntaxException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (response == null || response.getAsJsonObject("meta").get("status").getAsInt() != 200)
        return results;
    JsonArray hits = response.getAsJsonObject("response").getAsJsonArray("hits");
    int processed = 0;
    while (processed < hits.size()) {
        JsonObject song = hits.get(processed).getAsJsonObject().getAsJsonObject("result");
        String artist = song.getAsJsonObject("primary_artist").get("name").getAsString();
        String title = song.get("title").getAsString();
        String url = "http://genius.com/songs/" + song.get("id").getAsString();
        Lyrics l = new Lyrics(Lyrics.SEARCH_ITEM);
        l.setArtist(artist);
        l.setTitle(title);
        l.setURL(url);
        l.setSource("Genius");
        results.add(l);
        processed++;
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) Connection(org.jsoup.Connection) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) URL(java.net.URL) JsonArray(com.google.gson.JsonArray) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonParser(com.google.gson.JsonParser)

Example 82 with JsonParser

use of com.google.gson.JsonParser 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 83 with JsonParser

use of com.google.gson.JsonParser in project MusicDNA by harjot-oberai.

the class Bollywood method search.

public static ArrayList<Lyrics> search(String query) {
    ArrayList<Lyrics> results = new ArrayList<>();
    String searchUrl = "https://api.quicklyric.be/bollywood/search?q=%s";
    try {
        String jsonText;
        jsonText = Net.getUrlAsString(String.format(searchUrl, URLEncoder.encode(query, "utf-8")));
        JsonObject jsonResponse = new JsonParser().parse(jsonText).getAsJsonObject();
        JsonArray lyricsResults = jsonResponse.getAsJsonArray("lyrics");
        if (lyricsResults != null)
            for (int i = 0; i < lyricsResults.size(); ++i) {
                JsonObject lyricsResult = lyricsResults.get(i).getAsJsonObject();
                JsonArray tags = lyricsResult.get("tags").getAsJsonArray();
                Lyrics lyrics = new Lyrics(Lyrics.SEARCH_ITEM);
                lyrics.setTitle(lyricsResult.get("name").getAsString());
                for (int j = 0; j < tags.size(); ++j) {
                    JsonObject tag = tags.get(j).getAsJsonObject();
                    if (tag.get("tag_type").getAsString().equals("Singer")) {
                        lyrics.setArtist(tag.get("name").getAsString().trim());
                        break;
                    }
                }
                lyrics.setURL("https://api.quicklyric.be/bollywood/get?id=" + lyricsResult.get("id").getAsInt());
                results.add(lyrics);
            }
    } catch (IOException | JsonParseException e) {
        e.printStackTrace();
    }
    return results;
}
Also used : JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException) JsonParser(com.google.gson.JsonParser)

Example 84 with JsonParser

use of com.google.gson.JsonParser in project bigbluebutton by bigbluebutton.

the class UserConnectedToGlobalAudio method fromJson.

public static UserConnectedToGlobalAudio fromJson(String message) {
    JsonParser parser = new JsonParser();
    JsonObject obj = (JsonObject) parser.parse(message);
    if (obj.has("header") && obj.has("payload")) {
        JsonObject header = (JsonObject) obj.get("header");
        JsonObject payload = (JsonObject) obj.get("payload");
        if (header.has("name")) {
            String messageName = header.get("name").getAsString();
            if (messageName == USER_CONNECTED_TO_GLOBAL_AUDIO) {
                if (payload.has(Constants.VOICE_CONF) && payload.has(Constants.USER_ID) && payload.has(Constants.NAME)) {
                    String voiceConf = payload.get(Constants.VOICE_CONF).getAsString();
                    String userid = payload.get(Constants.USER_ID).getAsString();
                    String name = payload.get(Constants.NAME).getAsString();
                    return new UserConnectedToGlobalAudio(voiceConf, userid, name);
                }
            }
        }
    }
    return null;
}
Also used : JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Example 85 with JsonParser

use of com.google.gson.JsonParser in project bigbluebutton by bigbluebutton.

the class UserDisconnectedFromGlobalAudio method fromJson.

public static UserDisconnectedFromGlobalAudio fromJson(String message) {
    JsonParser parser = new JsonParser();
    JsonObject obj = (JsonObject) parser.parse(message);
    if (obj.has("header") && obj.has("payload")) {
        JsonObject header = (JsonObject) obj.get("header");
        JsonObject payload = (JsonObject) obj.get("payload");
        if (header.has("name")) {
            String messageName = header.get("name").getAsString();
            if (messageName == USER_DISCONNECTED_FROM_GLOBAL_AUDIO) {
                if (payload.has(Constants.VOICE_CONF) && payload.has(Constants.USER_ID) && payload.has(Constants.NAME)) {
                    String voiceConf = payload.get(Constants.VOICE_CONF).getAsString();
                    String userid = payload.get(Constants.USER_ID).getAsString();
                    String name = payload.get(Constants.NAME).getAsString();
                    return new UserDisconnectedFromGlobalAudio(voiceConf, userid, name);
                }
            }
        }
    }
    return null;
}
Also used : JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonParser (com.google.gson.JsonParser)356 JsonObject (com.google.gson.JsonObject)285 JsonElement (com.google.gson.JsonElement)105 JsonArray (com.google.gson.JsonArray)57 IOException (java.io.IOException)43 Gson (com.google.gson.Gson)40 InputStreamReader (java.io.InputStreamReader)32 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)20 Map (java.util.Map)18 Type (java.lang.reflect.Type)15 JsonReader (com.google.gson.stream.JsonReader)12 Test (org.junit.Test)12 URL (java.net.URL)11 AssetManager (android.content.res.AssetManager)10 InputStream (java.io.InputStream)10 GsonBuilder (com.google.gson.GsonBuilder)9 UserType (com.glitchcog.fontificator.bot.UserType)8 EmojiType (com.glitchcog.fontificator.emoji.EmojiType)8 JsonParseException (com.google.gson.JsonParseException)8