| 1 | -- Copyright (C) 2011-2015 Anton Burdinuk
|
|---|
| 2 | -- clark15b@gmail.com
|
|---|
| 3 | -- https://tsdemuxer.googlecode.com/svn/trunk/xupnpd
|
|---|
| 4 |
|
|---|
| 5 | function add_playlists_from_dir(dir_path,playlist,plist)
|
|---|
| 6 |
|
|---|
| 7 | local d=util.dir(dir_path)
|
|---|
| 8 |
|
|---|
| 9 | if d then
|
|---|
| 10 | table.sort(d)
|
|---|
| 11 | local tt={}
|
|---|
| 12 | for i,j in ipairs(playlist) do
|
|---|
| 13 | local path=nil
|
|---|
| 14 | if type(j)=='table' then path=j[1] else path=j end
|
|---|
| 15 | tt[path]=j
|
|---|
| 16 | end
|
|---|
| 17 |
|
|---|
| 18 | for i,j in ipairs(d) do
|
|---|
| 19 | if string.find(j,'%.m3u$') then
|
|---|
| 20 | local fname=dir_path..j
|
|---|
| 21 | if not tt[fname] then
|
|---|
| 22 | table.insert(plist,fname)
|
|---|
| 23 | if cfg.debug>0 then print('found unlisted playlist \''..fname..'\'') end
|
|---|
| 24 | end
|
|---|
| 25 | end
|
|---|
| 26 | end
|
|---|
| 27 | end
|
|---|
| 28 | end
|
|---|
| 29 |
|
|---|
| 30 | function playlist_new_folder(parent,name)
|
|---|
| 31 | local child={}
|
|---|
| 32 | parent.size=parent.size+1
|
|---|
| 33 | child.name=name
|
|---|
| 34 | child.objid=parent.objid..'_'..parent.size
|
|---|
| 35 | child.parent=parent
|
|---|
| 36 | child.size=0
|
|---|
| 37 | child.elements={}
|
|---|
| 38 | parent.elements[parent.size]=child
|
|---|
| 39 | return child
|
|---|
| 40 | end
|
|---|
| 41 |
|
|---|
| 42 | function playlist_sort_elements(pls)
|
|---|
| 43 | if cfg.sort_files~=true or pls==nil or pls.elements==nil then return end
|
|---|
| 44 | table.sort(pls.elements,function(a,b) return string.lower(a.name)<string.lower(b.name) end)
|
|---|
| 45 | end
|
|---|
| 46 |
|
|---|
| 47 | function playlist_fix_sub_tree(pls)
|
|---|
| 48 | playlist_sort_elements(pls)
|
|---|
| 49 | for i,j in ipairs(pls.elements) do
|
|---|
| 50 | j.objid=pls.objid..'_'..i
|
|---|
| 51 | j.parent=pls
|
|---|
| 52 |
|
|---|
| 53 | if j.elements then
|
|---|
| 54 | playlist_fix_sub_tree(j)
|
|---|
| 55 | else
|
|---|
| 56 | j.type=util.getfext(j.url)
|
|---|
| 57 |
|
|---|
| 58 | if not mime[j.type] then j.type=cfg.default_mime_type end
|
|---|
| 59 | end
|
|---|
| 60 |
|
|---|
| 61 | end
|
|---|
| 62 | end
|
|---|
| 63 |
|
|---|
| 64 | function playlist_attach(parent,pls)
|
|---|
| 65 | parent.size=parent.size+1
|
|---|
| 66 | pls.objid=parent.objid..'_'..parent.size
|
|---|
| 67 | pls.parent=parent
|
|---|
| 68 | parent.elements[parent.size]=pls
|
|---|
| 69 | end
|
|---|
| 70 |
|
|---|
| 71 | function get_m3u_count(plist)
|
|---|
| 72 | local count=0
|
|---|
| 73 | for i,j in ipairs(plist) do
|
|---|
| 74 | if type(j)=='table' then j=j[1] end
|
|---|
| 75 | if string.find(j,'%.m3u$') then count=count+1 end
|
|---|
| 76 | end
|
|---|
| 77 | return count
|
|---|
| 78 | end
|
|---|
| 79 |
|
|---|
| 80 | function reload_playlists()
|
|---|
| 81 | playlist_data={}
|
|---|
| 82 | playlist_data.name='root'
|
|---|
| 83 | playlist_data.objid='0'
|
|---|
| 84 | playlist_data.size=0
|
|---|
| 85 | playlist_data.elements={}
|
|---|
| 86 |
|
|---|
| 87 | local plist=clone_table(playlist)
|
|---|
| 88 |
|
|---|
| 89 | add_playlists_from_dir(cfg.playlists_path,playlist,plist)
|
|---|
| 90 | if cfg.feeds_path~=cfg.playlists_path then add_playlists_from_dir(cfg.feeds_path,playlist,plist) end
|
|---|
| 91 |
|
|---|
| 92 | local pls_folder=playlist_data
|
|---|
| 93 |
|
|---|
| 94 | if cfg.group==true and get_m3u_count(plist)>0 then
|
|---|
| 95 | pls_folder=playlist_new_folder(playlist_data,'Playlists')
|
|---|
| 96 | end
|
|---|
| 97 |
|
|---|
| 98 | local groups={}
|
|---|
| 99 |
|
|---|
| 100 | for i,j in ipairs(plist) do
|
|---|
| 101 |
|
|---|
| 102 | local pls
|
|---|
| 103 |
|
|---|
| 104 | if type(j)=='table' then
|
|---|
| 105 |
|
|---|
| 106 | if string.find(j[1],'%.m3u$') then pls=m3u.parse(j[1]) else pls=m3u.scan(j[1]) end
|
|---|
| 107 |
|
|---|
| 108 | if pls then
|
|---|
| 109 | if j[2] then pls.name=j[2] end
|
|---|
| 110 | if j[3] then pls.acl=j[3] end
|
|---|
| 111 | end
|
|---|
| 112 | else
|
|---|
| 113 | if string.find(j,'%.m3u$') then pls=m3u.parse(j) else pls=m3u.scan(j) end
|
|---|
| 114 | end
|
|---|
| 115 |
|
|---|
| 116 | if pls then
|
|---|
| 117 | if pls.filesystem then
|
|---|
| 118 | playlist_attach(playlist_data,pls)
|
|---|
| 119 | else
|
|---|
| 120 | playlist_attach(pls_folder,pls)
|
|---|
| 121 | end
|
|---|
| 122 |
|
|---|
| 123 | if cfg.debug>0 then print('playlist \''..pls.name..'\'') end
|
|---|
| 124 |
|
|---|
| 125 | if pls.filesystem then
|
|---|
| 126 | playlist_fix_sub_tree(pls)
|
|---|
| 127 | else
|
|---|
| 128 | local udpxy=cfg.udpxy_url
|
|---|
| 129 |
|
|---|
| 130 | for ii,jj in ipairs(pls.elements) do
|
|---|
| 131 |
|
|---|
| 132 | if udpxy then
|
|---|
| 133 | if string.find(jj.url,'^udp://@') or string.find(jj.url,'^rtp://@') then
|
|---|
| 134 | jj.url=string.format('%s/%s/%s',udpxy,string.sub(jj.url,1,3),string.sub(jj.url,8))
|
|---|
| 135 | end
|
|---|
| 136 | end
|
|---|
| 137 |
|
|---|
| 138 | if not jj.type then
|
|---|
| 139 | if pls.type then
|
|---|
| 140 | jj.type=pls.type
|
|---|
| 141 | else
|
|---|
| 142 | jj.type=util.getfext(jj.url)
|
|---|
| 143 | end
|
|---|
| 144 | end
|
|---|
| 145 |
|
|---|
| 146 | if pls.plugin and not jj.plugin then jj.plugin=pls.plugin end
|
|---|
| 147 |
|
|---|
| 148 | if pls.dlna_extras and not jj.dlna_extras then jj.dlna_extras=pls.dlna_extras end
|
|---|
| 149 |
|
|---|
| 150 | if not mime[jj.type] then jj.type=cfg.default_mime_type end
|
|---|
| 151 |
|
|---|
| 152 | jj.objid=pls.objid..'_'..ii
|
|---|
| 153 | jj.parent=pls
|
|---|
| 154 | if cfg.debug>1 then print('\''..jj.name..'\' '..jj.url..' <'..jj.type..'>') end
|
|---|
| 155 |
|
|---|
| 156 | if cfg.group==true then
|
|---|
| 157 | local group_title=jj['group-title']
|
|---|
| 158 | if group_title then
|
|---|
| 159 | local group=groups[group_title]
|
|---|
| 160 | if not group then
|
|---|
| 161 | group={}
|
|---|
| 162 | group.name=group_title
|
|---|
| 163 | group.elements={}
|
|---|
| 164 | group.size=0
|
|---|
| 165 | group.virtual=true
|
|---|
| 166 | groups[group_title]=group
|
|---|
| 167 | end
|
|---|
| 168 |
|
|---|
| 169 | local element=clone_table(jj)
|
|---|
| 170 | element.parent=group
|
|---|
| 171 | element.objid=nil
|
|---|
| 172 | group.size=group.size+1
|
|---|
| 173 | group.elements[group.size]=element
|
|---|
| 174 | end
|
|---|
| 175 | end
|
|---|
| 176 | end
|
|---|
| 177 | end
|
|---|
| 178 | end
|
|---|
| 179 | end
|
|---|
| 180 |
|
|---|
| 181 | if cfg.group==true then
|
|---|
| 182 | for i,j in pairs(groups) do
|
|---|
| 183 |
|
|---|
| 184 | if cfg.debug>0 then print('group \''..j.name..'\'') end
|
|---|
| 185 |
|
|---|
| 186 | playlist_attach(playlist_data,j)
|
|---|
| 187 |
|
|---|
| 188 | for ii,jj in ipairs(j.elements) do
|
|---|
| 189 | jj.objid=j.objid..'_'..ii
|
|---|
| 190 | end
|
|---|
| 191 | end
|
|---|
| 192 | end
|
|---|
| 193 |
|
|---|
| 194 | end
|
|---|
| 195 |
|
|---|
| 196 | function find_playlist_object(s)
|
|---|
| 197 | if not s then return nil end
|
|---|
| 198 |
|
|---|
| 199 | local pls=nil
|
|---|
| 200 |
|
|---|
| 201 | for i in string.gmatch(s,'([^_]+)') do
|
|---|
| 202 | if not pls then
|
|---|
| 203 | if string.find(i,'^%d+$') then pls=playlist_data else return nil end
|
|---|
| 204 | else
|
|---|
| 205 | if not pls.elements then return nil end
|
|---|
| 206 | pls=pls.elements[tonumber(i)]
|
|---|
| 207 | end
|
|---|
| 208 | end
|
|---|
| 209 | return pls
|
|---|
| 210 | end
|
|---|
| 211 |
|
|---|
| 212 | function rss_merge(new,old,max_num)
|
|---|
| 213 |
|
|---|
| 214 | local tt={}
|
|---|
| 215 |
|
|---|
| 216 | for i,j in ipairs(old) do tt[j.title]=j end
|
|---|
| 217 | for i,j in ipairs(new) do if tt[j.title] then j.link=nil end end
|
|---|
| 218 |
|
|---|
| 219 | tt={} local idx=1
|
|---|
| 220 |
|
|---|
| 221 | for i,j in ipairs(new) do if idx>max_num then break end if j.link then tt[idx]=j idx=idx+1 end end
|
|---|
| 222 |
|
|---|
| 223 | for i,j in ipairs(old) do if idx>max_num then break end tt[idx]=j idx=idx+1 end
|
|---|
| 224 |
|
|---|
| 225 | return tt
|
|---|
| 226 | end
|
|---|
| 227 |
|
|---|
| 228 | function rss_parse_m3u(path)
|
|---|
| 229 | local t={}
|
|---|
| 230 |
|
|---|
| 231 | local x=m3u.parse(path)
|
|---|
| 232 | if x and x.elements then
|
|---|
| 233 | local idx=1
|
|---|
| 234 | for i,j in ipairs(x.elements) do
|
|---|
| 235 | t[idx]={ ['title']=j.name, ['link']=j.url, ['logo']=j.logo }
|
|---|
| 236 | idx=idx+1
|
|---|
| 237 | end
|
|---|
| 238 | end
|
|---|
| 239 |
|
|---|
| 240 | return t
|
|---|
| 241 | end
|
|---|
| 242 |
|
|---|
| 243 | function rss_parse_feed(url,logo_regexp)
|
|---|
| 244 | local t={}
|
|---|
| 245 |
|
|---|
| 246 | local feed_data=http.download(url)
|
|---|
| 247 |
|
|---|
| 248 | if not feed_data then return t end
|
|---|
| 249 |
|
|---|
| 250 | if not logo_regexp then logo_regexp='url="(.-)"' end
|
|---|
| 251 |
|
|---|
| 252 | local x=xml.find('rss/channel',xml.decode(feed_data))
|
|---|
| 253 |
|
|---|
| 254 | feed_data=nil
|
|---|
| 255 |
|
|---|
| 256 | if x and x['@elements'] then
|
|---|
| 257 | local idx=1
|
|---|
| 258 | for i,j in ipairs(x['@elements']) do
|
|---|
| 259 | if j['@name']=='item' then
|
|---|
| 260 | local title=nil if j.title then title=j.title['@value'] end
|
|---|
| 261 | local link =nil if j.link then link=j.link['@value'] end
|
|---|
| 262 | local logo =nil if j.enclosure then logo=j.enclosure['@attr'] end
|
|---|
| 263 |
|
|---|
| 264 | if logo then logo=string.match(logo,logo_regexp) end
|
|---|
| 265 |
|
|---|
| 266 | if title and link then
|
|---|
| 267 | t[idx]={ ['title']=title, ['link']=link, ['logo']=logo }
|
|---|
| 268 | idx=idx+1
|
|---|
| 269 | end
|
|---|
| 270 | end
|
|---|
| 271 | end
|
|---|
| 272 | end
|
|---|
| 273 | return t
|
|---|
| 274 | end
|
|---|
| 275 |
|
|---|
| 276 | reload_playlists()
|
|---|