Source

Mango / sitemaps.py

from datetime import datetime, timedelta
from urllib import urlopen

from django.conf import settings
from django.contrib.sitemaps import PING_URL, Sitemap
from django.core.urlresolvers import NoReverseMatch, reverse
from django.utils.http import urlencode

import mango.settings
from mango.main import Index
from mango.utils import logger

def ping_google(sitemap_url=None, ping_url=PING_URL):
    if mango.settings.SITEMAP and not settings.DEBUG:
        if sitemap_url is None:
            try:
                sitemap_url = reverse('django.contrib.sitemaps.views.sitemap')
            except NoReverseMatch, error:
                logger.error(error)
        try:
            urlopen('%s?%s' % (ping_url,
                    urlencode({'sitemap': '%s%s' % (
                    mango.settings.BASE_URL, sitemap_url[1:])})))
        except Exception, error:
            logger.error(error)


class BlogSitemap(Sitemap):

    def items(self):
        return Index.get().descendants(include_pages=True)

    def changefreq(self, document):
        lastmod = self.lastmod(document)
        now = datetime.now()
        frequencies = (
            ('hourly', now - timedelta(hours=2)),
            ('daily', now - timedelta(2)),
            ('weekly', now - timedelta(14)),
            ('monthly', now - timedelta(60)),
        )
        # what's the frequency, Kenneth?
        for frequency, some_time_ago in frequencies:
            if lastmod > some_time_ago:
                return frequency

        return 'yearly'

    def lastmod(self, document):
        return datetime.utcfromtimestamp(document.lastmod)

    def location(self, document):
        return document.identifier()