net.dv8tion.discord.util.SearchResult.java Source code

Java tutorial

Introduction

Here is the source code for net.dv8tion.discord.util.SearchResult.java

Source

/**
 *     Copyright 2015-2016 Austin Keener
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.dv8tion.discord.util;

import org.apache.commons.lang3.StringEscapeUtils;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class SearchResult {
    private String title;
    private String content;
    private String url;

    public static SearchResult fromGoogle(JSONObject googleResult) {
        SearchResult result = new SearchResult();
        result.title = cleanString(googleResult.getString("title"));
        result.content = cleanString(googleResult.getString("snippet"));
        try {
            result.url = URLDecoder.decode(cleanString(googleResult.getString("link")), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    public String getTitle() {
        return content;
    }

    public String getContent() {
        return content;
    }

    public String getUrl() {
        return url;
    }

    public String getSuggestedReturn() {
        return url + " - *" + title + "*: \"" + content + "\"";
    }

    private static String cleanString(String uncleanString) {
        return StringEscapeUtils.unescapeJava(StringEscapeUtils.unescapeHtml4(
                uncleanString.replaceAll("\\s+", " ").replaceAll("\\<.*?>", "").replaceAll("\"", "")));
    }
}