broadcast-render.lua 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env lua
  2. --[[
  3. -- Copyright (c) 2013-2016 Marcus Rohrmoser, http://purl.mro.name/recorder
  4. --
  5. -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  6. -- associated documentation files (the "Software"), to deal in the Software without restriction,
  7. -- including without limitation the rights to use, copy, modify, merge, publish, distribute,
  8. -- sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  9. -- furnished to do so, subject to the following conditions:
  10. --
  11. -- The above copyright notice and this permission notice shall be included in all copies or
  12. -- substantial portions of the Software.
  13. --
  14. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  15. -- NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. -- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  17. -- OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. -- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. --
  20. -- MIT License http://opensource.org/licenses/MIT
  21. ]]
  22. if os.getenv('HTTP_HOST') then
  23. io.write('HTTP/1.1 400 Bad Request', '\n')
  24. io.write('Content-Type: text/plain', '\n')
  25. io.write('Server: RadioPi 2013/lua', '\n')
  26. io.write('\n', 'I\'m not supposed to be run as cgi')
  27. io.write('\n')
  28. io.flush()
  29. os.exit(1)
  30. end
  31. if arg[1] == nil or arg[1] == '-?' or arg[1] == '--help' then
  32. io.write([[
  33. Usage:
  34. display this help info
  35. $ broadcast-render.lua --help
  36. update/create future broadcasts from meta data
  37. $ broadcast-render.lua --luatables
  38. update/create all broadcasts from meta data
  39. $ broadcast-render.lua --luatables --update-past
  40. ]])
  41. os.exit(0)
  42. end
  43. -- ensure recorder.lua (next to this file) is found:
  44. package.path = arg[0]:gsub('/[^/]+/?$', '') .. '/?.lua;' .. package.path
  45. require('Recorder')
  46. Recorder.chdir2app_root( arg[0] )
  47. -- local prof = require 'profiler'
  48. -- prof.start()
  49. -- multiple xml files to create/update
  50. if arg[1] == '--luatables' then
  51. local metas,err = loadstring(table.concat{'return {', io.read('*a'), '}'})
  52. if metas == nil then
  53. io.write('error: ', err, "\n")
  54. os.exit(1)
  55. end
  56. local ok,metas = pcall(metas)
  57. if not ok then
  58. io.write('error: ', metas, "\n")
  59. os.exit(1)
  60. end
  61. -- table.sort(metas, function(a,b) return a.DC_format_timestart < b.DC_format_timestart end)
  62. local time_limit_min = os.time()
  63. -- DO only overwrite already started or past broadcasts if explicitely told
  64. if arg[2] == '--update-past' then time_limit_min = 0 end
  65. local process = function(meta)
  66. local bc = Broadcast.from_meta(meta)
  67. if bc:dtstart() <= time_limit_min then return bc:filename('xml'),'ignored' end -- don't overwrite already started or past broadcasts
  68. bc:match_podcasts()
  69. return bc:save()
  70. end
  71. for _,meta in ipairs(metas) do
  72. local ok,file,msg,err = pcall(process, meta)
  73. if ok then
  74. -- io.stderr:write(msg, ' ', file, "\n")
  75. else
  76. io.stderr:write('error: ', file, "\n")
  77. end
  78. end
  79. os.exit(0)
  80. end
  81. assert(false,'not supported')