class URLHandler: sleepTime = 5 rateLimitError = 400 rateLimit = None def __init__(self): self.rateLimit = RateLimit() def open_url(self,url): count = 1 while (count): if (count == 10): print ("In URLHandler.open_url: URL Exception Occur", url) return None try: self.rateLimit.check() res = urllib2.urlopen(url) print (url, count) return res except urllib2.HTTPError, e: print ("In URLHandler.open_url: HTTPError", e.code, e.strerror, e.message) if e.code == 401: #unauthorized return None elif e.code == 404: #user does not exist return None if self.rateLimitError == e.code: self.rateLimit.check() count = count + 1 time.sleep(self.sleepTime) except urllib2.URLError, e: print ("In URLHandler.open_url: URLError", e.reason) count = count + 1 time.sleep(self.sleepTime) except httplib.BadStatusLine, e: print ("In URLHandler.open_url: BadStatusLine",e) count = count + 1 time.sleep(self.sleepTime)
def __init__(self): self.rateLimit = RateLimit()
import time from RateLimit import RateLimit from flask import Flask from flask_cors import CORS, cross_origin # web app app = Flask(__name__, ) cors = CORS(app, resources={r"/foo": {"origins": "*"}}) app.config['CORS_HEADERS'] = 'Content-Type' # database engine engine = sqlalchemy.create_engine(os.getenv('SQL_URI')) events_hr = RateLimit(100, 60 * 60) events_da = RateLimit(100, 60 * 24 * 60) stats_hr = RateLimit(100, 60 * 60) stats_da = RateLimit(100, 60 * 24 * 60) events_hr_poi = RateLimit(100, 60 * 60) events_da_poi = RateLimit(100, 60 * 24 * 60) stats_hr_poi = RateLimit(100, 60 * 60) stats_da_poi = RateLimit(100, 60 * 24 * 60) @app.route('/') @cross_origin(origin='*', headers=['Content-Type', 'Authorization']) def index(): return 'Welcome to EQ Works 😎'