11 import xml
.etree
.ElementTree
as ElementTree
14 import elementtree
.ElementTree
as ElementTree
16 warnings
.warn('Python 2.5 or higher or elementtree is ' +
17 'needed to use the TivoPush')
19 if 'ElementTree' not in locals():
22 def __init__(self
, *arg
, **karg
):
23 raise Exception('Python 2.5 or higher or elementtree is ' +
24 'needed to use the TivoPush')
29 def __init__(self
, username
, password
):
30 self
.__logger
= logging
.getLogger('pyTivo.mind')
31 self
.__username
= username
32 self
.__password
= password
34 cj
= cookielib
.CookieJar()
35 cp
= urllib2
.HTTPCookieProcessor(cj
)
36 self
.__opener
= urllib2
.build_opener(cp
)
40 if not self
.__pcBodySearch
():
41 self
.__pcBodyStore
('pyTivo', True)
43 def pushVideo(self
, tsn
, url
, description
, duration
, size
,
45 # It looks like tivo only supports one pc per house
46 pc_body_id
= self
.__pcBodySearch
()[0]
49 'bodyId': 'tsn:' + tsn
,
50 'description': description
,
52 'encodingType': 'mpeg2ProgramStream',
53 'partnerId': 'tivo:pt.3187',
54 'pcBodyId': pc_body_id
,
55 'publishDate': time
.strftime('%Y-%m-%d %H:%M%S', time
.gmtime()),
64 data
['subtitle'] = subtitle
66 offer_id
, content_id
= self
.__bodyOfferModify
(data
)
67 self
.__subscribe
(offer_id
, content_id
, tsn
)
69 def getDownloadRequests(self
):
85 # It looks like tivo only supports one pc per house
86 pc_body_id
= self
.__pcBodySearch
()[0]
89 offer_list
= self
.__bodyOfferSchedule
(pc_body_id
)
91 for offer
in offer_list
.findall('bodyOffer'):
93 if offer
.findtext('state') != 'scheduled':
96 for n
in NEEDED_VALUES
:
97 d
[n
] = offer
.findtext(n
)
102 def completeDownloadRequest(self
, request
):
103 request
['encodingType'] = 'mpeg2ProgramStream'
104 request
['state'] = 'complete'
105 request
['type'] = 'bodyOfferModify'
106 request
['updateDate'] = time
.strftime('%Y-%m-%d %H:%M%S',
109 offer_id
, content_id
= self
.__bodyOfferModify
(request
)
110 self
.__subscribe
(offer_id
, content_id
, request
['bodyId'][4:])
113 def getXMPPLoginInfo(self
):
114 # It looks like tivo only supports one pc per house
115 pc_body_id
= self
.__pcBodySearch
()[0]
117 xml
= self
.__bodyXmppInfoGet
(pc_body_id
)
120 results
['server'] = xml
.findtext('server')
121 results
['port'] = int(xml
.findtext('port'))
122 results
['username'] = xml
.findtext('xmppId')
124 for sendPresence
in xml
.findall('sendPresence'):
125 results
.setdefault('presence_list',[]).append(sendPresence
.text
)
132 'cams_security_domain': 'tivocom',
133 'cams_login_config': 'http',
134 'cams_cb_username': self
.__username
,
135 'cams_cb_password': self
.__password
,
136 'cams_original_url': '/mind/mind7?type=infoGet'
140 'https://mind.tivo.com:8181/mind/login',
141 urllib
.urlencode(data
)
144 result
= self
.__opener
.open(r
)
148 self
.__logger
.debug('__login\n%s' % (data
))
150 def __dict_request(self
, data
, req
):
152 'https://mind.tivo.com:8181/mind/mind7?type=' + req
,
154 {'Content-Type': 'x-tivo/dict-binary'}
156 result
= self
.__opener
.open(r
)
158 xml
= ElementTree
.parse(result
).find('.')
160 self
.__logger
.debug('%s\n%s\n\n%sg' % (req
, data
,
161 ElementTree
.tostring(xml
)))
164 def __bodyOfferModify(self
, data
):
165 """Create an offer"""
167 xml
= self
.__dict
_request
(data
, 'bodyOfferModify&bodyId=' +
170 if xml
.findtext('state') != 'complete':
171 raise Exception(ElementTree
.tostring(xml
))
173 offer_id
= xml
.findtext('offerId')
174 content_id
= offer_id
.replace('of','ct')
176 return offer_id
, content_id
179 def __subscribe(self
, offer_id
, content_id
, tsn
):
180 """Push the offer to the tivo"""
182 'bodyId': 'tsn:' + tsn
,
184 'contentId': content_id
,
186 'type': 'singleOfferSource'
188 'title': 'pcBodySubscription',
192 return self
.__dict
_request
(data
, 'subscribe&bodyId=tsn:' + tsn
)
194 def __bodyOfferSchedule(self
, pc_body_id
):
195 """Get pending stuff for this pc"""
197 data
= {'pcBodyId': pc_body_id
}
198 return self
.__dict
_request
(data
, 'bodyOfferSchedule')
200 def __pcBodySearch(self
):
203 xml
= self
.__dict
_request
({}, 'pcBodySearch')
205 return [id.text
for id in xml
.findall('pcBody/pcBodyId')]
207 def __collectionIdSearch(self
, url
):
208 """Find collection ids"""
210 xml
= self
.__dict
_request
({'url': url
}, 'collectionIdSearch')
211 return xml
.findtext('collectionId')
213 def __pcBodyStore(self
, name
, replace
=False):
218 'replaceExisting': str(replace
).lower()
221 return self
.__dict
_request
(data
, 'pcBodyStore')
223 def __bodyXmppInfoGet(self
, body_id
):
225 return self
.__dict
_request
({'bodyId': body_id
},
226 'bodyXmppInfoGet&bodyId=' + body_id
)
230 """Helper to create x-tivo/dict-binary"""
233 keys
= [str(k
) for k
in d
]
239 output
.append( varint( len(k
) ) )
242 if isinstance(v
, dict):
243 output
.append( chr(2) )
244 output
.append( dictcode(v
) )
247 v
= unicode(v
).encode('utf-8')
248 output
.append( chr(1) )
249 output
.append( varint( len(v
) ) )
252 output
.append( chr(0) )
254 output
.append( chr(0x80) )
256 return ''.join(output
)
261 output
.append( chr(i
& 0x7f) )
263 output
.append( chr(i |
0x80) )
264 return ''.join(output
)
267 username
= config
.getTivoUsername()
268 password
= config
.getTivoPassword()
270 if not username
or not password
:
271 raise Exception("tivo_username and tivo_password required")
273 return Mind(username
, password
)