<commit_before>import datetime
import pickle
import re
import time
from xml.etree import ElementTree

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

from models import CHPIncident
from utils import conditional_http


class AtomHandler(webapp.RequestHandler):
	def get(self):
		center = self.request.get("center")
		dispatch = self.request.get("dispatch", default_value="STCC")
		area = self.request.get("area")
		roads = self.request.get("roads")

		incidents = CHPIncident.all()
		incidents.order('-LogTime')
		if center != "":
			incidents.filter('CenterID =', center)
		if dispatch != "":
			incidents.filter('DispatchID =', dispatch)
		if area != "":
			incidents.filter('Area =', area)

		last_mod = datetime.datetime.utcnow()
		if incidents.count(1) > 0:
			last_mod = max(incidents, key=lambda incident: incident.updated).updated
			if conditional_http.isNotModified(self, last_mod):
				return

		feed = ElementTree.Element('feed', {
			'xmlns': 'http://www.w3.org/2005/Atom',
			'xmlns:georss': 'http://www.georss.org/georss'
		})

		ElementTree.SubElement(feed, 'title').text = 'Traffic'
		ElementTree.SubElement(feed, 'subtitle').text = 'Traffic incidents from the CHP'
		ElementTree.SubElement(ElementTree.SubElement(feed, 'author'), 'name').text = 'The California Highway Patrol'
		ElementTree.SubElement(feed, 'id').text = 'tag:traffic.lectroid.net,2010-06-24:100'
		ElementTree.SubElement(feed, 'updated').text = last_mod.strftime("%Y-%m-%dT%H:%M:%SZ")

		# self link...
		self_href = "http://%s/atom" % self.request.environ['HTTP_HOST']
		query_string = "?" + self.request.environ['QUERY_STRING']
		if query_string != "?":
			self_href += query_string

		ElementTree.SubElement(feed, 'link', {
			'href': self_href,
			'rel': 'self',
			'type': 'application/atom+xml'
		})

		# pubsubhubbub link...
		ElementTree.SubElement(feed, 'link', {
			'href': 'http://pubsubhubbub.appspot.com',
			'rel': 'hub'
		})

		for incident in incidents:
			if roads != "":
				road_match = re.match(roads.replace(",", "|"), incident.Location, flags=re.I)
				if road_match is None:
					continue

			details = pickle.loads(incident.LogDetails)
			description = "%s, %s<ul>" % (incident.Location, incident.Area)
			for detail in details['details']:
				description += "<li>" + detail['DetailTime'] + ": " + detail['IncidentDetail'] + "</li>"
			description += "</ul>"

			entry = ElementTree.SubElement (feed, 'entry')

			ElementTree.SubElement(entry, 'title').text = incident.LogType
			ElementTree.SubElement(entry, 'id').text = 'tag:traffic.lectroid.net,2010-06-24:' + incident.LogID
			ElementTree.SubElement(entry, 'content', {'type': 'html'}).text = description
			ElementTree.SubElement(entry, 'published').text = incident.LogTime.strftime("%Y-%m-%dT%H:%M:%SZ")
			ElementTree.SubElement(entry, 'updated').text = incident.updated.strftime("%Y-%m-%dT%H:%M:%SZ")

			ElementTree.SubElement(entry, 'link', {
				'href': "http://%s/incident?id=%s" % (self.request.environ['HTTP_HOST'], incident.key().name())
			})

			if incident.geolocation is not None:
				ElementTree.SubElement(entry, 'georss:point').text = str(incident.geolocation.lat) + " " + str(incident.geolocation.lon)
<commit_msg>Don't show the PSH link on custom feeds.<commit_after>import datetime
import pickle
import re
import time
from xml.etree import ElementTree

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

from models import CHPIncident
from utils import conditional_http


class AtomHandler(webapp.RequestHandler):
	def get(self):
		center = self.request.get("center")
		dispatch = self.request.get("dispatch", default_value="STCC")
		area = self.request.get("area")
		roads = self.request.get("roads")

		incidents = CHPIncident.all()
		incidents.order('-LogTime')
		if center != "":
			incidents.filter('CenterID =', center)
		if dispatch != "":
			incidents.filter('DispatchID =', dispatch)
		if area != "":
			incidents.filter('Area =', area)

		last_mod = datetime.datetime.utcnow()
		if incidents.count(1) > 0:
			last_mod = max(incidents, key=lambda incident: incident.updated).updated
			if conditional_http.isNotModified(self, last_mod):
				return

		feed = ElementTree.Element('feed', {
			'xmlns': 'http://www.w3.org/2005/Atom',
			'xmlns:georss': 'http://www.georss.org/georss'
		})

		ElementTree.SubElement(feed, 'title').text = 'Traffic'
		ElementTree.SubElement(feed, 'subtitle').text = 'Traffic incidents from the CHP'
		ElementTree.SubElement(ElementTree.SubElement(feed, 'author'), 'name').text = 'The California Highway Patrol'
		ElementTree.SubElement(feed, 'id').text = 'tag:traffic.lectroid.net,2010-06-24:100'
		ElementTree.SubElement(feed, 'updated').text = last_mod.strftime("%Y-%m-%dT%H:%M:%SZ")

		# self link...
		self_href = "http://%s/atom" % self.request.environ['HTTP_HOST']
		query_string = "?" + self.request.environ['QUERY_STRING']
		if query_string != "?":
			self_href += query_string

		ElementTree.SubElement(feed, 'link', {
			'href': self_href,
			'rel': 'self',
			'type': 'application/atom+xml'
		})

		# pubsubhubbub link...
		# don't show for roads search feeds, since we don't ping for those
		if roads == "":
			ElementTree.SubElement(feed, 'link', {
				'href': 'http://pubsubhubbub.appspot.com',
				'rel': 'hub'
			})

		for incident in incidents:
			if roads != "":
				road_match = re.match(roads.replace(",", "|"), incident.Location, flags=re.I)
				if road_match is None:
					continue

			details = pickle.loads(incident.LogDetails)
			description = "%s, %s<ul>" % (incident.Location, incident.Area)
			for detail in details['details']:
				description += "<li>" + detail['DetailTime'] + ": " + detail['IncidentDetail'] + "</li>"
			description += "</ul>"

			entry = ElementTree.SubElement (feed, 'entry')

			ElementTree.SubElement(entry, 'title').text = incident.LogType
			ElementTree.SubElement(entry, 'id').text = 'tag:traffic.lectroid.net,2010-06-24:' + incident.LogID
			ElementTree.SubElement(entry, 'content', {'type': 'html'}).text = description
			ElementTree.SubElement(entry, 'published').text = incident.LogTime.strftime("%Y-%m-%dT%H:%M:%SZ")
			ElementTree.SubElement(entry, 'updated').text = incident.updated.strftime("%Y-%m-%dT%H:%M:%SZ")

			ElementTree.SubElement(entry, 'link', {
				'href': "http://%s/incident?id=%s" % (self.request.environ['HTTP_HOST'], incident.key().name())
			})

			if incident.geolocation is not None:
				ElementTree.SubElement(entry, 'georss:point').text = str(incident.geolocation.lat) + " " + str(incident.geolocation.lon)

{'fraction_non_alphanumeric': 0.17263893106589737, 'fraction_numerical': 0.007895535985423625, 'mean_word_length': 4.553962900505902, 'pattern_counts': {'":': 13, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2980760', 'n_tokens_mistral': 2505, 'n_tokens_neox': 2206, 'n_words': 413}