Mango /

from datetime import datetime, timedelta
from urllib import urlencode, urlopen

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

from mango.decorators import baseurl
from mango.main import Index
from mango.settings import SITEMAP
from mango.utils import logger

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

class BlogSitemap(Sitemap):

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

    def changefreq(self, document):
        lastmod = self.lastmod(document)
        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):
        lastmod = datetime.fromtimestamp(document.lastmod)
        if document.comments():
            lastmod = max(lastmod, document.comments()[-1].created_at)
        return lastmod

    def location(self, document):
        return document.urls['canon']['rel']