Ticket #6872: mythhdhrimport.py

File mythhdhrimport.py, 5.1 KB (added by Raymond Wagner <raymond@…>, 16 years ago)
Line 
1#!/usr/bin/python
2
3from subprocess import Popen, PIPE
4from MythTV import MythDB
5import re, os, sys, getopt
6
7SOURCEID=4
8
9def process(buffer):
10 lines = buffer.split('\n')
11 m = re.match('SCANNING: (?P<freq>[0-9]*) \((?P<chanstring>.*)\)', lines[0])
12 if m is None:
13 return
14 freq = m.group('freq')
15 chans = m.group('chanstring').split(', ')
16 m = re.match('(?P<table>[a-z\-]*):(?P<channel>[0-9]*)',chans[0])
17 table = m.group('table')
18 if table == 'qam256':
19 table = 'qam_256'
20 chan = m.group('channel')
21 m = re.match('LOCK: (?P<lock>[a-z0-9]*) \(.*\)',lines[1])
22 lock = m.group('lock')
23
24 print "scanning %s:%s (%s) - %s" % (table,chan,freq,lock)
25 if lock == 'none':
26 return
27 if len(lines) == 2:
28 print " no channels found"
29 return
30 for i in range(2,len(lines)):
31# print lines[i]
32 m = re.match('PROGRAM (?P<pid>[0-9]*).*\((?P<type>.*)\)',lines[i])
33 if m:
34 print " %s.%s - %s" % (chan,m.group('pid'),m.group('type'))
35 continue
36 m = re.match('PROGRAM (?P<pid>[0-9]*): (?P<vc>[0-9\.]*)( (?P<callsign>.*))?',lines[i])
37 if m is None:
38 continue
39 pid = m.group('pid')
40 virt = m.group('vc')
41 if virt == '0':
42 virt = '%s.%s' % (chan,pid)
43 call = m.group('callsign')
44 if call == None:
45 call = ''
46 print " %s.%s - %s (%s)" % (chan,pid,virt,call)
47 channels.append((freq,table,chan,pid,virt,call))
48
49
50SOURCE = None
51HDHR = None
52TUNER = None
53mythdb = MythDB()
54cursor = mythdb.cursor()
55
56opts, args = getopt.getopt(sys.argv[1:], 'hd:s:t:', ['help','device=','source=','tuner='])
57for o,a in opts:
58 if o in ('-h','--help'):
59 print "%s [-h|--help] [-d<HDHR ID>|--device=<HDHR ID>] [-s<SOURCE ID>|--source=<SOURCE ID>] [-t<0|1>|--tuner=<0|1>]" % sys.argv[0]
60 sys.exit(0)
61 elif o in ('-d','--device'):
62 HDHR = a
63 elif o in ('-s','--source'):
64 SOURCE = int(a)
65 elif o in ('-t','--tuner'):
66 TUNER = int(a)
67
68while SOURCE is None:
69 validsrc = []
70 print "Please choose a channel source:"
71 for i in range(0,cursor.execute("""SELECT sourceid,name FROM videosource;""")):
72 src = cursor.fetchone()
73 validsrc.append(src[0])
74 print " %s. %s" % src
75 SOURCE = int(raw_input('> '))
76 if SOURCE not in validsrc:
77 SOURCE = None
78
79while HDHR is None:
80 p = Popen('hdhomerun_config discover',shell=True,stdout=PIPE).stdout
81 devs = []
82 for i in p.read().split('\n'):
83 m = re.match('hdhomerun device (?P<dev>[0-9A-F]{8}) found at (?P<ip>[0-9\.]*)',i)
84 if not m:
85 continue
86 devs.append((m.group('dev'),m.group('ip')))
87 if len(devs) == 0:
88 print "No devices found"
89 sys.exit(-2)
90 elif len(devs) == 1:
91 print "Using device %s found at %s" % devs[0]
92 HDHR = devs[0][0]
93 else:
94 print "Please choose a HDHomerun device:"
95 count = 1
96 for dev,ip in devs:
97 print " %d. %s at %s" % (count,dev,ip)
98 res = int(raw_input('> '))
99 if (res > 0) & (res <= len(devs)):
100 HDHR = devs[res-1]
101
102while TUNER is None:
103 res = raw_input("Please choose tuner 0 or 1: ")
104 if res in ('0','1'):
105 TUNER = int(res)
106
107p = Popen('hdhomerun_config %s scan %d' % (HDHR,TUNER),shell=True,stdout=PIPE).stdout
108buf = ''
109channels = []
110newchans = []
111while True:
112 if buf.count('SCANNING') > 1:
113 process(buf[:buf.find('SCANNING',1)].strip())
114 buf = buf[buf.find('SCANNING',1):]
115 continue
116 size = len(buf)
117 buf += p.read(100)
118 if len(buf) == size:
119 process(buf.strip())
120 break
121
122mythdb = MythDB()
123cursor = mythdb.cursor()
124chanid = SOURCE*1000+1
125for chan in channels:
126 # check for matching multiplex. if not found, create one
127 if not cursor.execute("""SELECT mplexid FROM dtv_multiplex WHERE frequency=%s""" % chan[0]):
128 print "Multiplex not found, creating new for %s (%s)" % (chan[2],chan[0])
129 cursor.execute("""INSERT INTO dtv_multiplex SET sourceid=%d,frequency=%s,constellation='%s',sistandard='',polarity='v'""" % (SOURCE, chan[0],chan[1]))
130 cursor.execute("""SELECT mplexid FROM dtv_multiplex WHERE frequency=%s""" % chan[0])
131 mplex = cursor.fetchone()[0]
132
133 # check for existing channel at multiplex and serviceid
134 if cursor.execute("""SELECT chanid FROM channel WHERE mplexid=%d AND serviceid=%s""" % (mplex,chan[3])):
135 res = cursor.fetchone()
136 print "Channel %s:%s exists at chanid %d" % (chan[2],chan[3],res[0])
137 continue
138
139 # check for channel with matching callsign
140 if len(chan[5]):
141 if cursor.execute("""SELECT chanid,freqid,serviceid FROM channel WHERE callsign='%s'""" % chan[5]):
142 res = cursor.fetchone()
143 print "Channel %d moved from %s:%d to %s:%s" % (res[0],res[1],res[2],chan[2],chan[3])
144 while True:
145 res = raw_input(' Update? (yes/no)')
146 if res in ('yes','y'):
147 cursor.execute("""UPDATE channel SET freqid=%s,mplexid=%s,serviceid=%s WHERE chanid=%d""" % (chan[2],mplex,chan[3],res[0]))
148 elif res in ('no','n'):
149 break
150 else:
151 continue
152 continue
153
154 # create a new channel,
155 print "New channel - %s:%s %s" % (chan[2],chan[3],chan[5])
156 while True:
157 if cursor.execute("""SELECT chanid FROM channel WHERE chanid=%d""" % chanid):
158 chanid += 1
159 continue
160 cursor.execute("""INSERT INTO channel SET chanid=%s,channum='%s',freqid=%s,sourceid=%d,callsign='%s',name='%s',mplexid=%s,serviceid=%s,last_record=0""" % (chanid,chan[4],chan[2],SOURCE,chan[5],chan[5],mplex,chan[3]))
161 print "Channel %s.%s inserted as %s" % (chan[2],chan[3],chanid)
162 break
163