Last active
December 31, 2015 10:18
-
-
Save ties/7972012 to your computer and use it in GitHub Desktop.
Quick and dirty script to add all the channels from the VCK IPTV (vlc) playlist to tvheadend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Parse all multicast URL's from the IPTV stream list | |
| # | |
| import json, os, collections, re, requests, urlparse | |
| TVHEADEND_USER = '' | |
| TVHEADEND_PASS = '' | |
| assert TVHEADEND_USER and TVHEADEND_PASS | |
| TVHEADEND_ROOT = 'http://server:9981/' | |
| Channel = collections.namedtuple('Channel', ['name', 'url', 'extras']) | |
| class ParseVLC(object): | |
| line_regex = re.compile("#EXT(?P<tag>\w+):(?P<value>.*)") | |
| def __init__(self, file_name): | |
| if os.path.isfile(file_name): | |
| self.file = open(file_name, 'rb') | |
| else: | |
| raise ValueError("File {} does not exist".format(file_name)) | |
| def __iter__(self): | |
| ext_m3u = False | |
| buffer = [] | |
| # all lines with whitespace stripped | |
| for line in [line.strip() for line in self.file]: | |
| # eat everything until the header was found | |
| if not ext_m3u: | |
| if 'EXTM3U' in line: | |
| ext_m3u = True | |
| continue | |
| if line: | |
| buffer.append(line) | |
| else: | |
| yield self.parse_section(buffer) | |
| buffer = [] | |
| def parse_section(self, section): | |
| name = None | |
| url = None | |
| extras = {} | |
| for line in section: | |
| m = self.line_regex.match(line) | |
| if m: | |
| tag, value = m.groups() | |
| if tag == 'INF': | |
| _, name = value.split(',') | |
| if tag == 'VLCOPT': | |
| key, val = value.split('=') | |
| extras[key] = val | |
| else: | |
| url = line | |
| return Channel(name, url, extras) | |
| """ | |
| Wrapper for HTTP request with basic auth, post data | |
| """ | |
| def tvheadend_api_post(sub_url, data): | |
| url = urlparse.urljoin(TVHEADEND_ROOT, sub_url) | |
| res = requests.post(url, data=data, auth=(TVHEADEND_USER, TVHEADEND_PASS)) | |
| return res.json() | |
| """ | |
| Use the TVHeadend API to add all channels: | |
| """ | |
| p = ParseVLC('VCK IPTV - Zapper.vlc') | |
| for channel in p: | |
| # Get the UUID of iptv or the new channel, not sure. | |
| uuid_request = tvheadend_api_post("/api/idnode/load", data={'class': "mpegts_network", 'enum': 1, 'query': ''}) | |
| add_request = tvheadend_api_post("/api/mpegts/network/mux_create", data = { | |
| 'uuid': uuid_request['entries'][0]['key'], | |
| 'conf': json.dumps({ | |
| "enabled": True, | |
| "initscan": False, | |
| "iptv_url": channel.url, | |
| "iptv_interface": "eth0", | |
| "charset": "AUTO" | |
| }) | |
| }) | |
| print "Added channel {} at {}".format(channel.name, channel.url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment