summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--epg.lua69
-rw-r--r--main.lua66
2 files changed, 76 insertions, 59 deletions
diff --git a/epg.lua b/epg.lua
new file mode 100644
index 0000000..c7af266
--- /dev/null
+++ b/epg.lua
@@ -0,0 +1,69 @@
+-- Copyright 2025 David Vazgenovich Shakaryan
+
+local epg = {}
+local mt = {}
+mt.__index = mt
+
+function epg.new()
+ return setmetatable({channels = {}}, mt)
+end
+
+-- local (non-DST) offset from UTC
+local tz_offset
+do
+ local t = os.time()
+ tz_offset = os.time(os.date('*t', t)) - os.time(os.date('!*t', t))
+end
+
+local function parse_time(str)
+ local y, m, d, hh, mm, ss, zsign, zh, zm = str:match(
+ '(%d%d%d%d)(%d%d)(%d%d)(%d%d)(%d%d)(%d%d) ([+-])(%d%d)(%d%d)')
+ local dt = {
+ year = y, month = m, day = d,
+ hour = hh, min = mm, sec = ss, isdst = false}
+ return os.time(dt) + tz_offset -
+ (tonumber(zsign..zh) * 3600 + tonumber(zsign..zm) * 60)
+end
+
+function mt:load_xc_data(data)
+ for _, v in ipairs(data) do
+ local ch = v.channel:lower()
+ local prog = {
+ start = parse_time(v.start),
+ stop = parse_time(v.stop),
+ title = v.title,
+ desc = v.desc,
+ }
+
+ if self.channels[ch] then
+ self.channels[ch][#self.channels[ch]+1] = prog
+ else
+ self.channels[ch] = {prog}
+ end
+ end
+
+ for _, progs in pairs(self.channels) do
+ table.sort(progs, function(a, b)
+ return a.start < b.start
+ end)
+ end
+end
+
+function mt:channel_programmes(ch)
+ return self.channels[ch]
+end
+
+function mt:scheduled_programme(ch, time)
+ local progs = self.channels[ch]
+ if not progs then
+ return
+ end
+
+ for _, v in ipairs(progs) do
+ if time >= v.start and time < v.stop then
+ return v
+ end
+ end
+end
+
+return epg
diff --git a/main.lua b/main.lua
index 87ecc50..4805c10 100644
--- a/main.lua
+++ b/main.lua
@@ -2,6 +2,7 @@
local util = require('util')
local _downloader = require('downloader')
+local _epg = require('epg')
local _xc = require('xc')
local mp_utils = require('mp.utils')
@@ -53,7 +54,7 @@ osd_bg.data = '{\\pos(0,0)}' .. colours.bg ..
local osd_img
local objects = {}
-local epg = {}
+local epg = _epg.new()
local favourites
local playing_id
@@ -190,47 +191,6 @@ local function load_section(section, name, section_type)
end
end
--- local (non-DST) offset from UTC
-local tz_offset
-do
- local t = os.time()
- tz_offset = os.time(os.date('*t', t)) - os.time(os.date('!*t', t))
-end
-local function epg_parse_time(str)
- local y, m, d, hh, mm, ss, zsign, zh, zm = str:match(
- '(%d%d%d%d)(%d%d)(%d%d)(%d%d)(%d%d)(%d%d) ([+-])(%d%d)(%d%d)')
- local dt = {
- year = y, month = m, day = d,
- hour = hh, min = mm, sec = ss, isdst = false}
- return os.time(dt) + tz_offset -
- (tonumber(zsign..zh) * 3600 + tonumber(zsign..zm) * 60)
-end
-
-local function load_epg()
- local tmp = cached_xc_call('get_epg')
- for _, v in ipairs(tmp) do
- local ch = v.channel:lower()
- local prog = {
- start = epg_parse_time(v.start),
- stop = epg_parse_time(v.stop),
- title = v.title,
- desc = v.desc,
- }
-
- if epg[ch] then
- epg[ch][#epg[ch]+1] = prog
- else
- epg[ch] = {prog}
- end
- end
-
- for _, progs in pairs(epg) do
- table.sort(progs, function(a, b)
- return a.start < b.start
- end)
- end
-end
-
local function load_data()
add_object({
type = 'group',
@@ -247,7 +207,7 @@ local function load_data()
load_section('live', 'Live TV', 'live')
load_section('movie', 'Movies', 'vod')
load_section('series', 'Series', 'series')
- load_epg()
+ epg:load_xc_data(cached_xc_call('get_epg'))
favourites = util.read_json_file(
mp_utils.join_path(script_dir, 'favourites.json'))
@@ -624,22 +584,9 @@ local function toggle_menu_sort()
update_osd()
end
-local function epg_programme(channel, time)
- local progs = epg[channel]
- if not progs then
- return
- end
-
- for _, v in ipairs(progs) do
- if time >= v.start and time < v.stop then
- return v
- end
- end
-end
-
local function add_programme(opt, time)
if opt.epg_channel_id then
- local prog = epg_programme(opt.epg_channel_id, time)
+ local prog = epg:scheduled_programme(opt.epg_channel_id, time)
if prog then
opt.info = asscape(prog.title)
end
@@ -913,14 +860,15 @@ end
local function open_option_epg(opt)
local ch = opt.epg_channel_id:lower()
- if not epg[ch] then
+ local progs = epg:channel_programmes(ch)
+ if not progs then
return
end
local options = {}
local curr = 0
local time = os.time()
- for i, v in ipairs(epg[ch]) do
+ for i, v in ipairs(progs) do
prog = {
name = os.date('%a %d %b %H:%M', v.start) .. ' ' ..
os.date('%H:%M', v.stop) .. ' ' .. v.title,