Skip to content

Instantly share code, notes, and snippets.

@sirpengi
Created August 2, 2017 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirpengi/d6d74c34e008316028e6c7bf4319b809 to your computer and use it in GitHub Desktop.
Save sirpengi/d6d74c34e008316028e6c7bf4319b809 to your computer and use it in GitHub Desktop.
cached requests
from datetime import datetime, timedelta
from os import stat, unlink
import requests
import vcr
CFILE = "blah.cassette"
TTL = timedelta(minutes=15)
try:
cstat = stat(CFILE)
except OSError:
pass
else:
if cstat and datetime.fromtimestamp(cstat.st_mtime) < (datetime.now() - TTL):
unlink(CFILE)
with vcr.use_cassette(CFILE):
r = requests.get("http://httpbin.org/get")
print(r.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment