Guest User

Untitled

a guest
Sep 14th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.07 KB | None | 0 0
  1. from bottle import route, run, template, static_file, request
  2. from random import randint, choice
  3. import bottle
  4.  
  5. bottle.TEMPLATE_PATH.insert(0,'/var/www/apps/rotator/')
  6.  
  7. app = application = bottle.Bottle()
  8.  
  9. PROB_ESPECIFICA = 6
  10. #codigo de os paises que van a filtrar, por un tema de performance
  11. PAISES = ['AR', 'MX', 'CL']
  12.  
  13. imagenes = {
  14.     "160x600" : [
  15.         ('1_160x600.jpg','http://URLTOSITE1.com', ['TODOS']),
  16.         ('2_160x600.jpg','http://URLTOSITE2.com', ['TODOS']),
  17.         ('3_160x600.jpg','http://URLTOSITE3.com', ['TODOS']),
  18.         ('4_160x600.jpg','http://URLTOSITE4.com', ['TODOS'])
  19.     ],
  20.     "gif_jh_160x600" : [
  21.         ('gif1_160x600.gif','http://URLTOSITE1.com', ['TODOS']),
  22.         ('gif2_160x600.gif','http://URLTOSITE2.com', ['TODOS']),
  23.         ('gif3_160x600.gif','http://URLTOSITE3.com', ['TODOS']),
  24.         ('gif4_160x600.gif','http://URLTOSITE4.com', ['TODOS'])
  25.     ], 
  26.     "300x250" : [
  27.         ('1_300x250.jpg','http://URLTOSITE1.com', ['AR','TODOS'] ),
  28.         ('2_300x250.jpg','http://URLTOSITE2.com', ['TODOS']),
  29.         ('3_300x250.jpg','http://URLTOSITE3.com', ['AR']),
  30.         ('4_300x250.jpg','http://URLTOSITE4.com', ['AR'])
  31.     ],
  32.     "468x60" : [
  33.         ('1_468x60.jpg','http://URLTOSITE1.com', ['TODOS']),
  34.         ('2_468x60.jpg','http://URLTOSITE2.com', ['TODOS']),
  35.         ('3_468x60.jpg','http://URLTOSITE3.com', ['TODOS']),
  36.         ('4_468x60.jpg','http://URLTOSITE4.com', ['TODOS'])
  37.     ],
  38.     "728x90" : [
  39.         ('1_728x90.jpg','http://URLTOSITE1.com', ['TODOS']),
  40.         ('2_728x90.jpg','http://URLTOSITE2.com', ['TODOS']),
  41.         ('3_728x90.jpg','http://URLTOSITE3.com', ['TODOS']),
  42.         ('4_728x90.jpg','http://URLTOSITE4.com', ['TODOS'])
  43.     ],
  44.     "160x500" : [
  45.         ('1_160x500.jpg','http://URLTOSITE1.com'),
  46.         ('2_160x500.jpg','http://URLTOSITE2.com'),
  47.         ('3_160x500.jpg','http://URLTOSITE3.com'),
  48.         ('4_160x500.jpg','http://URLTOSITE4.com')
  49.     ]
  50. }
  51.  
  52. temp = '''<html><head>
  53. <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
  54. </head>
  55. <body style="margin:0!important">
  56.     <a href="{{link}}" "target="_blank" rel="nofollow">
  57.         <img alt="" src="{{urlImagen}}"/>
  58.     </a>
  59. </body></html>'''
  60.  
  61. tempr = '''<html><head>
  62. <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
  63. </head>
  64. <body style="margin:0!important">
  65.     <a href="{{link}}" "target="_blank" rel="nofollow">
  66.         <img alt="" src="{{urlImagen}}"/>
  67.     </a>
  68.     <script>
  69.     setTimeout(function(){document.location.reload(true);},3000)
  70.     </script>
  71. </body></html>'''
  72.    
  73. def imagenRandom(tuplasDeImagenes):
  74.     return choice(tuplasDeImagenes)
  75.  
  76.  
  77. @app.route('/api/<tamanio>')
  78. def index(tamanio):
  79.     return getImage(tamanio, temp)
  80.  
  81.  
  82. @app.route('/apir/<tamanio>')
  83. def indec(tamanio):
  84.     return getImage(tamanio, tempr)
  85.  
  86.  
  87. def getImage(tamanio, plantilla):
  88.     print('>>>>> ',request.headers.keys())
  89.     pais = request.get_header('GEOIP_CITY_COUNTRY_CODE')
  90.     print('=========== ' , pais, ' ==================')
  91.     posiblesImagenes = filtrarPorTamanioYPais(tamanio, pais)
  92.     img,link = imagenRandom(posiblesImagenes)
  93.     return template(plantilla, urlImagen="/static/" + img, link=link)
  94.    
  95.  
  96. def filtrar(lista, cod='TODOS'):
  97.     return [(imagen,url) for imagen,url,paises in lista if cod in paises]
  98.  
  99.  
  100. def filtrarEspecifico(lista, pais):
  101.     filtrado = filtrar(lista, pais)
  102.  
  103.     if not filtrado:
  104.         return filtrar(lista)
  105.  
  106.     return filtrado
  107.  
  108.  
  109. def filtrarPorTamanioYPais(tamanio, pais):
  110.     porTamanio = imagenes[tamanio]
  111.     r = randint(0,6)
  112.  
  113.     if pais in PAISES and r in range(0,PROB_ESPECIFICA):
  114.         return filtrarEspecifico(porTamanio, pais)
  115.     else:
  116.         return filtrar(porTamanio)
  117.    
  118.                
  119. @app.route('/static/<filename>')
  120. def server_static(filename):
  121.     return static_file(filename, root='./imagenes/')
  122.  
  123.  
  124. class StripPathMiddleware(object):
  125.     '''
  126.    Get that slash out of the request
  127.    '''
  128.     def __init__(self, a):
  129.         self.a = a
  130.     def __call__(self, e, h):
  131.         e['PATH_INFO'] = e['PATH_INFO'].rstrip('/')
  132.         return self.a(e, h)
  133.        
  134. if __name__ == '__main__':
  135.     bottle.run(app=StripPathMiddleware(app),
  136.         #server='python_server',
  137.         host='0.0.0.0',
  138.         port=8080)
Advertisement
Add Comment
Please, Sign In to add comment