Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2011
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.01 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. import os
  9.  
  10. tivo_nicenames={'1234':'Tivo Bedroom', 'ABCD':'Tivo Lounge'}
  11.  
  12. def find_tivos_zc(server):
  13.     """ Find TiVos on the LAN using Zeroconf. This is simpler and
  14.        cleaner than the fake HTTP method, but slightly slower, and
  15.        requires the Zeroconf module. (It's still much faster than
  16.        waiting for beacons.)
  17.  
  18.    """
  19.     global tivo_swversions
  20.  
  21.     class ZCListener:
  22.         def __init__(self, names):
  23.             self.names = names
  24.  
  25.         def removeService(self, server, type, name):
  26.             self.names.remove(name)
  27.  
  28.         def addService(self, server, type, name):
  29.             self.names.append(name)
  30.  
  31.     service_name = '_tivo-device._tcp.local.'
  32.  
  33.     tivos = []
  34.     tivo_names = []
  35.  
  36.     # Get the names of TiVos offering network remote control
  37.     try:
  38.         browser = Zeroconf.ServiceBrowser(server, 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 = server.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. print '-----------------------'
  164. print 'Searching for tivos ...'
  165. print '-----------------------'
  166. print
  167.  
  168. server = Zeroconf.Zeroconf()
  169.  
  170. tivos = find_tivos_zc(server);
  171.  
  172. if tivos:
  173.     for tivo in tivos:
  174.         tivo_name = ""
  175.         if tivo_nicenames.has_key(tivo['service_name']):
  176.             tivo_name = tivo_nicenames[tivo['service_name']];
  177.         else:
  178.             tivo_name = "VM "+tivo['service_name'];
  179.        
  180.         print("Registering tivo "+tivo['service_name']+" as "+tivo_name+"...")
  181.         registerTivo(server, socket.inet_aton(tivo['ip_address']), tivo['tsn'], tivo_name);
  182.    
  183.     print
  184.     print '-----------------------'
  185.     print 'Running mDNS daemon ...'
  186.     print '-----------------------'
  187. else:
  188.     print 'No tivos found!'
  189.     os._exit(0)
  190.  
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement