Skip to content
Snippets Groups Projects
SmartNews.java 4.61 KiB
Newer Older
Gianluigi Pelle's avatar
Gianluigi Pelle committed
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;


public class SmartNews {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    final private String category;
    final private String originalTitle;
    final private String plainTitle;
    final private String stemTitle;
    final private String plainDescription;
    final private String stemDescription;
    final private String url;
    final private String publicationDate;

    public SmartNews(String jsonString) throws JsonProcessingException {

        JsonNode jsonMessage = MAPPER.readTree(jsonString);
        this.category = jsonMessage.path("category").asText();
        this.originalTitle = jsonMessage.path("originalTitle").asText();
        this.plainTitle = jsonMessage.path("plainTitle").asText();
        this.stemTitle = jsonMessage.path("stemTitle").asText();
        this.plainDescription = jsonMessage.path("plainDescription").asText();
        this.stemDescription = jsonMessage.path("stemDescription").asText();
        this.url = jsonMessage.path("url").asText();
        this.publicationDate = jsonMessage.path("publicationDate").asText();
    }

    public String getCategory() {
        return category;
    }

    public String getOriginalTitle() {
        return originalTitle;
    }

    public String getPlainTitle() {
        return plainTitle;
    }

    public String getStemTitle() {
        return stemTitle;
    }

    public String getPlainDescription() {
        return plainDescription;
    }

    public String getStemDescription() {
        return stemDescription;
    }

    public String getUrl() {
        return url;
    }

    public String getPublicationDate() {
        return publicationDate;
    }

    public long getEventTimeMillis() {
        Date pubDate = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
            pubDate = sdf.parse(this.getPublicationDate());
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date().toInstant().toEpochMilli();
        }
        return pubDate.toInstant().toEpochMilli();
    }

    public String getNewsDomain() {
        String url = this.getRedirectUrl();
        URI uri = null;
        try {
            uri = new URI(url);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return "news.google.com";
        }
        String domain = uri.getHost();
        return domain.startsWith("www.") ? domain.substring(4) : domain;
    }

    public String getRedirectUrl() {
        try {
            URLConnection con = new URL(this.getUrl()).openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/5.0");
            con.connect();
            InputStream is = con.getInputStream();
            String redirectUrl = con.getURL().toString();
            is.close();
            return redirectUrl;
        } catch (MalformedURLException e) {
            //e.printStackTrace();
            return this.getUrl();
        } catch (IOException e) {
            //e.printStackTrace();
            try {
                URLConnection con = new URL(this.getUrl()).openConnection();
                con.setRequestProperty("User-Agent", "Mozilla/5.0");
                con.connect();
                String redirectUrl = con.getURL().toString();
                return redirectUrl;
            } catch (MalformedURLException ex) {
                //e.printStackTrace();
                return this.getUrl();
            } catch (IOException ex) {
                //e.printStackTrace();
                return this.getUrl();
            }
        }
    }

    public String toJsonString() {
        ObjectNode smartNews = MAPPER.getNodeFactory().objectNode();
        smartNews.put("category", this.getCategory());
        smartNews.put("originalTitle", this.getOriginalTitle());
        smartNews.put("plainTitle", this.getPlainTitle());
        smartNews.put("stemTitle", this.getStemTitle());
        smartNews.put("plainDescription", this.getPlainDescription());
        smartNews.put("stemDescription", this.getStemDescription());
        smartNews.put("url", this.getUrl());
        smartNews.put("publicationDate", this.getPublicationDate());
        return smartNews.toString();
    }

}