Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bottle import route, run, template, static_file, request
- from random import randint, choice
- import bottle
- bottle.TEMPLATE_PATH.insert(0,'/var/www/apps/rotator/')
- app = application = bottle.Bottle()
- PROB_ESPECIFICA = 6
- #codigo de os paises que van a filtrar, por un tema de performance
- PAISES = ['AR', 'MX', 'CL']
- imagenes = {
- "160x600" : [
- ('1_160x600.jpg','http://URLTOSITE1.com', ['TODOS']),
- ('2_160x600.jpg','http://URLTOSITE2.com', ['TODOS']),
- ('3_160x600.jpg','http://URLTOSITE3.com', ['TODOS']),
- ('4_160x600.jpg','http://URLTOSITE4.com', ['TODOS'])
- ],
- "gif_jh_160x600" : [
- ('gif1_160x600.gif','http://URLTOSITE1.com', ['TODOS']),
- ('gif2_160x600.gif','http://URLTOSITE2.com', ['TODOS']),
- ('gif3_160x600.gif','http://URLTOSITE3.com', ['TODOS']),
- ('gif4_160x600.gif','http://URLTOSITE4.com', ['TODOS'])
- ],
- "300x250" : [
- ('1_300x250.jpg','http://URLTOSITE1.com', ['AR','TODOS'] ),
- ('2_300x250.jpg','http://URLTOSITE2.com', ['TODOS']),
- ('3_300x250.jpg','http://URLTOSITE3.com', ['AR']),
- ('4_300x250.jpg','http://URLTOSITE4.com', ['AR'])
- ],
- "468x60" : [
- ('1_468x60.jpg','http://URLTOSITE1.com', ['TODOS']),
- ('2_468x60.jpg','http://URLTOSITE2.com', ['TODOS']),
- ('3_468x60.jpg','http://URLTOSITE3.com', ['TODOS']),
- ('4_468x60.jpg','http://URLTOSITE4.com', ['TODOS'])
- ],
- "728x90" : [
- ('1_728x90.jpg','http://URLTOSITE1.com', ['TODOS']),
- ('2_728x90.jpg','http://URLTOSITE2.com', ['TODOS']),
- ('3_728x90.jpg','http://URLTOSITE3.com', ['TODOS']),
- ('4_728x90.jpg','http://URLTOSITE4.com', ['TODOS'])
- ],
- "160x500" : [
- ('1_160x500.jpg','http://URLTOSITE1.com'),
- ('2_160x500.jpg','http://URLTOSITE2.com'),
- ('3_160x500.jpg','http://URLTOSITE3.com'),
- ('4_160x500.jpg','http://URLTOSITE4.com')
- ]
- }
- temp = '''<html><head>
- <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
- </head>
- <body style="margin:0!important">
- <a href="{{link}}" "target="_blank" rel="nofollow">
- <img alt="" src="{{urlImagen}}"/>
- </a>
- </body></html>'''
- tempr = '''<html><head>
- <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
- </head>
- <body style="margin:0!important">
- <a href="{{link}}" "target="_blank" rel="nofollow">
- <img alt="" src="{{urlImagen}}"/>
- </a>
- <script>
- setTimeout(function(){document.location.reload(true);},3000)
- </script>
- </body></html>'''
- def imagenRandom(tuplasDeImagenes):
- return choice(tuplasDeImagenes)
- @app.route('/api/<tamanio>')
- def index(tamanio):
- return getImage(tamanio, temp)
- @app.route('/apir/<tamanio>')
- def indec(tamanio):
- return getImage(tamanio, tempr)
- def getImage(tamanio, plantilla):
- print('>>>>> ',request.headers.keys())
- pais = request.get_header('GEOIP_CITY_COUNTRY_CODE')
- print('=========== ' , pais, ' ==================')
- posiblesImagenes = filtrarPorTamanioYPais(tamanio, pais)
- img,link = imagenRandom(posiblesImagenes)
- return template(plantilla, urlImagen="/static/" + img, link=link)
- def filtrar(lista, cod='TODOS'):
- return [(imagen,url) for imagen,url,paises in lista if cod in paises]
- def filtrarEspecifico(lista, pais):
- filtrado = filtrar(lista, pais)
- if not filtrado:
- return filtrar(lista)
- return filtrado
- def filtrarPorTamanioYPais(tamanio, pais):
- porTamanio = imagenes[tamanio]
- r = randint(0,6)
- if pais in PAISES and r in range(0,PROB_ESPECIFICA):
- return filtrarEspecifico(porTamanio, pais)
- else:
- return filtrar(porTamanio)
- @app.route('/static/<filename>')
- def server_static(filename):
- return static_file(filename, root='./imagenes/')
- class StripPathMiddleware(object):
- '''
- Get that slash out of the request
- '''
- def __init__(self, a):
- self.a = a
- def __call__(self, e, h):
- e['PATH_INFO'] = e['PATH_INFO'].rstrip('/')
- return self.a(e, h)
- if __name__ == '__main__':
- bottle.run(app=StripPathMiddleware(app),
- #server='python_server',
- host='0.0.0.0',
- port=8080)
Advertisement
Add Comment
Please, Sign In to add comment