<s> """<STR_LIT>""" <EOL> import os <EOL> import traceback <EOL> from settings import ( APP_URI ) <EOL> from flask import send_from_directory , Response , request , json <EOL> from settings import REDIS_INSTANCE , VCAP_CONFIG <EOL> from functools import wraps <EOL> from helpers import oui <EOL> from gevent import Timeout <EOL> from datetime import datetime , timedelta <EOL> PWD = os . environ . get ( "<STR_LIT>" ) <EOL> static_folder = os . path . join ( PWD , "<STR_LIT>" ) <EOL> vcap = os . environ . get ( "<STR_LIT>" ) <EOL> def check_auth ( username , password ) : <EOL> """<STR_LIT>""" <EOL> return username == '<STR_LIT>' and password == '<STR_LIT>' <EOL> def authenticate ( ) : <EOL> """<STR_LIT>""" <EOL> return Response ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <NUM_LIT> , <EOL> { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def requires_auth ( f ) : <EOL> @ wraps ( f ) <EOL> def decorated ( * args , ** kwargs ) : <EOL> auth = request . authorization <EOL> if not auth or not check_auth ( auth . username , auth . password ) : <EOL> return authenticate ( ) <EOL> return f ( * args , ** kwargs ) <EOL> return decorated <EOL> def request_wants_json ( ) : <EOL> best = request . accept_mimetypes . best_match ( [ '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> return best == '<STR_LIT:application/json>' and request . accept_mimetypes [ best ] > request . accept_mimetypes [ '<STR_LIT>' ] <EOL> def run_once_per_day ( f ) : <EOL> def wrapper ( * args , ** kwargs ) : <EOL> try : <EOL> if VCAP_CONFIG : <EOL> nextDate = REDIS_INSTANCE . get ( "<STR_LIT>" ) <EOL> if nextDate is None : <EOL> nextDate = datetime . utcnow ( ) <EOL> else : <EOL> nextDate = datetime . strptime ( nextDate , <EOL> '<STR_LIT>' ) <EOL> else : <EOL> if not wrapper . has_run : <EOL> nextDate = datetime . utcnow ( ) <EOL> else : <EOL> nextDate = datetime . utcnow ( ) + timedelta ( seconds = <NUM_LIT> ) <EOL> now = datetime . utcnow ( ) <EOL> print "<STR_LIT>" <EOL> if now >= nextDate : <EOL> print "<STR_LIT>" <EOL> wrapper . has_run = True <EOL> if VCAP_CONFIG : <EOL> REDIS_INSTANCE . set ( "<STR_LIT>" , datetime . utcnow ( ) <EOL> + timedelta ( seconds = <NUM_LIT> ) ) <EOL> return f ( * args , ** kwargs ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> data = { <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } <EOL> message = json . dumps ( data ) <EOL> print message <EOL> resp = Response ( message , <EOL> status = <NUM_LIT:200> , <EOL> mimetype = '<STR_LIT:application/json>' ) <EOL> return resp <EOL> except Exception , e : <EOL> print e <EOL> print traceback . format_exc ( ) <EOL> wrapper . has_run = False <EOL> return wrapper <EOL> def run_once ( f ) : <EOL> def wrapper ( * args , ** kwargs ) : <EOL> if not wrapper . has_run : <EOL> wrapper . has_run = True <EOL> return f ( * args , ** kwargs ) <EOL> wrapper . has_run = False <EOL> return wrapper <EOL> def index ( path = '<STR_LIT>' ) : <EOL> if request_wants_json ( ) : <EOL> data = { <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> } <EOL> message = json . dumps ( data ) <EOL> resp = Response ( message , status = <NUM_LIT:200> , mimetype = '<STR_LIT:application/json>' ) <EOL> resp . headers [ '<STR_LIT>' ] = APP_URI <EOL> return resp <EOL> else : <EOL> return send_from_directory ( static_folder , "<STR_LIT>" ) <EOL> @ requires_auth <EOL> @ run_once_per_day <EOL> def populate ( ) : <EOL> print "<STR_LIT>" <EOL> try : <EOL> with Timeout ( <NUM_LIT> ) : <EOL> oui . update ( ) <EOL> except Exception , e : <EOL> print e <EOL> print traceback . format_exc ( ) <EOL> finally : <EOL> data = { <EOL> '<STR_LIT:message>' : '<STR_LIT>' <EOL> } <EOL> message = json . dumps ( data ) <EOL> print message <EOL> resp = Response ( message , status = <NUM_LIT:200> , mimetype = '<STR_LIT:application/json>' ) <EOL> return resp <EOL> def favicon ( ) : <EOL> return send_from_directory ( os . path . join ( static_folder , "<STR_LIT>" ) , <EOL> "<STR_LIT>" ) </s>
