ondemand.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. from __future__ import print_function, absolute_import
  2. import warnings
  3. warnings.simplefilter("ignore", UserWarning)
  4. from tornado.ioloop import IOLoop
  5. from boxconfig import parse_config
  6. from dejavu.recognize import FilePerSecondRecognizer
  7. from dejavu import Dejavu
  8. from endpoint import setup_endpoint
  9. import logging as log
  10. import requests
  11. import json
  12. import time
  13. import os
  14. from queue import Queue, Empty
  15. log.basicConfig(format='[%(asctime)s] [%(module)s] %(message)s', level=log.INFO)
  16. PATH = '/tmp'
  17. config = parse_config()
  18. queue = Queue()
  19. recognizer = FilePerSecondRecognizer
  20. def obt_siguiente_trabajo():
  21. url = 'https://api.fourier.audio/na/calendario/pendiente?id=%s' % (config['device_id'],)
  22. response = requests.get(url)
  23. return response.json()
  24. def descargar_anuncio(ad_path):
  25. anuncio = os.path.basename(ad_path)
  26. path = os.path.join(PATH, 'ads')
  27. os.makedirs(path, exist_ok=True)
  28. ruta_anuncio = os.path.join(path, anuncio)
  29. if os.path.isfile(ruta_anuncio):
  30. return ruta_anuncio
  31. cloud_base_url = 'https://storage.googleapis.com/{}' \
  32. .format(config['bucket'])
  33. url = '{}/{}'.format(cloud_base_url, ad_path)
  34. response = requests.get(url)
  35. # TODO: Agregar alerta cuando la respuesta no sea 200
  36. if response.status_code == 200:
  37. with open(ruta_anuncio, "wb") as fp:
  38. fp.write(response.content)
  39. return ruta_anuncio
  40. else:
  41. log.info("[Anuncio][error] %s" % (response.text))
  42. return None
  43. def descargar_media(box, station, media):
  44. ref = '{}/{}/{}'.format(box, station, media)
  45. file = os.path.basename(ref)
  46. path = os.path.join(PATH, 'fourier', box, station)
  47. os.makedirs(path, exist_ok=True)
  48. out_file = os.path.join(path, file)
  49. if os.path.isfile(out_file):
  50. return out_file
  51. filename = ref.replace("/","%2F") \
  52. .replace("+","%2B")
  53. cloud_base_url = '%s%s' % (
  54. 'https://firebasestorage.googleapis.com',
  55. '/v0/b/fourier-6e14d.appspot.com/o'
  56. )
  57. url = '{}/{}?alt=media'.format(cloud_base_url, filename)
  58. response = requests.get(url)
  59. if response.status_code == 200:
  60. with open(out_file, "wb") as fp:
  61. fp.write(response.content)
  62. return out_file
  63. else:
  64. log.info("[Media][url] %s" % (response.text))
  65. log.info("[Media][error] %s" % (response.text))
  66. return None
  67. def enviar_resultados(trabajo):
  68. log.info('[Pendiente] %s' % (json.dumps(trabajo),))
  69. url = 'https://api.fourier.audio/na/calendario/resultado'
  70. response = requests.post(url, json=trabajo)
  71. log.info('[Response] %s' % (response.text))
  72. return response
  73. def llenar_pila():
  74. """ Search for pending scheduled work in
  75. server and add them to a memory queue. """
  76. try:
  77. response = obt_siguiente_trabajo()
  78. if len(response["elementos"]) > 0:
  79. queue.put(response)
  80. if queue.qsize() > 0:
  81. loop.add_callback(procesar_siguiente_pila)
  82. else:
  83. loop.add_timeout(time.time() + 30, llenar_pila)
  84. except Exception as ex:
  85. """ Errores desconocidos """
  86. log.error('[feed_queue] {}'.format(ex))
  87. loop.add_timeout(time.time() + 60, llenar_pila)
  88. raise ex
  89. def procesar_siguiente_pila():
  90. """ Try to the next item in a queue and start
  91. processing it accordingly. If success, repeat
  92. the function or go to feed if no more items. """
  93. try:
  94. item = queue.get(False)
  95. procesar_trabajo(item)
  96. loop.add_callback(procesar_siguiente_pila)
  97. except Empty:
  98. loop.add_callback(llenar_pila)
  99. except Exception as ex:
  100. log.error(ex)
  101. loop.add_callback(procesar_siguiente_pila)
  102. def procesar_trabajo(pendiente):
  103. ciudad = pendiente['origen']
  104. estacion = pendiente['estacion']
  105. segmento = 10
  106. if "segmento" in pendiente:
  107. segmento = int(pendiente["segmento"])
  108. # Descarga de anuncios
  109. log.info("Descargando anuncios")
  110. try:
  111. anuncios = []
  112. id_by_ad = {}
  113. item_ids = []
  114. for i in pendiente["elementos"]:
  115. id_by_ad[i['anuncio']] = i['id']
  116. if i['id'] not in item_ids:
  117. item_ids.append(i['id'])
  118. anuncio = descargar_anuncio(i["ruta"])
  119. if anuncio is not None:
  120. log.info("Listo %s" % (i['ruta'],))
  121. anuncios.append(anuncio)
  122. except Exception as err:
  123. log.info('[process_segment] [{}] {}'.format(estacion, err))
  124. # Descarga de media
  125. log.info("Descargando media")
  126. try:
  127. media = []
  128. for i in pendiente["media"]:
  129. archivo = descargar_media(ciudad, estacion, i["ruta"])
  130. if archivo is not None:
  131. log.info("Listo %s %s %s" % (ciudad, estacion, i['ruta'],))
  132. media.append((archivo, i["fecha"], i["timestamp"]))
  133. except Exception as err:
  134. log.info(err)
  135. if len(media) == 0 or len(anuncio) == 0:
  136. log.info("No hay media o anuncios para comparar")
  137. return
  138. dejavu = None
  139. resultados = {}
  140. try:
  141. dejavu = Dejavu({"database_type": "mem"})
  142. try:
  143. x = 0
  144. for ruta, fecha, ts in media:
  145. log.info("Huellando %s" % (ruta,))
  146. dejavu.fingerprint_file(ruta, ts)
  147. except Exception as ex:
  148. log.info(ex)
  149. for anuncio in anuncios:
  150. log.info("Buscando anuncio %s" % (anuncio,))
  151. for i in dejavu.recognize(recognizer, anuncio, segmento):
  152. if not "id" in i:
  153. continue
  154. if i["confidence"] < 35:
  155. continue
  156. obj = i
  157. obj["match_time"] = None
  158. nombre_anuncio = os.path.split(anuncio)[-1]
  159. id = id_by_ad[nombre_anuncio]
  160. dict = {
  161. "id": id,
  162. "anuncio": anuncio,
  163. "fecha": obj["name"],
  164. "timestamp": obj["name"] + int(obj['offset_seconds']),
  165. "confianza": obj["confidence"],
  166. "longitud": obj["length"],
  167. "desfase_segundos": obj["offset_seconds"]
  168. }
  169. if id in resultados.keys():
  170. resultados[id]["longitud"] += dict["longitud"]
  171. resultados[id]["confianza"] += dict["confianza"]
  172. continue
  173. resultados[id] = dict
  174. for id in resultados:
  175. e = resultados[id]
  176. for i in pendiente['elementos']:
  177. anuncio = e['anuncio'].replace('/tmp/ads/', '')
  178. if i['id'] == e['id'] and i['anuncio'] == anuncio:
  179. if 'encontrados' not in i:
  180. i['encontrados'] = []
  181. obj = {
  182. "fecha": e["fecha"],
  183. "anuncio": anuncio,
  184. "longitud": int(e["longitud"] / 1000),
  185. "confianza": e["confianza"],
  186. "timestamp": e["timestamp"],
  187. "desfase_segundos": e["desfase_segundos"]
  188. }
  189. i['encontrados'].append(obj)
  190. break
  191. log.info("[Resultado] %s" % (json.dumps(resultados)))
  192. pendiente["media"] = None
  193. enviar_resultados(pendiente)
  194. except Exception as ex:
  195. log.info(ex)
  196. app = setup_endpoint(queue=queue)
  197. loop = IOLoop.current()
  198. loop.add_callback(llenar_pila)
  199. if __name__ == '__main__':
  200. try:
  201. log.info('Starting ondemand service')
  202. loop.start()
  203. except KeyboardInterrupt:
  204. log.error('Process killed')