Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import struct
- import binascii
- import sys,os
- import re
- import datetime
- HOST = '192.168.1.101'
- PORT = 4510
- CMD2_EPG_SRV_ENUM_RESERVE = 1011
- CMD2_EPG_SRV_GET_RESERVE = 1012
- CMD2_EPG_SRV_ADD_RESERVE = 1013
- CMD2_EPG_SRV_CHG_RESERVE = 1015
- CMD2_EPG_SRV_ENUM_SERVICE = 1021
- CMD2_EPG_SRV_GET_PG_INFO = 1023
- CMD2_EPG_SRV_SUSPEND = 1051
- CMD_SUCCESS = 1
- RECMODE_ALL = 0
- RECMODE_SERVICE = 1
- RECMODE_NO = 5
- RESERVE_EXECUTE = 0
- RESERVE_PILED_UP = 1
- RESERVE_NO_EXECUTE = 2
- def readstr(buf, offset):
- ll, = struct.unpack('<I', buf[offset:offset+4])
- return (ll, buf[offset+4:offset+ll].decode('utf16', 'ignore')[:-1]) # remove NULL char
- def writestr(st):
- buf = (st+'\0').encode('utf16', 'ignore')[2:] # remove BOM
- return struct.pack('<I', len(buf)+4) + buf
- def readservicedat(buf, offset):
- co = offset
- bsize, = struct.unpack('<I', buf[co:co+4])
- co += 4
- netID,tsID,srvID = struct.unpack('<HHH', buf[co:co+6])
- co += 6
- srvtype,partflg = struct.unpack('<BB', buf[co:co+2])
- co += 2
- lsrvproname,srvproname = readstr(buf, co)
- co += lsrvproname
- lsrvname,srvname = readstr(buf, co)
- co += lsrvname
- lnetname,netname = readstr(buf, co)
- co += lnetname
- ltsname,tsname = readstr(buf, co)
- co += ltsname
- remoID, = struct.unpack('<B', buf[co:co+1])
- co += 1
- return (bsize, (netID,tsID,srvID, srvproname, srvname, netname, tsname,
- remoID, srvtype,partflg))
- def readreservedat(buf, offset):
- co = offset
- bsize, = struct.unpack('<I', buf[co:co+4])
- co += 4
- ltitle,title = readstr(buf, co)
- co += ltitle
- styear,stmonth,stdow,stday,sthour,stmin,stsec,stmsec = struct.unpack('<8H', buf[co:co+16])
- co += 16
- stdate = datetime.datetime(styear, stmonth, stday, sthour, stmin, stsec, stmsec)
- duration, = struct.unpack('<I', buf[co:co+4])
- co += 4
- lservice,service = readstr(buf, co)
- co += lservice
- netID,tsID,srvID,evID = struct.unpack('<HHHH', buf[co:co+8])
- co += 8
- lcomm,comm = readstr(buf, co)
- co += lcomm
- resrvID,waitFlg,ovlpMode = struct.unpack('<IBB', buf[co:co+6])
- co += 6
- lfilepath,filepath = readstr(buf, co)
- co += lfilepath
- # skip start time
- co += 16
- ssize, = struct.unpack('<I', buf[co:co+4])
- co += 4
- recmode,prio,tuiflg,srvmode = struct.unpack('<BBBI', buf[co:co+7])
- return (bsize, (resrvID, service, title,
- stdate,
- duration, netID,tsID,srvID,evID, waitFlg,ovlpMode,
- (recmode, prio, tuiflg, srvmode)) )
- def readepgevdat(buf, offset):
- co = offset
- bsize, = struct.unpack('<I', buf[co:co+4])
- co += 4
- netID,tsID,srvID,evID,stflg = struct.unpack('<HHHHB', buf[co:co+9])
- co += 9
- styear,stmonth,stdow,stday,sthour,stmin,stsec,stmsec = struct.unpack('<8H', buf[co:co+16])
- co += 16
- drflg,duration = struct.unpack('<BI', buf[co:co+5])
- co += 5
- shsize, = struct.unpack('<I', buf[co:co+4])
- levname,evname = readstr(buf, co+4)
- ltxchar,txchar = readstr(buf, co+4+levname)
- #lsrchev,srchev = readstr(buf, co+4+levname+ltxchar)
- #lsrchtx,srchtx = readstr(buf, co+4+levname+ltxchar+lsrchev)
- co += shsize
- exsize, = struct.unpack('<I', buf[co:co+4])
- #lexchar,exchar = readstr(buf, co+4)
- #lexsrch,exsrch = readstr(buf, co+4+lexchar)
- co += exsize
- return (bsize, (netID,tsID,srvID,evID, evname, txchar))
- def writedefsetting():
- buf = ''
- buf += struct.pack('<BBBIB', 1, 2, 1, 0, 0) # recMode -> pittariFlag
- buf += writestr('') # batFilePath
- buf += struct.pack('<II', 8, 0) # recFolderList (empty list)
- buf += struct.pack('<BBBIIBBI', 0, 0, 0, 0, 0, 0, 0, 0) # suspendMode -> tunerID
- return struct.pack('<I', len(buf)+4) + buf
- def writereserve(netID, tsID, srvID, evID, stdate, title, service, duration, comment):
- buf = ''
- buf += writestr(title)
- buf += struct.pack('<8H', stdate.year, stdate.month, stdate.isoweekday()%7,
- stdate.day, stdate.hour, stdate.minute,
- stdate.second, stdate.microsecond/1000) # startTime
- buf += struct.pack('<I', duration)
- buf += writestr(service)
- buf += struct.pack('<HHHH', netID, tsID, srvID, evID)
- buf += writestr(comment)
- buf += struct.pack('<IBB', 0, 0, 0) # reserveID -> overlapMode
- buf += writestr('') # recFilePath
- buf += struct.pack('<8H', 0, 0, 0, 0, 0, 0, 0, 0) # startTimeEpg
- buf += writedefsetting()
- buf += struct.pack('<I', 0) # reserveStatus
- return struct.pack('<I', len(buf)+4) + buf
- def sendcmd(sock, upcmd):
- sock.connect((HOST, PORT))
- sock.send(upcmd)
- downcmd = sock.recv(8)
- res,downsize = struct.unpack('<II', downcmd)
- if res != CMD_SUCCESS:
- return (res, None)
- downdat = ''
- while len(downdat) < downsize:
- downdat += sock.recv(downsize - len(downdat))
- return (res, downdat)
- def enumreserves():
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- upcmd = struct.pack('<II', CMD2_EPG_SRV_ENUM_RESERVE, 0)
- res,downdat = sendcmd(sock, upcmd)
- lsize,llen = struct.unpack('<II', downdat[:8])
- ret = []
- offset = 8
- for i in range(llen):
- bsize,dat = readreservedat(downdat, offset)
- ret.append(dat)
- offset += bsize
- sock.close()
- ret.sort(lambda r1,r2:cmp(r1[3],r2[3])) # stdate order
- return ret
- def getreserve(id):
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- upcmd = struct.pack('<III', CMD2_EPG_SRV_GET_RESERVE, 4, id)
- res,downdat = sendcmd(sock, upcmd)
- resrv = readreservedat(downdat, 0)
- sock.close()
- return resrv
- def getproginfo(netID, tsID, srvID, evID):
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- upcmd = struct.pack('<IIHHHH', CMD2_EPG_SRV_GET_PG_INFO, 8,
- evID, srvID, tsID, netID)
- res,downdat = sendcmd(sock, upcmd)
- epgev = readepgevdat(downdat, 0)
- sock.close()
- return epgev
- def updateredmode(resrvID, recmode):
- sock1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- upcmd1 = struct.pack('<III', CMD2_EPG_SRV_GET_RESERVE, 4, resrvID)
- res1,buf = sendcmd(sock1, upcmd1)
- sock1.close()
- size, = struct.unpack('<I', buf[0:4])
- offset = 4
- offset += struct.unpack('<I', buf[offset:offset+4])[0] # title
- offset += 20
- offset += struct.unpack('<I', buf[offset:offset+4])[0] # service
- offset += 8
- offset += struct.unpack('<I', buf[offset:offset+4])[0] # comment
- offset += 6
- offset += struct.unpack('<I', buf[offset:offset+4])[0] # filepath
- offset += 16
- # start REC_SETTING_DATA
- offset += 4
- org = struct.unpack('<B', buf[offset:offset+1])
- lbuf = list(buf)
- lbuf[offset:offset+1] = struct.pack('<B', recmode)
- buf = ''.join(lbuf)
- sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- # append header of list
- size2 = len(buf)+8
- upcmd2 = struct.pack('<IIII', CMD2_EPG_SRV_CHG_RESERVE, size2, size2, 1)
- upcmd2 += buf
- res2,_buf = sendcmd(sock2, upcmd2)
- sock2.close()
- return res2
- def addreserve(netID, tsID, srvID, evID, stdate, title, service, duration, comment):
- buf = writereserve(netID, tsID, srvID, evID, stdate, title, service, duration, comment)
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- # append header of list
- size = len(buf)+8
- upcmd = struct.pack('<IIII', CMD2_EPG_SRV_ADD_RESERVE, size, size, 1)
- upcmd += buf
- res,_buf = sendcmd(sock, upcmd)
- sock.close()
- return res
- def enumservices():
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- upcmd = struct.pack('<II', CMD2_EPG_SRV_ENUM_SERVICE, 0)
- res,downdat = sendcmd(sock, upcmd)
- lsize,llen = struct.unpack('<II', downdat[:8])
- ret = []
- offset = 8
- for i in range(llen):
- bsize,dat = readservicedat(downdat, offset)
- ret.append(dat)
- offset += bsize
- sock.close()
- return ret
- def loadtvpid(filename):
- fin = file(filename)
- meta = []
- for l in fin:
- if len(l.strip()) == 0:
- break
- meta.append(l.decode('cp932').strip())
- body = ''.join(fin).decode('cp932').strip()
- sep = re.compile(': *')
- header = dict(map(lambda m:sep.split(m, 1), meta))
- alpha = re.compile('[A-Z]+')
- typemap = {'DFS': (0x7880, 0x7FE8), 'BSDT': (4, 4), 'CSDT': (6, 7)}
- station = header['station']
- m = alpha.match(station)
- if not m is None and m.group(0) in typemap:
- typebnd = typemap[m.group(0)]
- srvID_ = int(station[m.end():])
- li = enumservices()
- srv = filter(lambda (netID,tsID,srvID, srvproname, srvname, netname, tsname,
- remoID, srvtype,partflg): \
- typebnd[0] <= netID and netID <= typebnd[1] and srvID_ == srvID,
- li)
- if len(srv) > 0:
- (netID,tsID,srvID, srvproname, srvname, netname, tsname,
- remoID, srvtype,partflg) = srv[0]
- evID = int(header['program-id'])
- year = int(header['year'])
- month = int(header['month'])
- day = int(header['date'])
- sh,sm = map(lambda i:int(i), header['start'].split(':'))
- eh,em = map(lambda i:int(i), header['end'].split(':'))
- stdate = datetime.datetime(year, month, day, sh, sm, 0, 0)
- endate = datetime.datetime(year, month, day, eh, em, 0, 0)
- if endate < stdate:
- endate = datetime.datetime(year, month, day+1, eh, em, 0, 0)
- duration = (endate-stdate).seconds
- return (netID, tsID, srvID, evID, stdate,
- header['program-title'], srvname, duration)
- return None
- def printreserves():
- li = enumreserves()
- if len(li):
- print " ID START LEN TITLE"
- for resrv in li:
- (resrvID, service, title,
- stdate,
- duration, netID,tsID,srvID,evID, waitFlg,ovlpMode,
- (recmode, prio, tuiflg, srvmode)) = resrv
- stat = ' '
- if recmode == RECMODE_NO:
- stat = 'x'
- elif ovlpMode != RESERVE_EXECUTE:
- stat = '!'
- print "%s %03d %s %01d:%02d:%02d %s (%s)" \
- % (stat, resrvID, stdate.strftime('%Y-%m-%d %H:%M'),
- duration/60/60, duration/60%60, duration%60, title, service)
- def sendsuspend():
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- upcmd = struct.pack('<III', CMD2_EPG_SRV_SUSPEND, 4, 1)
- res,downdat = sendcmd(sock, upcmd)
- sock.close()
- return res
- def printproginfo(resrvID):
- bsize,resrv = getreserve(resrvID)
- (resrvID, service, title,
- stdate,
- duration, netID,tsID,srvID,evID, waitFlg,ovlpMode,
- (recmode, prio, tuiflg, srvmode)) = resrv
- bsize,prog = getproginfo(netID, tsID, srvID, evID)
- (netID,tsID,srvID,evID, evname, txchar) = prog
- print "ID: %03d" % resrvID
- print "Start: %s" % stdate.strftime('%Y-%m-%d %H:%M')
- print "Duration: %01d:%02d:%02d" % (duration/60/60, duration/60%60, duration%60)
- print "Channel: %s (0x%04x 0x%04x 0x%04x)" % (service, netID, tsID, srvID)
- print "EV: 0x%04x" % (evID)
- print "Title: %s" % (title)
- print
- print txchar
- def main():
- if len(sys.argv) > 1 and sys.argv[1] == 'list':
- printreserves()
- elif len(sys.argv) > 2 and sys.argv[1] == 'enable':
- id = int(sys.argv[2])
- updateredmode(id, RECMODE_SERVICE)
- elif len(sys.argv) > 2 and sys.argv[1] == 'disable':
- id = int(sys.argv[2])
- updateredmode(id, RECMODE_NO)
- elif len(sys.argv) > 1 and sys.argv[1] == 'suspend':
- sendsuspend()
- elif len(sys.argv) > 2 and sys.argv[1] == 'info':
- printproginfo(int(sys.argv[2]))
- elif len(sys.argv) > 2 and sys.argv[1] == 'add':
- prg = loadtvpid(sys.argv[2])
- if not prg is None:
- (netID, tsID, srvID, evID, stdate, title, service, duration) = prg
- print "Start: %s" % stdate.strftime('%Y-%m-%d %H:%M')
- print "Duration: %01d:%02d:%02d" % (duration/60/60, duration/60%60, duration%60)
- print "Channel: %s (0x%04x 0x%04x 0x%04x)" % (service, netID, tsID, srvID)
- print "Title: %s" % (title)
- res = addreserve(netID, tsID, srvID, evID, stdate, title, service, duration, 'from PySample')
- if res != CMD_SUCCESS:
- print 'error: %d' % res
- else:
- print "Usage: python %s <command> <args...>" % sys.argv[0]
- print " Commands"
- print " list : list reservations"
- print " info <ID> : info of reservation of ID"
- print " enable <ID> : enable reservation of ID"
- print " disable <ID> : disable reservation of ID"
- print " suspend : suspend server"
- print " add <TVPID filepath> : add reservation by given TVPID file"
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement