Last active
March 24, 2019 10:06
-
-
Save iyaozhen/53e6a57a2f7e945ba1161953959a7cb2 to your computer and use it in GitHub Desktop.
according to sitemap.xml rebuild cache @Cache Enabler – WordPress Caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# https://cn.wordpress.org/plugins/cache-enabler/ | |
# rebuild cache according to sitemap.xml | |
import os | |
import requests | |
import xml.etree.ElementTree as ET | |
import time | |
import shutil | |
CACHE_DIR = "/var/www/wordpress/wp-content/cache/cache-enabler/" | |
SITEMAP_URL = "https://iyaozhen.com/sitemap.xml" | |
if os.path.isdir(CACHE_DIR): | |
shutil.rmtree(CACHE_DIR) | |
try: | |
sitemap = requests.get(SITEMAP_URL) | |
except (requests.HTTPError, requests.ConnectionError) as e: | |
print("get sitemap.xml error: %s" % e) | |
else: | |
urlset = ET.fromstring(sitemap.content) | |
print("start rebuild cache at %s" % time.asctime()) | |
for url in urlset: | |
loc = url[0].text | |
try: | |
requests.get(loc) | |
except (requests.HTTPError, requests.ConnectionError) as e: | |
print("request %s error: %s" % (loc, e)) | |
else: | |
print("hit %s" % loc) | |
finally: | |
time.sleep(0.1) | |
else: | |
print("no cache dir: %s" % CACHE_DIR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what this script does?