|
@@ -13,6 +13,7 @@ from firebase_admin import credentials
|
|
|
from firebase_admin import db as fbdb
|
|
|
from binascii import hexlify
|
|
|
from base64 import b64decode
|
|
|
+import logging as log
|
|
|
import firebase_admin
|
|
|
import mutagen.mp3
|
|
|
import requests
|
|
@@ -22,6 +23,8 @@ import time
|
|
|
import sys
|
|
|
import os
|
|
|
|
|
|
+log.basicConfig(format='[%(asctime)s] %(message)s', level=log.INFO)
|
|
|
+
|
|
|
AUDIOS_PATH = '/tmp'
|
|
|
AHEAD_TIME_AUDIO_TOLERANCE = 2 # second
|
|
|
|
|
@@ -48,22 +51,28 @@ def feed_queue():
|
|
|
server and add them to a memory queue. """
|
|
|
try:
|
|
|
response = client.get_schedule_pending()
|
|
|
+ downloaded_counter = len(response['items'])
|
|
|
for item in response['items']:
|
|
|
queue.put(item)
|
|
|
|
|
|
+ if downloaded_counter:
|
|
|
+ log.info('[feed_queue] {} new '
|
|
|
+ + 'pending schedule items.'\
|
|
|
+ .format(downloaded_counter)
|
|
|
+ )
|
|
|
+
|
|
|
if queue.qsize() > 0:
|
|
|
loop.add_callback(process_queue)
|
|
|
else:
|
|
|
loop.add_timeout(time.time() + 30, feed_queue)
|
|
|
|
|
|
except ConnectionError as ex:
|
|
|
- print('cannot feed: {}, retryig later'.format(ex),
|
|
|
- file=sys.stderr)
|
|
|
+ log.error('[feed_queue] cannot feed: {}, retryig later'.format(ex))
|
|
|
loop.add_timeout(time.time() + 15, feed_queue)
|
|
|
|
|
|
except Exception as ex:
|
|
|
""" Errores desconocidos """
|
|
|
- print(ex, file=sys.stderr)
|
|
|
+ log.error('[feed_queue] {}'.format(ex))
|
|
|
loop.add_timeout(time.time() + 60, feed_queue)
|
|
|
|
|
|
def process_queue():
|
|
@@ -77,7 +86,7 @@ def process_queue():
|
|
|
except Empty:
|
|
|
loop.add_callback(feed_queue)
|
|
|
except Exception as ex:
|
|
|
- print(str(ex), file=sys.stderr)
|
|
|
+ log.error(ex)
|
|
|
loop.add_callback(process_queue)
|
|
|
|
|
|
def process_segment(item):
|
|
@@ -90,7 +99,7 @@ def process_segment(item):
|
|
|
# y calcular su fingerprint.
|
|
|
filename, md5hash = cloud_download(ad_key=item['ad'])
|
|
|
if not filename:
|
|
|
- print('ad file missing', file=sys.stderr)
|
|
|
+ log.info('ad file missing')
|
|
|
return
|
|
|
|
|
|
# 1.1 Calcular el número de segmentos requeridos
|
|
@@ -99,14 +108,14 @@ def process_segment(item):
|
|
|
audio = mutagen.mp3.MP3(filename)
|
|
|
segments_needed = int(round(float(audio.info.length) / float(5)))
|
|
|
except Exception as ex:
|
|
|
- print('file {} is not an mp3'.format(audio))
|
|
|
- print(ex)
|
|
|
+ log.error('file {} is not an mp3'.format(audio))
|
|
|
+ log.error(str(ex))
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
dejavu.fingerprint_file(filename)
|
|
|
except Exception as ex:
|
|
|
- print('cannot fingerprint: {}'.format(ex))
|
|
|
+ log.error('cannot fingerprint: {}'.format(ex))
|
|
|
|
|
|
# 2. Read the list of files from local database
|
|
|
audios_counter = 0
|
|
@@ -122,9 +131,9 @@ def process_segment(item):
|
|
|
'timestamp': ts,
|
|
|
'offset': match['offset']
|
|
|
})
|
|
|
- print("{} {}".format(ts, match['confidence']))
|
|
|
+ log.info("{} {}".format(ts, match['confidence']))
|
|
|
except KeyError as ex:
|
|
|
- print(ex, file=sys.stderr)
|
|
|
+ log.error(str(ex))
|
|
|
|
|
|
ts += match['length'] / 1000
|
|
|
|
|
@@ -139,12 +148,11 @@ def process_segment(item):
|
|
|
missing_files=(12 - audios_counter) \
|
|
|
if audios_counter < 12 else 0
|
|
|
)
|
|
|
-
|
|
|
- print("api response: {}".format(response))
|
|
|
+ log.info('api response: {}'.format(response))
|
|
|
except ConnectionError as ex:
|
|
|
- print(ex, file=sys.stderr)
|
|
|
+ log.error(str(ex))
|
|
|
except UserWarning as warn:
|
|
|
- print(str(warn), file=sys.stderr)
|
|
|
+ log.warning(str(warn))
|
|
|
|
|
|
def find_repetitions(results, segments_needed=2):
|
|
|
found_counter = 0
|
|
@@ -186,7 +194,7 @@ def iterate_audios(dt, station):
|
|
|
- AHEAD_TIME_AUDIO_TOLERANCE
|
|
|
to_time = from_time + 3599 + AHEAD_TIME_AUDIO_TOLERANCE
|
|
|
|
|
|
- print('from {} to {}'.format(from_time, to_time))
|
|
|
+ log.info('from {} to {}'.format(from_time, to_time))
|
|
|
|
|
|
cursor = db.cursor()
|
|
|
cursor.execute((
|
|
@@ -243,6 +251,7 @@ loop.add_callback(feed_queue)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
try:
|
|
|
+ log.info('Starting ondemand service')
|
|
|
loop.start()
|
|
|
except KeyboardInterrupt:
|
|
|
- print('process killed')
|
|
|
+ log.error('Process killed')
|