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()),
57 'source': ('file:/C%3A%2FDocuments%20and%20Settings%2F' +
58 'Stephanie%2FDesktop%2FVideo'),
65 offer_id
, content_id
= self
.__bodyOfferModify
(data
)
66 self
.__subscribe
(offer_id
, content_id
, tsn
)
68 def getDownloadRequests(self
):
84 # It looks like tivo only supports one pc per house
85 pc_body_id
= self
.__pcBodySearch
()[0]
88 offer_list
= self
.__bodyOfferSchedule
(pc_body_id
)
90 for offer
in offer_list
.findall('bodyOffer'):
92 if offer
.findtext('state') != 'scheduled':
95 for n
in NEEDED_VALUES
:
96 d
[n
] = offer
.findtext(n
)
101 def completeDownloadRequest(self
, request
):
102 request
['encodingType'] = 'mpeg2ProgramStream'
103 request
['state'] = 'complete'
104 request
['type'] = 'bodyOfferModify'
105 request
['updateDate'] = time
.strftime('%Y-%m-%d %H:%M%S',
108 offer_id
, content_id
= self
.__bodyOfferModify
(request
)
109 self
.__subscribe
(offer_id
, content_id
, request
['bodyId'][4:])
112 def getXMPPLoginInfo(self
):
113 # It looks like tivo only supports one pc per house
114 pc_body_id
= self
.__pcBodySearch
()[0]
116 xml
= self
.__bodyXmppInfoGet
(pc_body_id
)
119 results
['server'] = xml
.findtext('server')
120 results
['port'] = int(xml
.findtext('port'))
121 results
['username'] = xml
.findtext('xmppId')
123 for sendPresence
in xml
.findall('sendPresence'):
124 results
.setdefault('presence_list',[]).append(sendPresence
.text
)
131 'cams_security_domain': 'tivocom',
132 'cams_login_config': 'http',
133 'cams_cb_username': self
.__username
,
134 'cams_cb_password': self
.__password
,
135 'cams_original_url': '/mind/mind7?type=infoGet'
139 'https://mind.tivo.com:8181/mind/login',
140 urllib
.urlencode(data
)
143 result
= self
.__opener
.open(r
)
147 self
.__logger
.debug('__login\n%s' % (data
))
149 def __dict_request(self
, data
, req
):
151 'https://mind.tivo.com:8181/mind/mind7?type=' + req
,
153 {'Content-Type': 'x-tivo/dict-binary'}
155 result
= self
.__opener
.open(r
)
157 xml
= ElementTree
.parse(result
).find('.')
159 self
.__logger
.debug('%s\n%s\n\n%sg' % (req
, data
,
160 ElementTree
.tostring(xml
)))
163 def __bodyOfferModify(self
, data
):
164 """Create an offer"""
166 xml
= self
.__dict
_request
(data
, 'bodyOfferModify&bodyId=' +
169 if xml
.findtext('state') != 'complete':
170 raise Exception(ElementTree
.tostring(xml
))
172 offer_id
= xml
.findtext('offerId')
173 content_id
= offer_id
.replace('of','ct')
175 return offer_id
, content_id
178 def __subscribe(self
, offer_id
, content_id
, tsn
):
179 """Push the offer to the tivo"""
181 'bodyId': 'tsn:' + tsn
,
183 'contentId': content_id
,
185 'type': 'singleOfferSource'
187 'title': 'pcBodySubscription',
191 return self
.__dict
_request
(data
, 'subscribe&bodyId=tsn:' + tsn
)
193 def __bodyOfferSchedule(self
, pc_body_id
):
194 """Get pending stuff for this pc"""
196 data
= {'pcBodyId': pc_body_id
}
197 return self
.__dict
_request
(data
, 'bodyOfferSchedule')
199 def __pcBodySearch(self
):
202 xml
= self
.__dict
_request
({}, 'pcBodySearch')
204 return [id.text
for id in xml
.findall('pcBody/pcBodyId')]
206 def __collectionIdSearch(self
, url
):
207 """Find collection ids"""
209 xml
= self
.__dict
_request
({'url': url
}, 'collectionIdSearch')
210 return xml
.findtext('collectionId')
212 def __pcBodyStore(self
, name
, replace
=False):
217 'replaceExisting': str(replace
).lower()
220 return self
.__dict
_request
(data
, 'pcBodyStore')
222 def __bodyXmppInfoGet(self
, body_id
):
224 return self
.__dict
_request
({'bodyId': body_id
},
225 'bodyXmppInfoGet&bodyId=' + body_id
)
229 """Helper to create x-tivo/dict-binary"""
232 keys
= [str(k
) for k
in d
]
238 output
.append( varint( len(k
) ) )
241 if isinstance(v
, dict):
242 output
.append( chr(2) )
243 output
.append( dictcode(v
) )
246 v
= unicode(v
).encode('utf-8')
247 output
.append( chr(1) )
248 output
.append( varint( len(v
) ) )
251 output
.append( chr(0) )
253 output
.append( chr(0x80) )
255 return ''.join(output
)
260 output
.append( chr(i
& 0x7f) )
262 output
.append( chr(i |
0x80) )
263 return ''.join(output
)
266 username
= config
.getTivoUsername()
267 password
= config
.getTivoPassword()
269 if not username
or not password
:
270 raise Exception("tivo_username and tivo_password required")
272 return Mind(username
, password
)