Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added scripts/map_channelnumbers.py
extended config loading
moved methods from merger_channels.py to an API class
  • Loading branch information
ckarrie committed Nov 27, 2016
1 parent f6096d3 commit 9dc0749
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 105 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
*.egg-info/*
*.pyc
.idea*
9 changes: 9 additions & 0 deletions scripts/__init__.py
@@ -1,5 +1,14 @@
import json
import os

CONFIG = {
'hostname': 'enter-your-tvheadend-hostname-or-ip',
'username': 'enter-your-tvheadend-username',
'password': 'enter-your-tvheadend-password',
}

if os.path.exists('config.json'):
with open('config.json', 'r') as config_file:
CONFIG = json.load(config_file)
config_file.close()

173 changes: 173 additions & 0 deletions scripts/map_channelnumbers.py
@@ -0,0 +1,173 @@
from tvh.htsp import HTSPClient
from tvh.api import HTSPApi
from scripts import CONFIG

htsp = HTSPClient((CONFIG['hostname'], 9982))
msg = htsp.hello()
htsp.authenticate(CONFIG['username'], CONFIG['password'])
htspapi = HTSPApi(htsp=htsp)

EXACT = '$'

channels = [
# [<Channel Name>, <Serive lookup, i.e. Mux>]
["Das Erste HD", "11493"],
["ZDF HD", "11361"],
["RTL HD", "10832"],
["SAT.1 HD", "11464"],
["ProSieben HD", "11464"],
["Kabel eins HD", "11464"],
["Kabel eins HD", "11464"],
["RTLII HD", "10832"],
["VOX HD", "10832"],
["arte HD", "11493"],
["3sat HD", "11347"],
["WDR HD Bonn", "12603"],
"NDR FS HH HD",
"BR Fernsehen Nord HD",
"SWR RP HD",
"rhein main tv",
"hr-fernsehen HD",
"MDR Sachsen HD",
"PHOENIX HD",
"ServusTV HD Deutschland",
"zdf_neo HD",
["ONE HD", "11052"],
"ZDFinfo HD",
["tagesschau24 HD", "11052"],
"ard-alpha",
"n-tv HD",
"N24 HD" + EXACT,
"rbb Berlin HD",
"N24 DOKU",
"kabel eins Doku",
"Zee One HD",
["TLC HD" + EXACT, "10964"],
["ANIXE HD" + EXACT, "10773"],
"Comedy Central/VIVA",
"KiKA HD",
"sat.1 gold hd" + EXACT,
"sport1 hd" + EXACT,
["Sport1", "12382"], # SPORT1+
"Sport1 US HD",
"SUPER RTL" + EXACT,
["NICKELODEON HD" + EXACT, "10773"],
["TELE 5 HD" + EXACT, "12574"],
["DMAX HD" + EXACT, "12574"],
"RTLNITRO HD",
"RTLplus",
"SIXX HD" + EXACT,
"Pro7 MAXX HD" + EXACT,

["SRF 1 HD", "10971"],
["SRF zwei HD", "10971"],
"ORF1 HD",
"ORF2W HD",

"Sky Cinema HD" + EXACT,
"Sky Cinema +1 HD" + EXACT,
"Sky Cinema +24 HD" + EXACT,
"Sky Cinema Hits HD" + EXACT,
"Sky Cinema Nostalgie" + EXACT,
"Sky Cinema Action HD" + EXACT,
["Sky Cinema Comedy" + EXACT, "11719"],
"Sky Cinema Emotion" + EXACT,
"Sky Cinema Family HD" + EXACT,
"Sky 1 HD" + EXACT,
["Sky Atlantic HD" + EXACT, "11992"],
["Sky Atlantic +1 HD" + EXACT, "11797"],
["Universal HD", "11875"],
"MGM HD",
["Sky Krimi", "12031"],
["Fox HD", "11332"],
["Fox Serie", "11758"],
"RTL Crime HD",
["13th Street HD", "11992"],
["Syfy HD", "12304"],
["TNT Serie HD", "12382"],
# Disney
["Disney Junior HD", "12070"],
["Disney Cinemagic HD", "11992"],
["Disney XD", "11719"],
"Motorvision TV",
["A&E", "10920"],
["Romance TV", "10920"],
"RTL Passion",
"Heimatkanal",

["TNT Comedy HD", "11875"],
["E!", "11875"],
["Jukebox", "11170"],
["Goldstar TV", "11758"],
["Classica", "11719"],

["Sky 3D", "11332"],
["Sky Select HD", "11875"],
["NatGeo HD", "11992"],
["Nat Geo Wild HD", "11914"],
["Discovery HD", "11914"],



["Sky Sport News HD", "12304"],
"Sky Sport Bundesliga 1 HD" + EXACT,
"Sky Sport Bundesliga 2 HD" + EXACT,
"Sky Sport Bundesliga 3 HD" + EXACT,
"Sky Sport Bundesliga 4 HD" + EXACT,
"Sky Sport Bundesliga 5 HD" + EXACT,
"Sky Sport Bundesliga 6 HD" + EXACT,
"Sky Sport Bundesliga 7 HD" + EXACT,
"Sky Sport Bundesliga 8 HD" + EXACT,
"Sky Sport Bundesliga 9 HD" + EXACT,
"Sky Sport Bundesliga 10 HD" + EXACT,
"Sky Sport Bundesliga 11 HD" + EXACT,
"Sky Sport Bundesliga UHD" + EXACT,
["Eurosport 1 HD", "12382"],
["Eurosport 2 HD", "11170"],
["Sky Sport 1 HD" + EXACT, "11914"],
["Sky Sport 2 HD" + EXACT, "11992"],
["Sky Sport 3 HD" + EXACT, "11914"],
["Sky Sport 4 HD" + EXACT, "11992"],
["Sky Sport 5 HD" + EXACT, "12304"],
["Sky Sport 6 HD" + EXACT, "12382"],
"Sky Sport 7 HD" + EXACT,
"Sky Sport 8 HD" + EXACT,
"Sky Sport 9 HD" + EXACT,
"Sky Sport 10 HD" + EXACT,
"Sky Sport 11 HD" + EXACT,
"Sky Sport UHD" + EXACT,
["sportdigital HD", "12721"],

["Beate-Uhse.TV", "11719"],

# Spanisch
["COMEDYCENTRALHD", "11258"]

]

#tag_name = 'Deutsche Programme'
#tag_uuid = ''

for channelnr, channel in enumerate(channels, start=1):
if isinstance(channel, list) and len(channel) == 2:
channel_lookup, service_lookup = channel
found = htspapi.search_channelsnames_bygrid(lookup=channel_lookup, services=service_lookup)
else:
found = htspapi.search_channelsnames_bygrid(lookup=channel)

if len(found) == 1:
found = found[0]
elif len(found) > 1:
print "Found multiple channels", found
found = None
else:
print "None found for", channel
found = None

if found:
uuid = found.get('uuid')
print "Found uuid for channel:", uuid, channel
resp = htspapi.update_channels(uuids=[uuid], data={'number': channelnr})
if 'error' in resp.keys():
# Why i'm getting errors here?

This comment has been minimized.

Copy link
@ckarrie

ckarrie Nov 27, 2016

Author Contributor

@perexg can you take a look why TVH returns an error here?

Found uuid for channel: 0c990db3e4f6f9d5bb63724d92bf684e Sky Sport Bundesliga 7 HD$
 -  {'node': [{'uuid': '0c990db3e4f6f9d5bb63724d92bf684e', 'number': 98}]}
Found uuid for channel: 7ec311061b8859048683a8d771c084b4 Sky Sport Bundesliga 8 HD$
 -  {'node': [{'uuid': '7ec311061b8859048683a8d771c084b4', 'number': 99}]}
  - Error: Fehlerhafte Anfrage
Found uuid for channel: 541cbc003991153232f2a7c77db6892f Sky Sport Bundesliga 9 HD$
 -  {'node': [{'uuid': '541cbc003991153232f2a7c77db6892f', 'number': 100}]}
  - Error: Fehlerhafte Anfrage
Found uuid for channel: 8429a81582eba9cbd96435669e433dac Sky Sport Bundesliga 10 HD$
 -  {'node': [{'uuid': '8429a81582eba9cbd96435669e433dac', 'number': 101}]}
  - Error: Fehlerhafte Anfrage

This comment has been minimized.

Copy link
@perexg

perexg Nov 30, 2016

Which HTTP error code is exactly returned for the query?

This comment has been minimized.

Copy link
@ckarrie

ckarrie Dec 3, 2016

Author Contributor

Enabled Debug in python HTSP Client:

Not working:

2016-12-03 19:42:34 DEBUG : htsp tx:
2016-12-03 19:42:34 DEBUG : { 'args': { 'node': [ { 'number': 78,
                        'tags': [ 'fd2a61026e652c77e13ba5c194bad5e5',
                                  'ea5b5ccf30be5293af5f1bf26e1637c7',
                                  '8192e01bf18c5f47c287914e5300bd3b',
                                  '0542b683a5cf3a942d59847403b8f507',
                                  'd56bc2cfb49884d25de7aeefed9ced98'],
                        'uuid': '1279011f5e37f633bf5e03e9380b9261'}]},
  'digest': SECRET,
  'method': 'api',
  'path': 'idnode/save',
  'username': 'ckarrie'}
2016-12-03 19:42:34 DEBUG : htsp rx:
2016-12-03 19:42:34 DEBUG : { 'error': 'Fehlerhafte Anfrage'}

Working:

2016-12-03 19:42:34 DEBUG : htsp tx:
2016-12-03 19:42:34 DEBUG : { 'args': { 'node': [ { 'number': 80,
                        'tags': [ 'fd2a61026e652c77e13ba5c194bad5e5',
                                  'ea5b5ccf30be5293af5f1bf26e1637c7',
                                  '8192e01bf18c5f47c287914e5300bd3b',
                                  '0542b683a5cf3a942d59847403b8f507',
                                  'd56bc2cfb49884d25de7aeefed9ced98'],
                        'uuid': 'cf7cf7ce9ee175b78139fefac969d38d'}]},
  'digest': SECRET,
  'method': 'api',
  'path': 'idnode/save',
  'username': 'ckarrie'}
2016-12-03 19:42:34 DEBUG : htsp rx:
2016-12-03 19:42:34 DEBUG : { }

This comment has been minimized.

Copy link
@ckarrie

ckarrie Dec 5, 2016

Author Contributor

Thank you very much @perexg, it's fixed!

This comment has been minimized.

Copy link
@perexg

perexg Dec 5, 2016

I think that empty reply is correct for api idnode/save - the error code or response member is set only when an error occurs...

print " - Error:", resp.get('error')
112 changes: 7 additions & 105 deletions scripts/merge_channels.py
@@ -1,4 +1,5 @@
from tvh.htsp import HTSPClient
from tvh.api import HTSPApi
from scripts import CONFIG

htsp = HTSPClient((CONFIG['hostname'], 9982))
Expand All @@ -9,110 +10,11 @@
cap = msg['servercapability']


def search_channelnames(lookup):
"""
Fetch all channels and search for matching channelnames, downsides:
- fetchs all channels
- not case insensitive
"""
htsp.send('api', {'path': 'channel/list'})
msg = htsp.recv()
found = []
for re in msg['response']['entries']:
if lookup in re['val']:
found.append(re)

return found


def search_channelsnames_bygrid(lookup=''):
"""
Fetch the channel grid with filter parameters
"""
htsp.send('api', {'path': 'channel/grid', 'args': {
'start': 0,
'limit': 999999,
'sort': 'name',
'dir': 'ASC',
'filter': [
{
"type": "string",
"value": lookup,
"field": "name"
}
],
'all': 1
}})
msg = htsp.recv()
return msg['response']['entries']


def get_serviceuuids_from_channeluuid(uuid):
htsp.send('api', {'path': 'idnode/load', 'args': {'uuid': uuid, 'meta': 1}})
msg = htsp.recv()
found_services = []
for re in msg['response']['entries']:
params = re['params']
for paramdict in params:
if 'services' in paramdict.values():
found_services.extend(paramdict['value'])
return found_services


def create_channel(name, services=[], tags=[], epg_parent="", enabled=True, number=0):
htsp.send('api', {
'path': 'channel/create',
'args': {
'conf': {
'name': name,
'enabled': 'true',
'number': number,
'services': services,
'tags': tags,
# 'bouquet': '',
}
}

})
return htsp.recv()


def update_channel(uuid, enabled='false', ):
args = {
'node': [
{'uuid': uuid, 'enabled': enabled}
]
}
htsp.send('api', {
'path': 'idnode/save',
'args': args
})
return htsp.recv()


def get_idnode_value(uuid):
htsp.send('api', {'path': 'idnode/load', 'args': {'uuid': uuid, 'meta': 0}})
msg = htsp.recv()
return msg['response']['entries'][0]["text"]


def update_channels(uuids, enabled=True):
nodes = []
for uuid in uuids:
nodes.append({'uuid': uuid, 'enabled': enabled})
args = {
'node': nodes
}
htsp.send('api', {
'path': 'idnode/save',
'args': args
})
print "update_channels returned", htsp.recv()

htspapi = HTSPApi(htsp=htsp)

search_channelname = raw_input("Search channel name: ")
# found_channels = search_channelnames(search_channelname)
found_channels_grid = search_channelsnames_bygrid(search_channelname)
found_channels_grid = htspapi.search_channelsnames_bygrid(search_channelname)
used_channels = []
# for c in found_channels:
# channeluuid = c['key']
Expand All @@ -124,14 +26,14 @@ def update_channels(uuids, enabled=True):
found_services = []

for chan in used_channels:
services = get_serviceuuids_from_channeluuid(chan)
services = htspapi.get_serviceuuids_from_channeluuid(chan)
found_services.extend(services)

for chan in found_channels_grid:
service_names = []
service_uuids = chan['services']
for suuid in service_uuids:
service_names.append(get_idnode_value(uuid=suuid))
service_names.append(htspapi.get_idnode_value(uuid=suuid))
service_name = "; ".join(service_names)
chan['service_name'] = service_name
use = raw_input('- Use channel "%(name)s" @ %(service_name)s (%(number)d)? [yN]: ' % chan)
Expand All @@ -147,11 +49,11 @@ def update_channels(uuids, enabled=True):
merged_channel_nr = raw_input("Number of new channel: ")
number = int(merged_channel_nr)

create_channel(merged_channel_name, services=found_services, number=number)
htspapi.create_channel(merged_channel_name, services=found_services, number=number)

disable_existing_channels = raw_input("Disable existing %d channels [yN]: " % len(found_services))
if disable_existing_channels == "y":
update_channels(uuids=used_channels, enabled=False)
htspapi.enable_channels(uuids=used_channels, enabled=False)
# for c in used_channels:
# update_channel(uuid=c, enabled='false')

Expand Down

0 comments on commit 9dc0749

Please sign in to comment.