basic.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import os.path
  2. import mutagen
  3. def maxwait(songs, context, conf):
  4. wait = int(conf.get('EF_MAXWAIT_SEC', 0))
  5. if wait == 0:
  6. return True
  7. print(context)
  8. curpos, duration = map(int, context['status']['time'].split(':'))
  9. remaining = duration - curpos
  10. if remaining > wait:
  11. return False, 'remaining %d max allowed %d' % (remaining, wait)
  12. return True
  13. def get_duration(path):
  14. '''get track duration in seconds'''
  15. audio = mutagen.File(path)
  16. return int(audio.info.length)
  17. def percentwait(songs, context, conf, getdur=get_duration):
  18. percentwait = int(conf.get('EF_MAXWAIT_PERC', 0))
  19. if percentwait == 0:
  20. return True
  21. print(context)
  22. curpos, duration = map(int, context['status']['time'].split(':'))
  23. remaining = duration - curpos
  24. eventduration = 0
  25. for uri in songs['uris']:
  26. if not uri.startswith('file://'):
  27. return True, '%s is not a file' % uri
  28. path = uri[len('file://'):] # strips file://
  29. eventduration += getdur(path)
  30. wait = eventduration * (percentwait/100.)
  31. if remaining > wait:
  32. return False, 'remaining %d max allowed %d' % (remaining, wait)
  33. return True