예제 #1
0
파일: sitemap.py 프로젝트: supor/vlog
def handle_sitemap(logic, request):
    def make_url(path, lastmod, changefre='monthly', priority='0.3'):
        info = {}
        info['path'] = path
        info['lastmod'] = lastmod.strftime("%Y-%m-%dT%H:%M:%S+00:00")
        info['changefreq'] = changefre
        info['priority'] = priority
        return info

    urls = []
    urls.append(make_url('/', now(), 'weekly', '0.8'))
    pages = logic.page.get_all_pages()
    for p in pages:
        path = "/page/{0}/".format(quote(p.get('link_title').encode("utf-8")))
        urls.append(make_url(path, p.get('pubdate')))

    posts = logic.post.get_all_posts()
    for p in posts:
        date = p.get("pubdate")
        path = "/{0}/{1}/{2}/{3}/".format(date.year, date.month, date.day,
                                         quote(p.get('link_title').encode("utf-8")))
        urls.append(make_url(path, p.get('pubdate')))

    cates = logic.category.get_categories().get("data")
    for c in cates:
        path = "/category/{0}/".format(quote(c.get('name').encode("utf-8")))
        urls.append(make_url(path, now(), 'weekly', '0.5'))

    tags = logic.tag.get_tags().get("data")
    for t in tags:
        path = "/tag/{0}/".format(quote(t.get("name").encode("utf-8")))
        urls.append(make_url(path, now(), 'weekly', '0.1'))

    months = logic.post.get_months()
    for m in months:
        path = "/date/{0}/{1}/".format(m.get('year'), m.get('month'))
        urls.append(make_url(path, now(), 'monthly', '0.2'))

    template_path = 'sitemap.jinja'
    env = BaseHandler._path_to_evn.get(TEMPLATE_PATH)
    if not env:
        __loader = FileSystemLoader(TEMPLATE_PATH)
        env = Environment(loader = __loader)
        BaseHandler._path_to_evn[TEMPLATE_PATH] = env
    template = env.get_template(template_path)
    current = now().strftime("%Y-%m-%dT%H:%M:%S+00:00")
    content = template.render(urls = urls, request = request, now = current)
    static_path = os.path.join(STATIC_PATH, 'sitemap.xml')
    static_file = open(static_path, 'w')
    static_file.write(content)
    static_file.close()
예제 #2
0
파일: rss.py 프로젝트: supor/vlog
def handle_rss(logic, request):
    posts = logic.post.get_posts().get("data")
    for p in posts:
        p["link_title"] = quote(p.get("link_title").encode("utf-8"))
        p['rss_date'] = p.get('pubdate').strftime(timeformat)

    template_path = 'rss.jinja'
    env = BaseHandler._path_to_evn.get(TEMPLATE_PATH)
    if not env:
        __loader = FileSystemLoader(TEMPLATE_PATH)
        env = Environment(loader = __loader)
        BaseHandler._path_to_evn[TEMPLATE_PATH] = env
    template = env.get_template(template_path)
    content = template.render(posts = posts, request = request,
                              buildDate = now().strftime(timeformat),
                              SITE_TITLE = logic.option.site_title,
                              description = logic.option.description)
    static_path = os.path.join(STATIC_PATH, 'rss.xml')
    static_file = open(static_path, 'w')
    static_file.write(content.encode('utf-8'))
    static_file.close()