sintonia/utils/airtime-silan

85 lines
2.4 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
2015-03-16 19:54:29 +01:00
import json
import logging
2015-03-16 19:54:29 +01:00
import os
import subprocess
import sys
2015-03-16 19:54:29 +01:00
import traceback
from api_clients import api_client as apc
from configobj import ConfigObj
2015-03-16 19:54:29 +01:00
# create logger
logger = logging.getLogger()
# no logging
ch = logging.StreamHandler()
logging.disable(50)
# add ch to logger
logger.addHandler(ch)
if os.geteuid() != 0:
2021-05-27 16:23:02 +02:00
print "Must be a root user."
2015-03-16 19:54:29 +01:00
sys.exit(1)
# loading config file
try:
2021-05-27 16:23:02 +02:00
config = ConfigObj("/etc/airtime/airtime.conf")
2015-03-16 19:54:29 +01:00
except Exception, e:
2021-05-27 16:23:02 +02:00
print ("Error loading config file: %s", e)
2015-03-16 19:54:29 +01:00
sys.exit(1)
api_client = apc.AirtimeApiClient(config)
try:
# keep getting few rows at a time for current music_dir (stor
# or watched folder).
subtotal = 0
while True:
# return a list of pairs where the first value is the
# file's database row id and the second value is the
# filepath
files = api_client.get_files_without_silan_value()
total_files = len(files)
2021-05-27 16:23:02 +02:00
if total_files == 0:
break
2015-03-16 19:54:29 +01:00
processed_data = []
total = 0
for f in files:
2021-05-27 16:23:02 +02:00
full_path = f["fp"]
2015-03-16 19:54:29 +01:00
# silence detect(set default queue in and out)
try:
2021-05-27 16:23:02 +02:00
command = ["silan", "-b" "-f", "JSON", full_path]
2015-03-16 19:54:29 +01:00
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
2021-05-27 16:23:02 +02:00
out = proc.communicate()[0].strip("\r\n")
2015-03-16 19:54:29 +01:00
info = json.loads(out)
data = {}
2021-05-27 16:23:02 +02:00
data["cuein"] = str("{0:f}".format(info["sound"][0][0]))
data["cueout"] = str("{0:f}".format(info["sound"][-1][1]))
data["length"] = str("{0:f}".format(info["file duration"]))
processed_data.append((f["id"], data))
2015-03-16 19:54:29 +01:00
total += 1
if total % 5 == 0:
2021-05-27 16:23:02 +02:00
print "Total %s / %s files has been processed.." % (
total,
total_files,
)
2015-03-16 19:54:29 +01:00
except Exception, e:
print e
print traceback.format_exc()
print "Processed: %d songs" % total
subtotal += total
try:
print api_client.update_cue_values_by_silan(processed_data)
2021-05-27 16:23:02 +02:00
except Exception, e:
2015-03-16 19:54:29 +01:00
print e
print traceback.format_exc()
print "Total %d songs Processed" % subtotal
except Exception, e:
print e
print traceback.format_exc()