package de.metalmatze.krautreporter;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.metalmatze.krautreporter.models.ArticleModel;

public class KrautreporterRssParser {

    private ArrayList<ArticleModel> articles;
    private ArticleModel article;
    private String text;
    private static final String ns = null;
    protected XmlPullParser parser;

    public KrautreporterRssParser() throws XmlPullParserException {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        this.parser = factory.newPullParser();
        this.articles = new ArrayList<ArticleModel>();
        this.article = new ArticleModel();
    }

    public ArrayList<ArticleModel> parse(InputStream inputStream) throws XmlPullParserException, IOException {
        this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        this.parser.setInput(inputStream, "UTF-8");

        int eventType = this.parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {

            String tagName = this.parser.getName();

            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagName.equalsIgnoreCase("item")) {
                        this.article = new ArticleModel();
                    }
                    break;
                case XmlPullParser.TEXT:
                    this.text = this.parser.getText();
                    break;
                case XmlPullParser.END_TAG:
                    if (tagName.equalsIgnoreCase("item"))
                    {
                        this.articles.add(this.article);
                    }
                    else if (tagName.equalsIgnoreCase("guid"))
                    {
                        this.article.uuid = this.text;
                    }
                    else if (tagName.equalsIgnoreCase("title"))
                    {
                        this.article.title = this.text;
                    }
                    else if (tagName.equalsIgnoreCase("pubDate"))
                    {
                        this.article.date = this.parseDate(this.text);
                    }
                    else if (tagName.equalsIgnoreCase("link"))
                    {
                        this.article.link = this.text;
                    }
                    else if (tagName.equalsIgnoreCase("description"))
                    {
                        String content = this.parseContent(this.text);
                        this.article.content = content;
                    }
                    break;
                default:
                    break;
            }

            eventType = this.parser.next();
        }

        return this.articles;
    }

    private Date parseDate(String text) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);

        Date date = null;
        try {
            date = dateFormat.parse(text);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }

    private String parseContent(String text) {

        this.parseTeaserImage(text);

        return text;
    }

    private void parseTeaserImage(String text) {
        Pattern pattern = Pattern.compile("(<img src=')(.*(teaser_image).*)('\\/>)");
        Matcher matcher = pattern.matcher(text);

        while (matcher.find())
        {
            this.article.image = "https://krautreporter.de" + matcher.group(2);
        }
    }

}

@@ -629,51 +629,8 @@
 xt;%0A
-    private static final String ns = null;%0A
     
@@ -931,28 +931,16 @@
 rayList%3C
-ArticleModel
 %3E();%0A   
@@ -3449,32 +3449,39 @@
 text) %7B%0A%0A       
+ text =
  this.parseTease
@@ -3489,24 +3489,64 @@
 Image(text);
+%0A        text = this.parseExcerpt(text);
 %0A%0A        re
@@ -3579,12 +3579,14 @@
 ate 
-void
+String
  par
@@ -3696,17 +3696,37 @@
 )('%5C%5C/%3E)
-%22
+(.*)%22, Pattern.DOTALL
 );%0A     
@@ -3891,25 +3891,448 @@
 oup(2);%0A
-        %7D
+%0A            return matcher.group(5);%0A        %7D%0A%0A        return text;%0A    %7D%0A%0A    private String parseExcerpt(String text) %7B%0A        Pattern pattern = Pattern.compile(%22(%3Cp%3E%3C%5C%5C/p%3E)*(%3Cp%3E(.+?)%3C%5C%5C/p%3E)(.*)%22, Pattern.DOTALL);%0A        Matcher matcher = pattern.matcher(text);%0A%0A        while (matcher.find())%0A        %7B%0A            this.article.excerpt = matcher.group(3);%0A%0A            return matcher.group(4);%0A        %7D%0A%0A        return text;
 %0A    %7D%0A%0A

Improve extracting teaser image and excerpt from rss feed