1 from plugins
.video
.video
import Video
, VideoDetails
13 import xml
.etree
.ElementTree
as ElementTree
16 CLASS_NAME
= 'WebVideo'
19 class WebVideo(Video
):
21 CONTENT_TYPE
= 'x-not-for/tivo'
24 self
.work_queue
= Queue
.Queue()
25 self
.download_thread_num
= 1
27 self
.in_progress_lock
= threading
.Lock()
31 self
.startWorkerThreads()
35 xmpp_info
= m
.getXMPPLoginInfo()
37 jid
=xmpp
.protocol
.JID(xmpp_info
['username'] + '/pyTivo')
39 server
=xmpp_info
['server'],
40 port
=xmpp_info
['port'],
44 cl
.RegisterHandler('message', self
.processMessage
)
45 cl
.auth(user
=jid
.getNode(), password
=config
.getTivoPassword(), resource
='pyTivo')
47 cl
.sendInitPresence(requestRoster
=0)
49 for user_name
in xmpp_info
['presence_list']:
50 jid
=xmpp
.protocol
.JID(user_name
)
53 t
= threading
.Thread(target
=self
.processXMPP
, args
=(cl
,))
57 def startWorkerThreads(self
):
58 for i
in range(self
.download_thread_num
):
59 t
= threading
.Thread(target
=self
.processDlRequest
)
63 def processXMPP(self
, client
):
64 while client
.Process():
67 def processMessage(self
, sess
,mess
):
68 xmpp_action
= ElementTree
.fromstring(mess
.getBody())
70 method_name
= 'xmpp_' + xmpp_action
.findtext('action').lower()
71 if not hasattr(self
, method_name
):
74 method
= getattr(self
, method_name
)
77 def xmpp_cdsupdate(self
, xml
=None):
80 self
.in_progress_lock
.acquire()
82 for request
in m
.getDownloadRequests():
83 if not request
['bodyOfferId'] in self
.in_progress
:
84 self
.in_progress
[request
['bodyOfferId']] = True
85 self
.work_queue
.put(request
)
87 self
.in_progress_lock
.release()
89 def processDlRequest(self
):
92 data
= self
.work_queue
.get()
94 for share_name
, settings
in config
.getShares():
95 if settings
['type'] == 'webvideo':
99 path
= settings
['path']
100 file_name
= os
.path
.join(path
, '%s-%s' % (data
['bodyOfferId'] ,data
['url'].split('/')[-1]))
102 self
.downloadFile(data
['url'], file_name
)
105 file_info
= VideoDetails()
106 file_info
.update(self
.metadata_full(file_name
, tsn
))
109 s
= socket
.socket(socket
.AF_INET
, socket
.SOCK_DGRAM
)
110 s
.connect(('tivo.com',123))
111 ip
= s
.getsockname()[0]
112 port
= config
.getPort()
114 data
['url'] = 'http://%s:%s' % (ip
, port
) + urllib
.quote('/%s/%s' % (share_name
, os
.path
.split(file_name
)[-1]))
115 data
['duration'] = file_info
['duration'] / 1000
116 data
['size'] = file_info
['size']
121 m
.completeDownloadRequest(data
)
123 self
.in_progress_lock
.acquire()
125 del self
.in_progress
[data
['bodyOfferId']]
127 self
.in_progress_lock
.release()
130 def downloadFile(self
, url
, file_path
):
131 print 'downloading %s to %s' % (url
, file_path
)
133 outfile
= open(file_path
, 'awb')
134 size
= os
.path
.getsize(file_path
)
135 r
= urllib2
.Request(url
)
137 r
.add_header('Range', 'bytes=%s-' % size
)
138 infile
= urllib2
.urlopen(r
)
139 shutil
.copyfileobj(infile
, outfile
, 8192)
141 print 'done downloading %s to %s' % (url
, file_path
)