Gamaliel Espinoza 7 jaren geleden
bovenliggende
commit
0a94c2dc96
3 gewijzigde bestanden met toevoegingen van 40 en 26 verwijderingen
  1. 1 0
      .gitignore
  2. 1 1
      ondemand/__init__.py
  3. 38 25
      ondemand/service.py

+ 1 - 0
.gitignore

@@ -57,3 +57,4 @@ typings/
 # dotenv environment variables file
 .env
 
+*.pyc

+ 1 - 1
ondemand/__init__.py

@@ -1 +1 @@
-__version__ = '1.0.4b1'
+__version__ = '1.1.0'

+ 38 - 25
ondemand/service.py

@@ -7,7 +7,6 @@ from fourier.boxconfig import parse_config
 from fourier.dejavu.recognize import FilePerSecondRecognizer
 from datetime import datetime, timedelta
 from fourier.dejavu import Dejavu
-from firebase import firebase
 from Queue import Queue, Empty
 from firebase_admin import credentials
 from firebase_admin import db as fbdb
@@ -24,9 +23,7 @@ AUDIOS_PATH = '/tmp'
 config = parse_config()
 queue = Queue()
 client = Client(config['device_id'],
-                #config['apiSecret'],
-                '7002a5a7592f624aea1364d64a11e39f',
-                base_url='http://localhost:41222')
+                config['apiSecret'])
 cloud_base_url = 'https://storage.googleapis.com/{}'\
                 .format(config['bucket'])
 base_path = config.get("basepath", "/var/fourier")
@@ -41,6 +38,8 @@ db_path = os.path.join(device_path, 'files.db')
 db = sqlite3.connect(db_path)
 
 def feed_queue():
+    """ Search for pending scheduled work in
+    server and add them to a memory queue. """
     try:
         response = client.get_schedule_pending()
         for item in response['items']:
@@ -62,12 +61,18 @@ def feed_queue():
         loop.add_timeout(time.time() + 60, feed_queue)
 
 def process_queue():
+    """ Try to the next item in a queue and start
+    processing it accordingly. If success, repeat
+    the function or go to feed if no more items. """
     try:
         item = queue.get(False)
         process_segment(item)
         loop.add_callback(process_queue)
     except Empty:
         loop.add_callback(feed_queue)
+    except Exception as ex:
+        print(str(ex), file=sys.stderr)
+        loop.add_callback(process_queue)
 
 def process_segment(item):
     """ Procesa una hora de audio """
@@ -82,34 +87,41 @@ def process_segment(item):
         print('ad file missing', file=sys.stderr)
         return
     
-    dejavu.fingerprint_file(filename)
+    try:
+        dejavu.fingerprint_file(filename)
+    except Exception as ex:
+        print('cannot fingerprint: {}'.format(ex))
 
-    # 2. leer la lista de archivos de la base
-    #    de datos local de la caja.
+    # 2. Read the list of files from local database
     for path, name, ts in iterate_audios(date, station):
-        matches = []
+        results = []
         for match in dejavu.recognize(recognizer, path, 5):
-            matches.append({
-                'confidence': match['confidence'],
-                'timestamp': ts
-            })
-            print("{} {} {}".format(
-                match['confidence'],
-                ts,
-                match['name'],
-            ))
-            ts += float(match["length"]) / 1000.0
+            try:
+                results.append({
+                    'confidence': match['confidence'],
+                    'timestamp': ts
+                })
+                print("{} {}".format(ts, match['confidence']))
+            except KeyError as ex:
+                print(ex, file=sys.stderr)
 
+            ts += float(match["length"]) / 1000.0
         try:
             client.put_schedule_results(
                 item['schedule'],
                 item['id'],
-                matches
+                results
             )
         except ConnectionError as ex:
             print(ex, file=sys.stderr)
+        except UserWarning as warn:
+            print(str(warn), file=sys.stderr)
 
 def iterate_audios(dt, station):
+    """ Given a datetime object and an station,
+    iterate a list of files that are between
+    the the date and itself plus 5 minutes;
+    station must match too """
     from_time = time.mktime(dt.timetuple())
     to_time = from_time + 5 * 60
 
@@ -129,12 +141,14 @@ def iterate_audios(dt, station):
 
     for mp3path in files:
         mp3name = os.path.basename(mp3path)
-        mp3date = parse_date_filename(mp3_name)
+        mp3date = parse_date_filename(mp3name)
         if mp3date:
             mp3ts = int(mp3date.strftime("%s"))
             yield (mp3path, mp3name, mp3ts)
 
 def cloud_download(ad_key=None):
+    """ Given an ad key, the file is downloaded to
+    the system temporal folder to be processed """
     ad = fbdb.reference('ads/{}'.format(ad_key)).get()
     filename = os.path.basename(ad['path'])
     out_file = os.path.join(AUDIOS_PATH, filename)
@@ -145,15 +159,14 @@ def cloud_download(ad_key=None):
         with open(out_file, "wb") as fp:
             fp.write(response.content)
             return out_file
-    else:
-        print('no')
-        return None
 
 def parse_date_filename(val):
     try:
-        return strptime(val[:19], '%Y-%m-%dT%H-%M-%S')
+        return datetime.strptime(val[:19],
+            '%Y-%m-%dT%H-%M-%S')
     except ValueError:
-        return strptime(val[:19], '%Y-%m-%d-%H-%M-%S')
+        return datetime.strptime(val[:19],
+            '%Y-%m-%d-%H-%M-%S')
 
 app = Application()
 loop = IOLoop.current()