<s> from rfc3339 import rfc3339 <EOL> from datetime import datetime <EOL> import logging <EOL> _logger = logging . getLogger ( __name__ ) <EOL> from django . core . management . base import BaseCommand <EOL> from django . core . paginator import Paginator <EOL> from django . db import reset_queries <EOL> from chronam . core import models as m <EOL> from chronam . core . rdf import rdf_uri <EOL> class Command ( BaseCommand ) : <EOL> help = "<STR_LIT>" <EOL> def handle ( self , ** options ) : <EOL> write_sitemaps ( ) <EOL> def write_sitemaps ( ) : <EOL> """<STR_LIT>""" <EOL> sitemap_index = open ( '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> sitemap_index . write ( '<STR_LIT>' ) <EOL> max_urls = <NUM_LIT> <EOL> page_count = <NUM_LIT:0> <EOL> url_count = <NUM_LIT:0> <EOL> sitemap_file = None <EOL> for loc , last_mod in sitemap_urls ( ) : <EOL> if url_count % max_urls == <NUM_LIT:0> : <EOL> page_count += <NUM_LIT:1> <EOL> if sitemap_file : <EOL> sitemap . write ( '<STR_LIT>' ) <EOL> sitemap . close ( ) <EOL> sitemap_file = '<STR_LIT>' % page_count <EOL> sitemap_path = '<STR_LIT>' % sitemap_file <EOL> _logger . info ( "<STR_LIT>" % sitemap_path ) <EOL> sitemap = open ( sitemap_path , '<STR_LIT:w>' ) <EOL> sitemap . write ( '<STR_LIT>' ) <EOL> sitemap_index . write ( '<STR_LIT>' % sitemap_file ) <EOL> sitemap . write ( "<STR_LIT>" % ( loc , rfc3339 ( last_mod ) ) ) <EOL> url_count += <NUM_LIT:1> <EOL> if url_count % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> reset_queries ( ) <EOL> try : <EOL> sitemap . write ( '<STR_LIT>' ) <EOL> sitemap . close ( ) <EOL> except NameError : <EOL> _logger . info ( "<STR_LIT>" ) <EOL> pass <EOL> sitemap_index . write ( '<STR_LIT>' ) <EOL> sitemap_index . close ( ) <EOL> def sitemap_urls ( ) : <EOL> """<STR_LIT>""" <EOL> for batch in m . Batch . objects . filter ( sitemap_indexed__isnull = True ) : <EOL> yield batch . url , batch . released <EOL> yield rdf_uri ( batch ) , batch . released <EOL> batch . sitemap_indexed = datetime . now ( ) <EOL> batch . save ( ) <EOL> for issue in batch . issues . all ( ) : <EOL> yield issue . url , batch . released <EOL> yield rdf_uri ( issue ) , batch . released <EOL> for page in issue . pages . all ( ) : <EOL> yield page . url , batch . released <EOL> yield rdf_uri ( page ) , batch . released <EOL> paginator = Paginator ( m . Title . objects . filter ( sitemap_indexed__isnull = True ) , <NUM_LIT> ) <EOL> for page_num in range ( <NUM_LIT:1> , paginator . num_pages + <NUM_LIT:1> ) : <EOL> page = paginator . page ( page_num ) <EOL> for title in page . object_list : <EOL> yield title . url , title . created <EOL> title . sitemap_indexed = datetime . now ( ) <EOL> title . save ( ) </s>
