package com.shirwa.simplistic_rss;

import org.joda.time.DateTime;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * Copyright (C) 2014 Shirwa Mohamed <shirwa99@gmail.com>
 *
 * 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.
 */

public class RssItem implements RssThing {
    static final String tagPattern = "<[^>]*>";
    // < = &lt;, > = &gt; URL in Group 3
    static final Pattern imgPattern =
            Pattern.compile("(&lt;|<)img.*?src=(\"|')(.*?)(\"|')",
                    Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

    String title;
    String description;
    String link;
    String imageUrl;
    DateTime pubDate;
    boolean notLookedForImg = true;
    // Description but only first X chars without formatting
    int snippetLen = 200;
    String snippet;
    String plainTitle;

    public DateTime getPubDate() {
        return pubDate;
    }

    public void setPubDate(final DateTime pubDate) {
        this.pubDate = pubDate;
    }

    public void appendTitle(String title) {
        if (this.title == null) {
            this.title = "";
        }
        this.title += title;
    }

    public void appendDescription(String description) {
        if (this.description == null) {
            this.description = "";
        }
        this.description += description;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImageUrl() {
        if (imageUrl != null) {
            return imageUrl;
        }
        if (notLookedForImg && description != null) {
            notLookedForImg = false;
            // Try and find an image in the item
            Matcher m = imgPattern.matcher(description);
            if (m.find()) {
                imageUrl = m.group(3);
            }
        }
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    /**
     * Return a list of all images in the body
     */
    public List<String> getAllImageUrls() {
        ArrayList<String> urlList = new ArrayList<String>();
        if (description != null) {
            Matcher m = imgPattern.matcher(description);
            while (m.find()) {
                urlList.add(m.group(3));
            }
        }
        return urlList;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getSnippet() {
        if (snippet == null && description != null && !description.isEmpty()) {
            snippet = description.replaceAll(tagPattern, "")
                    .replaceAll("\\s+", " ");
            if (snippetLen > snippet.length()) {
                snippetLen = snippet.length();
            }
            if (snippetLen == 0) {
                // done
                snippet = null;
            } else {
                // Unicode ellipsis instead of three dots ...
                snippet = snippet.substring(0, snippetLen) + "\u2026";
                snippet = android.text.Html.fromHtml(snippet).toString().trim();
            }
        }

        return snippet;
    }

    public String getPlainTitle() {
        if (plainTitle == null && this.title != null) {
            plainTitle =
                    android.text.Html.fromHtml(this.title).toString().trim();
        }
        return plainTitle;
    }
}

@@ -1112,34 +1112,1045 @@
     
-String title;%0A    String d
+// Bloat patterns, are removed from description%0A    static final Pattern%5B%5D bloatPatterns = new Pattern%5B%5D%7B%0A            // Remove feedflare div%0A            Pattern.compile(%0A                    %22(%3C%7C&lt;)div class=('%7C%5C%22)feedflare('%7C%5C%22).*?/div(%3E%7C&gt;)%22,%0A                    Pattern.CASE_INSENSITIVE %7C Pattern.DOTALL),%0A            // Remove feedsportal links%0A            Pattern.compile(%0A                    %22(%3C%7C&lt;)a((?!/a).)*feedsportal.*?/a(%3E%7Cgt;)%22,%0A                    Pattern.CASE_INSENSITIVE %7C Pattern.DOTALL),%0A            // Remove links containing zero size images%0A            Pattern.compile(%0A                    %22(%3C%7C&lt;)a((?!/a).)*width=('%7C%5C%22)1('%7C%5C%22)((?!/a).)*/a(%3E%7C&gt;)%22,%0A                    Pattern.CASE_INSENSITIVE %7C Pattern.DOTALL),%0A            // Remove zero size images%0A            Pattern.compile(%0A                    %22(%3C%7C&lt;)img((?!/((%3E%7C&gt;)%7Cimg)).)*width=('%7C%5C%22)1('%7C%5C%22).*?/(img)?(%3E%7C&gt;)%22,%0A                    Pattern.CASE_INSENSITIVE %7C Pattern.DOTALL),%0A    %7D;%0A%0A    String title;%0A    String description;%0A    String cleanD
 escr
@@ -3048,32 +3048,449 @@
 ription;%0A    %7D%0A%0A
+    /**%0A     *%0A     * @return description without bloat, ads, and spam%0A     */%0A    public String getCleanDescription() %7B%0A        if (cleanDescription == null && description != null) %7B%0A            cleanDescription = description;%0A            for (Pattern p: bloatPatterns) %7B%0A                cleanDescription = p.matcher(cleanDescription).replaceAll(%22%22);%0A            %7D%0A        %7D%0A%0A        return cleanDescription;%0A    %7D%0A%0A
     public Strin

Clean bloat from descriptions