<commit_before>"""Our web services"""
import json
import re
import datetime

from twisted.web import resource
from twisted.python import log
from feedgen.feed import FeedGenerator
import iembot.util as botutil

XML_CACHE = {}
XML_CACHE_EXPIRES = {}


def wfo_rss(iembot, rm):
    """build a RSS for the given room"""
    if len(rm) == 4 and rm[0] == 'k':
        rm = '%schat' % (rm[-3:],)
    elif len(rm) == 3:
        rm = 'k%schat' % (rm,)
    if rm not in XML_CACHE:
        XML_CACHE[rm] = ""
        XML_CACHE_EXPIRES[rm] = -2

    # should not be empty given the caller
    lastID = iembot.chatlog[rm][0].seqnum
    if lastID == XML_CACHE_EXPIRES[rm]:
        return XML_CACHE[rm]

    rss = FeedGenerator()
    rss.generator('iembot')
    rss.title("%s IEMBot RSS Feed" % (rm,))
    rss.link(href="https://weather.im/iembot-rss/room/%s.xml" % (rm,),
             rel='self')
    rss.description("%s IEMBot RSS Feed" % (rm,))
    rss.lastBuildDate(
        datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT"))

    for entry in iembot.chatlog[rm]:
        botutil.add_entry_to_rss(entry, rss)

    XML_CACHE[rm] = rss.rss_str()
    XML_CACHE_EXPIRES[rm] = lastID
    return rss.rss_str()


class RSSService(resource.Resource):
    """Our RSS service"""

    def isLeaf(self):
        """allow uri"""
        return True

    def __init__(self, iembot):
        """Constructor"""
        resource.Resource.__init__(self)
        self.iembot = iembot

    def render(self, request):
        uri = request.uri.decode('utf-8')
        if uri.startswith("/wfo/"):
            tokens = re.findall(
                "/wfo/(k...|botstalk|...chat).xml", uri.lower())
        else:
            tokens = re.findall("/room/(.*).xml", uri.lower())
        if not tokens:
            return b"ERROR!"

        rm = tokens[0]
        if uri.startswith("/wfo/"):
            if len(rm) == 4 and rm[0] == 'k':
                rm = '%schat' % (rm[-3:],)
            elif len(rm) == 3:
                rm = 'k%schat' % (rm,)
        if not self.iembot.chatlog.get(rm, []):
            rss = FeedGenerator()
            rss.generator('iembot')
            rss.title("IEMBot RSS Feed")
            rss.link(
                href="https://weather.im/iembot-rss/room/%s.xml" % (rm,),
                rel='self')
            rss.description("Syndication of iembot messages.")
            rss.lastBuildDate(
                datetime.datetime.utcnow(
                    ).strftime("%a, %d %b %Y %H:%M:%S GMT"))
            fe = rss.add_entry()
            fe.title("IEMBOT recently restarted, no history yet")<commit_msg>bandaid on method HEAD for RSS<commit_after>"""Our web services"""
import json
import re
import datetime

from twisted.web import resource
from twisted.python import log
from feedgen.feed import FeedGenerator
import iembot.util as botutil

XML_CACHE = {}
XML_CACHE_EXPIRES = {}


def wfo_rss(iembot, rm):
    """build a RSS for the given room"""
    if len(rm) == 4 and rm[0] == 'k':
        rm = '%schat' % (rm[-3:],)
    elif len(rm) == 3:
        rm = 'k%schat' % (rm,)
    if rm not in XML_CACHE:
        XML_CACHE[rm] = ""
        XML_CACHE_EXPIRES[rm] = -2

    # should not be empty given the caller
    lastID = iembot.chatlog[rm][0].seqnum
    if lastID == XML_CACHE_EXPIRES[rm]:
        return XML_CACHE[rm]

    rss = FeedGenerator()
    rss.generator('iembot')
    rss.title("%s IEMBot RSS Feed" % (rm,))
    rss.link(href="https://weather.im/iembot-rss/room/%s.xml" % (rm,),
             rel='self')
    rss.description("%s IEMBot RSS Feed" % (rm,))
    rss.lastBuildDate(
        datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT"))

    for entry in iembot.chatlog[rm]:
        botutil.add_entry_to_rss(entry, rss)

    XML_CACHE[rm] = rss.rss_str()
    XML_CACHE_EXPIRES[rm] = lastID
    return rss.rss_str()


class RSSService(resource.Resource):
    """Our RSS service"""

    def isLeaf(self):
        """allow uri"""
        return True

    def __init__(self, iembot):
        """Constructor"""
        resource.Resource.__init__(self)
        self.iembot = iembot

    def render(self, request):
        if request.method == b'HEAD':
            return b''
        uri = request.uri.decode('utf-8')
        if uri.startswith("/wfo/"):
            tokens = re.findall(
                "/wfo/(k...|botstalk|...chat).xml", uri.lower())
        else:
            tokens = re.findall("/room/(.*).xml", uri.lower())
        if not tokens:
            return b"ERROR!"

        rm = tokens[0]
        if uri.startswith("/wfo/"):
            if len(rm) == 4 and rm[0] == 'k':
                rm = '%schat' % (rm[-3:],)
            elif len(rm) == 3:
                rm = 'k%schat' % (rm,)
        if not self.iembot.chatlog.get(rm, []):
            rss = FeedGenerator()
            rss.generator('iembot')
            rss.title("IEMBot RSS Feed")
            rss.link(
                href="https://weather.im/iembot-rss/room/%s.xml" % (rm,),
                rel='self')
            rss.description("Syndication of iembot messages.")
            rss.lastBuildDate(
                datetime.datetime.utcnow(
                    ).strftime("%a, %d %b %Y %H:%M:%S GMT"))
            fe = rss.add_entry()
            fe.title("IEMBOT recently restarted, no history yet")
{'fraction_non_alphanumeric': 0.15781544256120528, 'fraction_numerical': 0.004519774011299435, 'mean_word_length': 2.054054054054054, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1962667', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1748, 'n_words': 407}