Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate static HTML files for a list of urls and the .htaccess rewrite rules for them.
#!/usr/bin/env python
import os
import urllib2
baseUrl='https://www.thompsontee.com/'
cacheDir='static_cache'
htaccessFile='htaccess'
urls={
'about-us.html': 'about-us',
'blog_category_press.html': 'blog/category/press',
'blog_category_thompson-tee-blog.html': 'blog/category/thompson-tee-blog',
'frequently-asked-questions.html': 'frequently-asked-questions',
'index.html': '',
'made-in-the-usa.html': 'made-in-the-usa',
'men.html': 'men.html',
'privacy-policy-cookie-restriction-mode.html': 'privacy-policy-cookie-restriction-mode',
'returns_asp.html': 'returns.asp',
'shipping-deliveries.html': 'shipping-deliveries',
'sweat-101.html': 'sweat-101',
'technology.html': 'technology',
'testimonials.html': 'testimonials',
'women.html': 'women.html'
}
def main():
print 'Make sure you\'ve disabled static caching on production so fresh html is generated.'
setup();
cache();
htaccess();
print 'Done. Remeber to place the files on the server and update the htaccess rules.'
def setup():
print 'Setting up...'
if not os.path.isdir(cacheDir):
print 'Cache directory does not exist, creating...'
os.makedirs(cacheDir)
for file in os.listdir(cacheDir):
path = os.path.join(cacheDir, file)
try:
if os.path.isfile(path):
os.unlink(path)
except Exception as e:
print 'Failed to clear the cache directory'
print e
exit(1)
os.unlink(htaccessFile)
def cache():
print 'Downloading files...'
for file, url in urls.iteritems():
response = urllib2.urlopen('%s%s' % (baseUrl, url))
html = response.read()
with open('%s/%s' % (cacheDir, file), 'w') as handle:
handle.write(html.replace('</body>', '<!-- static cache --></body>'))
def htaccess():
print 'Building .htaccess rules...'
lines = []
for file, url in urls.iteritems():
lines.append('RewriteRule ^/?%s/?$ /static_cache/%s [L,NC]' % (url, file))
with open(htaccessFile, 'w') as handle:
handle.write('\n'.join(lines))
print 'Rules written to %s' % htaccessFile
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment