В парсер M3U добавлена поддержка дополнительных тегов (группы, страны и т.д.)
authorRoman Bazalevsky <rvb@rvb.name>
Mon, 2 Nov 2015 11:14:52 +0000 (14:14 +0300)
committerRoman Bazalevsky <rvb@rvb.name>
Mon, 2 Nov 2015 11:14:52 +0000 (14:14 +0300)
plugins/m3u_plugin.py
plugins/m3u_plugin.py~ [deleted file]
plugins/modules/M3uParser.py
plugins/modules/PlaylistGenerator.py
vlcclient/vlcmessages.py
xmlparse.py [new file with mode: 0644]

index 636ad4e19d6f72e348d4aebf18992496c687f6b5..1bb6483b34866e0aee5823625d94a22166cd7c9f 100644 (file)
@@ -68,9 +68,12 @@ class M3u(VPProxyPlugin):
 
         if self.splitted_path[1]=='index':
 
+            text=""
             for dir in os.walk(config.m3u_directory):
                 if dir[0]==config.m3u_directory:
-                    text='\n'.join(dir[2])
+                    for dirname in dir[2]:
+                        if dirname.endswith('.m3u'):
+                            text=text+'\n'+dirname
 
             connection.send_response(200)
             connection.send_header('Content-Type', 'text/plain; charset=utf-8')
@@ -158,6 +161,23 @@ class M3u(VPProxyPlugin):
                     channel=dict()
                     channel['name']=record.title.decode('utf-8')
                     channel['url']=record.path.decode('utf-8')            
+                    try:
+                      channel['tvg']=record.attrs['tvg-name'].decode('utf-8')
+                    except:
+                      None
+                    try: 
+                      if record.attrs['group-title'] != 'None':   
+                        channel['group']=record.attrs['group-title'].decode('utf-8')
+                    except:
+                      None
+                    try:    
+                      channel['country']=record.attrs['country'].decode('utf-8')
+                    except:
+                      None
+                    try:    
+                      channel['logo']=record.attrs['tvg-logo'].decode('utf-8')
+                    except:
+                      None
                     playlistgen.addItem(channel)
 
             exported = playlistgen.exportm3u(hostport,prefix)
diff --git a/plugins/m3u_plugin.py~ b/plugins/m3u_plugin.py~
deleted file mode 100644 (file)
index 3d2de5c..0000000
+++ /dev/null
@@ -1,171 +0,0 @@
-__author__ = 'rvb'
-'''
-Local Playlist Plugin
-(based on ytv plugin by ValdikSS)
-
-plsylist index
-
-http://ip:port/index
-
-m3u playlists
-
-http://ip:port/m3u
-http://ip:port/m3u/
-http://ip:port/m3u/list-name
-
-plain-tet channel names
-
-http://ip:port/list
-http://ip:port/list/
-http://ip:port/list/list-name
-
-forward to player
-
-http://ip:port/play/channel-name
-http://ip:port/play/list-name/channel-name
-http://ip:port/play/{list-name}/(get|mp4|webm)/channel-name
-
-'''
-import json
-import logging
-import urlparse
-import urllib
-from modules.PluginInterface import VPProxyPlugin
-from modules.PlaylistGenerator import PlaylistGenerator
-from modules.M3uParser import parseM3U
-import config.m3u as config
-import os
-
-class M3u(VPProxyPlugin):
-
-    handlers = ('m3u', 'm3ut', 'm3uw', "list", "play", "index")
-
-    logger = logging.getLogger('plugin_m3u')
-    playlist = None
-
-    def handle(self, connection):
-
-        hostport = connection.headers['Host']
-
-        self.splitted_path=connection.path.split('/')
-
-        if self.splitted_path[1]=='m3u':
-          prefix='get'
-        elif self.splitted_path[1]=='m3uw':
-          prefix='ogg'  
-        elif self.splitted_path[1]=='m3ut':
-          prefix='mp4'  
-        elif self.splitted_path[1] in ("list","play","index"):
-          None  
-        else:
-          connection.dieWithError(404)  
-          
-        if len(self.splitted_path)>3 and self.splitted_path[1]!="play":
-            connection.dieWithError()
-            return
-
-        if self.splitted_path[1]=='index':
-
-            for dir in os.walk(config.m3u_directory):
-                if dir[0]==config.m3u_directory:
-                    text='\n'.join(dir[2])
-
-            connection.send_response(200)
-            connection.send_header('Content-Type', 'text/plain; charset=utf-8')
-            connection.end_headers()
-
-            listing = text.encode('utf-8')
-            connection.wfile.write(listing) 
-                
-            return
-            
-        if len(self.splitted_path)<3 or (len(self.splitted_path)==3 and self.splitted_path[1]=="play"):
-
-           m3u_file=config.m3u_directory+'/'+config.m3u_default
-
-        else:
-        
-           filename = self.splitted_path[2]
-           if filename:
-             m3u_file=config.m3u_directory+'/'+self.splitted_path[2]
-           else:
-             m3u_file=config.m3u_directory+'/'+config.m3u_default
-
-        try:
-            playlist=parseM3U(m3u_file)
-        except:
-            connection.dieWithError(404)
-            return    
-
-        if self.splitted_path[1]=="list":
-            connection.send_response(200)
-            connection.send_header('Content-Type', 'text/plain; charset=utf-8')
-            connection.end_headers()
-        elif self.splitted_path[1]=="play":
-            channel=self.splitted_path[len(self.splitted_path)-1]
-            channel=urllib.unquote(channel).decode('utf-8')
-            if len(self.splitted_path)<=4:
-              prefix="get"
-            elif len(self.splitted_path)==5:  
-              prefix=self.splitted_path[len(self.splitted_path)-2]
-              if prefix not in ('get','mp4','webm'):
-                connection.dieWithError()
-            else:
-              connection.dieWithError()  
-            url=None
-            for record in playlist:
-                if record.title.decode('utf-8').replace('/','')==channel:
-                    url=record.path.decode('utf-8')
-            if url:
-                redirect='/'+prefix+'/'+url
-                connection.send_response(302)
-                connection.send_header('Location', redirect)
-                connection.end_headers()
-            else:
-                connection.dieWithError(404)
-            
-        else:
-            connection.send_response(200)
-            connection.send_header('Content-Type', 'application/x-mpegurl')
-            connection.end_headers()
-
-        try:
-            playlist=parseM3U(m3u_file)
-        except:
-            connection.dieWithError(404)
-            return    
-
-        if not playlist:
-            connection.dieWithError()
-            return
-
-        if self.splitted_path[1]=="list":
-             exported = ""
-            
-             for record in playlist:
-                 if record.title:
-                     exported = exported + "" + record.title.decode('utf-8').replace('/','') + "\n"
-        
-        else:    
-
-            playlistgen = PlaylistGenerator()
-
-            for record in playlist:
-                print record
-                channel=dict()
-                channel['name']=record.title.decode('utf-8')
-                channel['url']=record.path.decode('utf-8')            
-                playlistgen.addItem(channel)
-
-            exported = playlistgen.exportm3u(hostport,prefix)
-
-        exported = exported.encode('utf-8')
-        
-        connection.wfile.write(exported)
-
-    def getparam(self, key):
-        if key in self.params:
-            return self.params[key][0]
-        else:
-            return None
index 9e2fce64aeb79ffdb7d69bcb84c0ed2d5a256bb5..e946ec27bfa86689b4eb8f5b675986e7f2bbb39e 100644 (file)
@@ -6,10 +6,11 @@ import hexdump
 import codecs
 
 class track():
-    def __init__(self, length, title, path):
+    def __init__(self, length, title, path, attrs=None):
         self.length = length
         self.title = title
         self.path = path
+        self.attrs = attrs
 
 
 # # # song info lines are formatted like:
@@ -41,9 +42,22 @@ def parseM3U(infile):
         line = line.strip()
         if line.startswith('#EXTINF:'):
             # pull length and title from #EXTINF line
-            length,title=line.split('#EXTINF:')[1].split(',',1)
+            prefix,title=line.split('#EXTINF:')[1].split(',',1)
+            title=title.strip()
+            length,attrstr=prefix.split(' ',1)
+            attrs={}
+
+            while attrstr:
+
+              attrstr=attrstr.strip()
+              key,tail=attrstr.split('=',1)
+              tail=tail[1:]
+              value,attrstr=tail.split('"',1)
+              attrstr=attrstr.strip()
+              attrs[key]=value 
+            
             title=title.decode('string_escape')
-            song=track(length,title,None)
+            song=track(length,title,None,attrs)
         elif (len(line) != 0):
             # pull song path from all other, non-blank lines
             song.path=line
index c46197f35d6a20711efa6d359dc0718e920a269d..4e7654c57be9394e260737dfd2d356d36b544e1b 100644 (file)
@@ -12,7 +12,7 @@ class PlaylistGenerator(object):
         '#EXTM3U url-tvg="http://www.teleguide.info/download/new3/jtv.zip"\n'
     m3uemptyheader = '#EXTM3U\n'
     m3uchanneltemplate = \
-        '#EXTINF:-1 group-title="%s" tvg-name="%s" tvg-logo="%s",%s\n%s\n'
+        '#EXTINF:-1 group-title="%s" tvg-name="%s" tvg-logo="%s" country="%s",%s\n%s\n'
 
     def __init__(self):
         self.itemlist = list()
@@ -24,6 +24,7 @@ class PlaylistGenerator(object):
             name - item name
             url - item URL
             tvg - item JTV name (optional)
+            country - country of origin (optional)
             group - item playlist group (optional)
             logo - item logo file name (optional)
         '''
@@ -34,9 +35,14 @@ class PlaylistGenerator(object):
         '''
         Generates EXTINF line with url
         '''
+        print(item)
         return PlaylistGenerator.m3uchanneltemplate % (
-            item.get('group', ''), item.get('tvg', ''), item.get('logo', ''),
-            item.get('name'), item.get('url'))
+            item.get('group', ''), 
+            item.get('tvg', ''), 
+            item.get('logo', ''),
+            item.get('country', ''),
+            item.get('name'), 
+            item.get('url'))
 
     def exportm3u(self, hostport, prefix="get", add_ts=False, empty_header=False, archive=False):
         '''
@@ -58,3 +64,17 @@ class PlaylistGenerator(object):
             itemlist += PlaylistGenerator._generatem3uline(item)
 
         return itemlist
+
+    def dumpm3u(self):
+        '''
+        Dump m3u playlist
+        '''
+        itemlist = PlaylistGenerator.m3uemptyheader
+
+        for item in self.itemlist:
+            item['tvg'] = item.get('tvg', '') if item.get('tvg') else \
+                item.get('name').replace(' ', '_')
+            # For .acelive and .torrent
+            itemlist += PlaylistGenerator._generatem3uline(item)
+
+        return itemlist
index 48e9321e0b78ae1f2855fb6be95d4b7e4c130908..2139e3fa41aa6644ba55ffc6bc908e2fa24b3497 100644 (file)
@@ -17,7 +17,7 @@ class VlcMessage(object):
                 command = command + 'transcode{vcodec=theora,acodec=vorbis,vb=800,ab=128}:'    
                 muxer='ogg'
             elif qtype=='ogv':
-                command = command + 'transcode{vcodec=theora,acodec=vorbis}:'    
+                command = command + 'transcode{vcodec=theora,acodec=vorbis,vb=8192,ab=256}:'    
                 muxer='ogg'
 #            elif qtype=='webm':
 #                command = command + 'transcode{vcodec=VP80,acodec=vorbis,vb=512,ab=64}:'
diff --git a/xmlparse.py b/xmlparse.py
new file mode 100644 (file)
index 0000000..bc4c3cf
--- /dev/null
@@ -0,0 +1,60 @@
+#!/usr/bin/python
+
+import xml.dom.minidom
+import sys,os
+from pprint import pprint
+from plugins.modules.PlaylistGenerator import PlaylistGenerator
+
+tvguide_url="http://www.teleguide.info/download/new3/jtv.zip"
+
+def parseXML(filename):
+  datasource = open(filename)
+  return xml.dom.minidom.parse(datasource)   # parse an open file
+
+def getText(element):
+  rc = []
+  nodelist = element.childNodes
+  for node in nodelist:
+    if node.nodeType == node.TEXT_NODE:
+      rc.append(node.data)
+      return ''.join(rc)
+
+def main(argv):
+  if len(argv) < 2:
+    sys.stderr.write("Usage: %s <xml-file>\n" % (argv[0],))
+    return 1
+                    
+  if not os.path.exists(argv[1]):
+    sys.stderr.write("ERROR: Channel file %r was not found!\n" % (argv[1],))
+    return 1
+
+  dom = parseXML(argv[1])
+  tracklist = dom.getElementsByTagName('playlist')[0].getElementsByTagName('trackList')[0].getElementsByTagName('track')
+
+  channels = PlaylistGenerator()
+  
+  for track in tracklist:
+  
+    channel = {}
+  
+    channel['name'] = getText(track.getElementsByTagName('title')[0])
+    channel['url'] = getText(track.getElementsByTagName('location')[0])
+    try:
+      channel['logo'] = getText(track.getElementsByTagName('image')[0])
+    except:
+      None
+    try:    
+      channel['country'] = getText(track.getElementsByTagName('country')[0])
+    except:
+      None
+    try:        
+      channel['group'] = getText(track.getElementsByTagName('category')[0])
+    except:
+      None  
+
+    channels.addItem(channel)  
+
+  print channels.dumpm3u().encode('utf8')
+
+if __name__ == "__main__":
+   sys.exit(main(sys.argv))
\ No newline at end of file