com.github.breadmoirai.samurai.util.SearchResult.java Source code

Java tutorial

Introduction

Here is the source code for com.github.breadmoirai.samurai.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 com.github.breadmoirai.samurai.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 title;
    }

    public String getContent() {
        return content;
    }

    public String getUrl() {
        return url;
    }

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

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("SearchResult{");
        sb.append("title='").append(title).append('\'');
        sb.append(", content='").append(content).append('\'');
        sb.append(", url='").append(url).append('\'');
        sb.append('}');
        return sb.toString();
    }
}