Advertisement
and_cesbo

Astra. EPG Aggregator

Dec 22nd, 2015
2,806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. -- EPG Aggregator
  2. -- Required Astra 5.62-rc8 or newer
  3. --
  4. -- Installation:
  5. -- 1. Install Astra: https://cesbo.com/astra/quick-start/
  6. -- 2. Save script into the file: /etc/astra/epg.lua
  7. -- 3. Change options in the script:
  8. -- 3.1. epg_file - full path to the file to save EPG
  9. -- 3.2. epg_save_interval - save EPG into the file each 60 seconds
  10. -- 3.3. epg_server_port - port to receive EPG from Astra
  11. -- 3.4. epg_expire - expiration time. Channel will be excluded from EPG if has no updates more than 24 hours
  12. -- 3.5. epg_stalker - if true, convert <sub-title/> to <desc/>
  13. -- 4. Launch Astra: astra /etc/astra/epg.lua --daemon
  14. -- 5. In the Stream options (on Astra that receives channels), set EPG Export option. Format: JSON and Destination: http://server-address:5000 (server-address - ip address of the server with this script, 5000 - is a port number defined in the epg_server_port)
  15. --
  16.  
  17. epg_file = "/tmp/epg.xml"
  18. epg_save_interval = 60
  19. epg_server_port = 5000
  20. epg_expire = 24 * 60 * 60
  21. epg_stalker = false
  22.  
  23. --
  24.  
  25. epg_list = {}
  26.  
  27. function epg_save()
  28.     local channels = {}
  29.     local items = {}
  30.  
  31.     local ctime = os.time()
  32.     for id, data in pairs(epg_list) do
  33.         if data.expire < ctime then
  34.             epg_list[id] = nil
  35.         else
  36.             table.insert(channels, { id = id, name = data.name })
  37.             for _, x in ipairs(data.items) do
  38.                 table.insert(items, x)
  39.             end
  40.         end
  41.     end
  42.  
  43.     table.sort(channels, function(a, b)
  44.         return a.id < b.id
  45.     end)
  46.  
  47.     table.sort(items, function(a, b)
  48.         return a.start_ut < b.start_ut
  49.     end)
  50.  
  51.     local result = epg_utils.format["xmltv"]({ channels = channels, items = items })
  52.     local fd = assert(io.open(epg_file, "w"))
  53.     fd:write(result.content)
  54.     fd:close()
  55. end
  56.  
  57. timer({
  58.     interval = epg_save_interval,
  59.     callback = function()
  60.         epg_save()
  61.     end
  62. })
  63.  
  64. function on_http_request(server, client, request)
  65.     if not request then
  66.         return nil
  67.     end
  68.  
  69.     local data = json.decode(request.content)
  70.     if not data or not data.items then
  71.         server:abort(client, 400)
  72.         return nil
  73.     end
  74.  
  75.     if data.name then
  76.         -- deprecated
  77.         data.channels = {{ id = data.name, name = data.name }}
  78.  
  79.         for _,x in ipairs(data.items) do
  80.             x.channel = data.name
  81.         end
  82.  
  83.         data.name = nil
  84.     end
  85.  
  86.     if not data.channels then
  87.         server:abort(client, 400)
  88.         return nil
  89.     end
  90.  
  91.     if epg_stalker then
  92.         for _,x in ipairs(data.items) do
  93.             x["desc"], x["subtitle"] = x["subtitle"], nil
  94.         end
  95.     end
  96.  
  97.     local ch = data.channels[1]
  98.     epg_list[ch.id] = {
  99.         name = ch.name,
  100.         items = data.items,
  101.         expire = os.time() + epg_expire
  102.     }
  103.  
  104.     server:send(client, {
  105.         code = 200,
  106.         headers = { "Connection: close" }
  107.     })
  108. end
  109.  
  110. http_server({
  111.     port = epg_server_port,
  112.     route = {{"/", on_http_request}}
  113. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement