# coding:utf-8
"""
@file
@brief About RSS
"""
import datetime
import os
import re
from pyquickhelper.loghelper import fLOG
from .filefunction import find_all_blogs_function


modelForARSSFeed = """<rss version="2.0">
                        <channel>
                            <title>XD blog</title>
                            <link>http://www.xavierdupre.fr/blog/xd_blog_nojs.html</link>
                            <description>new posts from XD blog</description>
                            """.replace("                        ", "")

modelForARSSRow = """
        <item>
            <title>%s</title>
            <link>http://www.xavierdupre.fr/blog/%s_nojs.html</link>
            <guid isPermaLink="true">http://www.xavierdupre.fr/blog/%s_nojs.html</guid>
            <description>%s</description>
            <pubDate>%s</pubDate>
        </item>"""

modelForARSSChannel = """\n</channel>\n</rss>\n"""


def file_build_rss(folder=".",
                   outfile="blog/xdbrss.xml",
                   now=datetime.datetime.now(),
                   model_feed=modelForARSSFeed,
                   model_row=modelForARSSRow,
                   model_channel=modelForARSSChannel):
    """
    Build a RSS file, the function keeps the blog post (HTML format) from the last month.
    If a post contains one the two following string:
    @code
    <!-- SUMMARY BEGINS -->
    <!-- SUMMARY ENDS -->
    @endcode
    The summary will only contains the part included in those two comments.


    @param  folder          folder where the blog post can be found
    @param  outfile         final file to produce
    @param  now             date to use as a final date, only blog post between one month now and now will be kept
    @param  model_feed      see model_channel
    @param  model_row       see model_row
    @param  model_channel   the part related to a post in the rss stream is composed
                            by the concatenation of the three stream:
                            @code
                            model_feed
                            model_row
                            model_channel
                            @endcode
                            You should see the default value to see how you can replace them.
    @return                 2-uple: outfile and the list of kept blog post (the last month)
    """

    now -= datetime.datetime(2000, 2, 1) - datetime.datetime(2000, 1, 1)
    fLOG("now - month ", now)
    file = find_all_blogs_function(folder)
    exp = re.compile('<meta +name=\\"description\\" +content=\\"(.*?)\\" */>')
    expt = re.compile('<title>(.*?)</title>')

    keepfiles = []
    rss = []
    for f in file:
        temp = os.path.split(f)[-1].lower().replace(".html", "")
        day = datetime.datetime(int(temp[:4]), int(temp[5:7]), int(temp[8:10]))
        if day > now:
            keepfiles.append(f)

            ff = open(f, "r", encoding="utf8")
            t = ff.read().replace("\n", " ").replace("\r", " ")
            ff.close()
            check_encoding(f)

            summary = exp.search(t)
            title = expt.search(t)

            if not title:
                raise ValueError("unable to find title in " + f)
            fLOG("getting summary for ", f)

            title = title.groups()[0]
            summary = None if summary is None else summary.groups()[0]
            adddots = False

            if summary is None or len(summary) == 0:
                if "<!-- SUMMARY BEGINS -->" in t and "<!-- SUMMARY ENDS -->" in t:
                    p0 = t.find("<!-- SUMMARY BEGINS -->")
                    p1 = t.find("<!-- SUMMARY ENDS -->")
                    summary = t[
                        p0 + len("<!-- SUMMARY BEGINS -->"):p1].strip(" \n\r\t")
                    summary = summary.replace("<", "&lt;")
                    summary = summary.replace(">", "&gt;")
                    adddots = True

                if summary is None or len(summary) == 0:
                    p0 = t.find("<body>")
                    p1 = t.find("</body>")
                    summary = t[p0 + len("<body>"):p1].strip(" \n\r\t")
                    summary = summary.replace("<", "&lt;")
                    summary = summary.replace(">", "&gt;")

                if summary is None or len(summary) == 0:
                    raise ValueError("summary is empty for blog " + f)

            summary = re.sub(r"\s+", " ", summary)
            rss.append((day, f, summary, temp, title))

    rows = ["<?xml version=\"1.0\" encoding=\"utf-8\"?>"]
    rows.append(modelForARSSFeed)

    rss.sort(reverse=True)
    for day, f, summary, short, title in rss:
        if adddots and not summary.endswith("..."):
            summary += " suite..." if not summary.endswith(
                ".") else " suite..."

        row = modelForARSSRow % (title, short, short, summary, str(day))
        rows.append(row)

    rows.append(modelForARSSChannel)
    content = "\n".join(rows)
    rssf = open(outfile, "w", encoding='utf-8')
    rssf.write(content)
    rssf.close()

    return outfile, keepfiles


def check_encoding(file):
    """
    check the encoding of a file (ASCII here),
    read the file, it does not return anything
    @param      file        file to check
    """
    f = open(file, "r")
    f.read()
    f.close()

@@ -5285,24 +5285,218 @@
 r%22)%0A    
-f.read()
+try:%0A        f.read()%0A    except Exception as e:%0A        size = os.stat(file).st_size%0A        raise Exception(%0A            %22issue with file (size %7B1%7D)%5Cn  File %5C%22%7B0%7D%5C%22, line 1%22.format(file, size)) from e
 %0A    f.c

update blog automation