airtime-silan 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/python
  2. from configobj import ConfigObj
  3. from api_clients import api_client as apc
  4. import logging
  5. import json
  6. import os
  7. import sys
  8. import subprocess
  9. import traceback
  10. # create logger
  11. logger = logging.getLogger()
  12. # no logging
  13. ch = logging.StreamHandler()
  14. logging.disable(50)
  15. # add ch to logger
  16. logger.addHandler(ch)
  17. if os.geteuid() != 0:
  18. print 'Must be a root user.'
  19. sys.exit(1)
  20. # loading config file
  21. try:
  22. config = ConfigObj('/etc/airtime/airtime.conf')
  23. except Exception, e:
  24. print('Error loading config file: %s', e)
  25. sys.exit(1)
  26. api_client = apc.AirtimeApiClient(config)
  27. try:
  28. # keep getting few rows at a time for current music_dir (stor
  29. # or watched folder).
  30. subtotal = 0
  31. while True:
  32. # return a list of pairs where the first value is the
  33. # file's database row id and the second value is the
  34. # filepath
  35. files = api_client.get_files_without_silan_value()
  36. total_files = len(files)
  37. if total_files == 0: break
  38. processed_data = []
  39. total = 0
  40. for f in files:
  41. full_path = f['fp']
  42. # silence detect(set default queue in and out)
  43. try:
  44. command = ['silan', '-b' '-f', 'JSON', full_path]
  45. proc = subprocess.Popen(command, stdout=subprocess.PIPE)
  46. out = proc.communicate()[0].strip('\r\n')
  47. info = json.loads(out)
  48. data = {}
  49. data['cuein'] = str('{0:f}'.format(info['sound'][0][0]))
  50. data['cueout'] = str('{0:f}'.format(info['sound'][-1][1]))
  51. data['length'] = str('{0:f}'.format(info['file duration']))
  52. processed_data.append((f['id'], data))
  53. total += 1
  54. if total % 5 == 0:
  55. print "Total %s / %s files has been processed.." % (total, total_files)
  56. except Exception, e:
  57. print e
  58. print traceback.format_exc()
  59. print "Processed: %d songs" % total
  60. subtotal += total
  61. try:
  62. print api_client.update_cue_values_by_silan(processed_data)
  63. except Exception ,e:
  64. print e
  65. print traceback.format_exc()
  66. print "Total %d songs Processed" % subtotal
  67. except Exception, e:
  68. print e
  69. print traceback.format_exc()