web video should be working.
[pyTivo.git] / plugins / webvideo / webvideo.py
blob06102100223eda74976cf3f8ae7ab98c9ad17701
1 from plugins.video.video import Video, VideoDetails
2 import mind
3 import config
5 import xmpp
7 import threading
8 import xml.etree.ElementTree as ElementTree
10 CLASS_NAME = 'WebVideo'
13 class WebVideo(Video):
15 CONTENT_TYPE = 'x-not-for/tivo'
17 def init(self):
18 self.sem = threading.Semaphore(1)
20 self.startXMPP()
21 self.xmpp_cdsupdate()
23 def startXMPP(self):
24 m = mind.getMind()
25 xmpp_info = m.getXMPPLoginInfo()
27 jid=xmpp.protocol.JID(xmpp_info['username'] + '/pyTivo')
28 cl=xmpp.Client(
29 server=xmpp_info['server'],
30 port=xmpp_info['port'],
33 cl.connect()
34 cl.RegisterHandler('message', self.processMessage)
35 cl.auth(user=jid.getNode(), password=config.getTivoPassword(), resource='pyTivo')
37 cl.sendInitPresence(requestRoster=0)
39 for user_name in xmpp_info['presence_list']:
40 jid=xmpp.protocol.JID(user_name)
41 cl.sendPresence(jid)
44 t = threading.Thread(target=self.processXMPP, args=(cl,))
45 t.setDaemon(True)
46 t.start()
48 def processXMPP(self, client):
49 while client.Process():
50 pass
52 def processMessage(self, sess,mess):
53 xmpp_action = ElementTree.fromstring(mess.getBody())
55 method_name = 'xmpp_' + xmpp_action.findtext('action').lower()
56 if not hasattr(self, method_name):
57 return False
59 method = getattr(self, method_name)
60 method(xmpp_action)
63 def xmpp_cdsupdate(self, xml=None):
64 m = mind.getMind()
65 for request in m.getDownloadRequests():
66 t = threading.Thread(target=self.processDlRequest, args=(request,))
67 t.setDaemon(True)
68 t.start()
70 def processDlRequest(self, data):
71 import shutil
72 import os.path
73 import urllib2
74 import urllib
76 for share_name, settings in config.getShares():
77 if settings['type'] == 'webvideo':
78 break
80 self.sem.acquire()
82 path = settings['path']
83 file_name = os.path.join(path, '%s-%s' % (data['bodyOfferId'] ,data['url'].split('/')[-1]))
85 print 'downloading %s to %s' % (data['url'], file_name)
87 outfile = open(file_name, 'wb')
89 infile = urllib2.urlopen(data['url'])
90 shutil.copyfileobj(infile, outfile)
92 print 'done downloading %s to %s' % (data['url'], file_name)
94 tsn = data['bodyId']
95 file_info = VideoDetails()
96 file_info.update(self.metadata_full(file_name, tsn))
98 import socket
99 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
100 s.connect(('tivo.com',123))
101 ip = s.getsockname()[0]
102 port = config.getPort()
104 data['url'] = 'http://%s:%s' % (ip, port) + urllib.quote('/%s/%s' % (share_name, os.path.split(file_name)[-1]))
105 data['duration'] = file_info['duration'] / 1000
106 data['size'] = file_info['size']
108 print data
110 m = mind.getMind()
111 m.completeDownloadRequest(data)
113 self.sem.release()