Advertisement
Guest User

VM1

a guest
Dec 3rd, 2011
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """An mDNS daemon designed to appear, to the iPad app, to be a TiVo Premiere."""
  4. import sys
  5. import Zeroconf
  6. import socket
  7. import time
  8.  
  9. tivo_nicenames={'1234':'Tivo Bedroom', 'ABCD':'Tivo Lounge'}
  10.  
  11. def find_tivos_zc():
  12.     """ Find TiVos on the LAN using Zeroconf. This is simpler and
  13.        cleaner than the fake HTTP method, but slightly slower, and
  14.        requires the Zeroconf module. (It's still much faster than
  15.        waiting for beacons.)
  16.  
  17.    """
  18.     global tivo_swversions
  19.  
  20.     class ZCListener:
  21.         def __init__(self, names):
  22.             self.names = names
  23.  
  24.         def removeService(self, server, type, name):
  25.             self.names.remove(name)
  26.  
  27.         def addService(self, server, type, name):
  28.             self.names.append(name)
  29.  
  30.     service_name = '_tivo-device._tcp.local.'
  31.  
  32.     tivos = []
  33.     tivo_names = []
  34.  
  35.     # Get the names of TiVos offering network remote control
  36.     try:
  37.         serv = Zeroconf.Zeroconf()
  38.         browser = Zeroconf.ServiceBrowser(serv, service_name, ZCListener(tivo_names))
  39.     except:
  40.         return tivos
  41.  
  42.     # Give them half a second to respond
  43.     time.sleep(0.5)
  44.  
  45.     # Now get the addresses -- this is the slow part
  46.  
  47.     for t in tivo_names:
  48.  
  49.         s = serv.getServiceInfo(service_name, t)
  50.  
  51.         if s:
  52.             new_tivo = {}
  53.  
  54.             name = t.replace('.' + service_name, '')
  55.             address = socket.inet_ntoa(s.getAddress())
  56.  
  57.             new_tivo['service_name'] = name;
  58.             new_tivo['ip_address'] = address;
  59.             new_tivo['tsn'] = s.getProperties()['TSN'];
  60.  
  61.             tivos.append(new_tivo);
  62.  
  63.     serv.close()
  64.     return tivos
  65.  
  66.  
  67. def registerTivo( server, local_ip, TSN, tivo_name ):
  68.     # You need the right value here.  Either use tcpdump to find the value that
  69.     # your TiVo device advertises, or navigate to:
  70.     #  Account & System Info
  71.     #  System Information
  72.     #  TiVo Service Number
  73.     # It should be 15 hex digits (0-9 and a-f) with no dashes.  All in uppercase
  74.     # to be safe.
  75.  
  76.  
  77.     #IP address of your PC/laptop
  78.  
  79.     server.registerService(
  80.         Zeroconf.ServiceInfo(
  81.             '_tivo-device._tcp.local.',
  82.             tivo_name+'._tivo-device._tcp.local.',
  83.             address = local_ip,
  84.             port = 80,
  85.             weight = 0, priority=0,
  86.             properties = {
  87.                 'path': '/',
  88.                 'services': '_tivo-mindrpc._tcp,_tivo-remote._tcp',
  89.                 'platformname': 'TiVo Premiere',
  90.                 'swversion': '14.8.U2-01-3.746',
  91.                 'platform': 'tcd/Series4',
  92.                 'TSN': TSN,
  93.                 }
  94.             )
  95.         )
  96.  
  97.     server.registerService(
  98.         Zeroconf.ServiceInfo(
  99.             '_http._tcp.local.',
  100.             tivo_name+'._http._tcp.local.',
  101.             address = local_ip,
  102.             port = 80,
  103.             weight = 0, priority=0,
  104.             properties = {
  105.                 'path': '/index.html',
  106.                 'swversion': '14.8.U2-01-3.746',
  107.                 'platform': 'tcd/Series4',
  108.                 'TSN': TSN,
  109.                 }
  110.             )
  111.         )
  112.  
  113.     server.registerService(
  114.         Zeroconf.ServiceInfo(
  115.             '_tivo-mindrpc._tcp.local.',
  116.             tivo_name+'._tivo-mindrpc._tcp.local.',
  117.             address = local_ip,
  118.             port = 1413,
  119.             weight = 0, priority=0,
  120.             properties = {
  121.                 'protocol': 'tivo-mindrpc',
  122.                 'path': '/',
  123.                 'swversion': '14.8.U2-01-3.746',
  124.                 'platform': 'tcd/Series4',
  125.                 'TSN': TSN,
  126.                 }
  127.             )
  128.         )
  129.  
  130.     server.registerService(
  131.         Zeroconf.ServiceInfo(
  132.             '_tivo-videos._tcp.local.',
  133.             tivo_name+'._tivo-videos._tcp.local.',
  134.             address = local_ip,
  135.             port = 443,
  136.             weight = 0, priority=0,
  137.             properties = {
  138.                 'protocol': 'https',
  139.                 'path': '/TiVoConnect?Command=QueryContainer&Container=%2FNowPlaying',
  140.                 'swversion': '14.8.U2-01-3.746',
  141.                 'platform': 'tcd/Series4',
  142.                 'TSN': TSN,
  143.                 }
  144.             )
  145.         )
  146.  
  147.     server.registerService(
  148.         Zeroconf.ServiceInfo(
  149.                 '_tivo-remote._tcp.local.',
  150.                 tivo_name+'._tivo-remote._tcp.local.',
  151.                 address = local_ip,
  152.                 port = 1393,
  153.                 weight = 0, priority=0,
  154.                 properties = {
  155.                 'platformname': 'TiVo Premiere',
  156.                 'swversion': '14.8.U2-01-3.746',
  157.                 'platform': 'tcd/Series4',
  158.                 'TSN': TSN,
  159.                 }
  160.             )
  161.         )
  162.  
  163. tivos = find_tivos_zc();
  164.  
  165. server = Zeroconf.Zeroconf()
  166.  
  167. for tivo in tivos:
  168.     tivo_name = ""
  169.     if tivo_nicenames.has_key(tivo['service_name']):
  170.         tivo_name = tivo_nicenames[tivo['service_name']];
  171.     else:
  172.         tivo_name = "VM "+tivo['service_name'];
  173.        
  174.     print("Registering tivo "+tivo['service_name']+" as "+tivo_name+"...")
  175.     registerTivo(server, socket.inet_aton(tivo['ip_address']), tivo['tsn'], tivo_name);
  176.  
  177.        
  178. print 'Running mDNS daemon ...'
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement