Skip to content

Instantly share code, notes, and snippets.

@joefutrelle
Created November 4, 2014 16:33
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 joefutrelle/b6cdd5dc5fa2ccf9da06 to your computer and use it in GitHub Desktop.
Save joefutrelle/b6cdd5dc5fa2ccf9da06 to your computer and use it in GitHub Desktop.
Mock IFCB
import os
import re
import time
from urllib2 import urlopen
import json
FEED_URL='http://ifcb-data.whoi.edu/mvco/feed.json'
DATA_DIR='/data'
FORMATS=('hdr','adc','roi')
def copy_new():
feed = json.loads(urlopen(FEED_URL).read())
pid = feed[0]['pid']
lid = re.sub(r'.*/','',pid)
urls = dict((fmt,'%s.%s' % (pid,fmt)) for fmt in FORMATS)
paths = dict((fmt,os.path.join(DATA_DIR,'%s.%s' % (lid,fmt))) for fmt in FORMATS)
for fmt in FORMATS:
url, path = urls[fmt], paths[fmt]
if not os.path.exists(path):
with open(path,'w') as local:
local.write(urlopen(url).read())
def delete_old():
now = time.time()
interval = 3600 * 12 # 12 hours
elapsed = now - interval
for f in os.listdir(DATA_DIR):
p = os.path.join(DATA_DIR,f)
if os.stat(p).st_mtime < elapsed:
os.remove(p)
try:
copy_new()
except:
pass
try:
delete_old()
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment