瀏覽代碼

Fixes and fixes

Gamaliel Espinoza Macedo 6 年之前
父節點
當前提交
3de02bd767
共有 1 個文件被更改,包括 21 次插入3 次删除
  1. 21 3
      ondemand/service.py

+ 21 - 3
ondemand/service.py

@@ -116,7 +116,15 @@ def process_queue_with_threads():
             if not t:
                 try:
                     item = queue.get(False)
-                    thread = Thread(target=process_segment, args=(item,))
+                    station = item['station']
+                    date = dateutil.parser.parse(item['date'])
+
+                    thread = Thread(target=process_segment,
+                        args=(item,),
+                        kwargs={
+                            'audios': [f for f in iterate_audios(date, station)]
+                        }
+                    )
                     threads[index] = thread
                     thread.start()
                 except Empty:
@@ -132,7 +140,7 @@ def process_queue_with_threads():
     log.info('Finished thread processing')
     loop.add_callback(feed_queue)
 
-def process_segment(item):
+def process_segment(item, audios=None):
     """ Procesa una hora de audio """
 
     station = item['station']
@@ -162,10 +170,19 @@ def process_segment(item):
     except Exception as ex:
         log.error('cannot fingerprint: {}'.format(ex))
 
+    """ Hay dos posibles escensarios al obtener los audios
+      a. Los audios vienen por el parámetro "audios" de la
+         función, siendo esta una lista.
+      b. Los audios se obtienen directamente de la base
+         de datos en modo de cursor.
+    """
+    audios_iterable = audios if audios \
+        else iterate_audios(date, station)
+
     # 2. Read the list of files from local database
     audios_counter = 0
     results = []
-    for path, name, ts in iterate_audios(date, station):
+    for path, name, ts in audios_iterable:
         log.info('file: {}'.format(path))
         audios_counter += os.path.isfile(path)
         for match in dejavu.recognize(recognizer, path, 5,
@@ -259,6 +276,7 @@ def iterate_audios(dt, station):
         mp3name = os.path.basename(mp3path)
         yield (mp3path, mp3name, ts)
 
+
 def cloud_download(ad_key=None):
     """ Given an ad key, the file is downloaded to
     the system temporal folder to be processed """