Created
October 6, 2013 20:09
-
-
Save iainelder/6858513 to your computer and use it in GitHub Desktop.
Write requests cache to files.
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
import requests | |
import requests_cache | |
from urlparse import urlparse | |
from os.path import basename | |
def dump_cache(): | |
""" | |
Writes all the responses in the cache to files. | |
The filename is the basename of the URL. | |
This hasn't been tested for POST requests; probably broken | |
TODO: fix for POST requests | |
TODO: set modified time on file | |
""" | |
cache = requests_cache.core.get_cache() | |
entries = [{'timestamp': time, | |
'filename': basename(urlparse(resp.url).path), | |
'content': resp.content | |
} | |
for resp, time in [ | |
(cache.restore_response(entry), time) | |
for entry, time in cache.responses.values() | |
] | |
] | |
for entry in entries: | |
with open(entry['filename'], 'w') as f: | |
f.write(entry['content']) | |
# script runner does this before script runs | |
requests_cache.install_cache('carhire_cache') | |
requests_cache.clear() | |
# script does its thing (discarding raw responses) | |
url = 'https://ota.cartrawler.com/cartrawlerota/files/static/ctlocation.EN.xml' | |
requests.get(url) | |
# dump the cache | |
dump_cache() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment