Changeset e19fdd68c9407b567c1608c8b3a5a9396ab27552
- Timestamp:
- 04/06/08 03:38:44
(9 months ago)
- Author:
- Jason Michalski <armooo@armooo.net>
- git-committer:
- Jason Michalski <armooo@armooo.net> 1207471124 -0500
- git-parent:
[81d4220ba0feab8bdd6f39101f7bea2f451b9a98]
- git-author:
- Jason Michalski <armooo@armooo.net> 1207471124 -0500
- Message:
Updated to use a pool or worker threads
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r81d4220 |
re19fdd6 |
|
| 7 | 7 | import threading |
|---|
| 8 | 8 | import xml.etree.ElementTree as ElementTree |
|---|
| | 9 | import Queue |
|---|
| 9 | 10 | |
|---|
| 10 | 11 | CLASS_NAME = 'WebVideo' |
|---|
| … | … | |
| 16 | 17 | |
|---|
| 17 | 18 | def init(self): |
|---|
| 18 | | self.sem = threading.Semaphore(1) |
|---|
| | 19 | self.work_queue = Queue.Queue() |
|---|
| | 20 | self.download_thread_num = 1 |
|---|
| | 21 | self.in_progress = {} |
|---|
| | 22 | self.in_progress_lock = threading.Lock() |
|---|
| 19 | 23 | |
|---|
| 20 | 24 | self.startXMPP() |
|---|
| 21 | 25 | self.xmpp_cdsupdate() |
|---|
| | 26 | self.startWorkerThreads() |
|---|
| 22 | 27 | |
|---|
| 23 | 28 | def startXMPP(self): |
|---|
| … | … | |
| 41 | 46 | cl.sendPresence(jid) |
|---|
| 42 | 47 | |
|---|
| 43 | | |
|---|
| 44 | 48 | t = threading.Thread(target=self.processXMPP, args=(cl,)) |
|---|
| 45 | 49 | t.setDaemon(True) |
|---|
| 46 | 50 | t.start() |
|---|
| | 51 | |
|---|
| | 52 | def startWorkerThreads(self): |
|---|
| | 53 | for i in range(self.download_thread_num): |
|---|
| | 54 | t = threading.Thread(target=self.processDlRequest) |
|---|
| | 55 | t.setDaemon(True) |
|---|
| | 56 | t.start() |
|---|
| 47 | 57 | |
|---|
| 48 | 58 | def processXMPP(self, client): |
|---|
| … | … | |
| 60 | 70 | method(xmpp_action) |
|---|
| 61 | 71 | |
|---|
| 62 | | |
|---|
| 63 | 72 | def xmpp_cdsupdate(self, xml=None): |
|---|
| 64 | 73 | 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() |
|---|
| 69 | 74 | |
|---|
| 70 | | def processDlRequest(self, data): |
|---|
| | 75 | self.in_progress_lock.acquire() |
|---|
| | 76 | try: |
|---|
| | 77 | for request in m.getDownloadRequests(): |
|---|
| | 78 | if not request['bodyOfferId'] in self.in_progress: |
|---|
| | 79 | self.in_progress[request['bodyOfferId']] = True |
|---|
| | 80 | self.work_queue.put(request) |
|---|
| | 81 | finally: |
|---|
| | 82 | self.in_progress_lock.release() |
|---|
| | 83 | |
|---|
| | 84 | def processDlRequest(self): |
|---|
| 71 | 85 | import shutil |
|---|
| 72 | 86 | import os.path |
|---|
| … | … | |
| 74 | 88 | import urllib |
|---|
| 75 | 89 | |
|---|
| 76 | | for share_name, settings in config.getShares(): |
|---|
| 77 | | if settings['type'] == 'webvideo': |
|---|
| 78 | | break |
|---|
| | 90 | while True: |
|---|
| | 91 | data = self.work_queue.get() |
|---|
| 79 | 92 | |
|---|
| 80 | | self.sem.acquire() |
|---|
| | 93 | for share_name, settings in config.getShares(): |
|---|
| | 94 | if settings['type'] == 'webvideo': |
|---|
| | 95 | break |
|---|
| 81 | 96 | |
|---|
| 82 | | path = settings['path'] |
|---|
| 83 | | file_name = os.path.join(path, '%s-%s' % (data['bodyOfferId'] ,data['url'].split('/')[-1])) |
|---|
| 84 | 97 | |
|---|
| 85 | | print 'downloading %s to %s' % (data['url'], file_name) |
|---|
| | 98 | path = settings['path'] |
|---|
| | 99 | file_name = os.path.join(path, '%s-%s' % (data['bodyOfferId'] ,data['url'].split('/')[-1])) |
|---|
| 86 | 100 | |
|---|
| 87 | | outfile = open(file_name, 'wb') |
|---|
| | 101 | print 'downloading %s to %s' % (data['url'], file_name) |
|---|
| 88 | 102 | |
|---|
| 89 | | infile = urllib2.urlopen(data['url']) |
|---|
| 90 | | shutil.copyfileobj(infile, outfile) |
|---|
| | 103 | outfile = open(file_name, 'wb') |
|---|
| 91 | 104 | |
|---|
| 92 | | print 'done downloading %s to %s' % (data['url'], file_name) |
|---|
| | 105 | infile = urllib2.urlopen(data['url']) |
|---|
| | 106 | shutil.copyfileobj(infile, outfile) |
|---|
| 93 | 107 | |
|---|
| 94 | | tsn = data['bodyId'] |
|---|
| 95 | | file_info = VideoDetails() |
|---|
| 96 | | file_info.update(self.metadata_full(file_name, tsn)) |
|---|
| | 108 | print 'done downloading %s to %s' % (data['url'], file_name) |
|---|
| 97 | 109 | |
|---|
| 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() |
|---|
| | 110 | tsn = data['bodyId'] |
|---|
| | 111 | file_info = VideoDetails() |
|---|
| | 112 | file_info.update(self.metadata_full(file_name, tsn)) |
|---|
| 103 | 113 | |
|---|
| 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'] |
|---|
| | 114 | import socket |
|---|
| | 115 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|---|
| | 116 | s.connect(('tivo.com',123)) |
|---|
| | 117 | ip = s.getsockname()[0] |
|---|
| | 118 | port = config.getPort() |
|---|
| 107 | 119 | |
|---|
| 108 | | print data |
|---|
| | 120 | data['url'] = 'http://%s:%s' % (ip, port) + urllib.quote('/%s/%s' % (share_name, os.path.split(file_name)[-1])) |
|---|
| | 121 | data['duration'] = file_info['duration'] / 1000 |
|---|
| | 122 | data['size'] = file_info['size'] |
|---|
| 109 | 123 | |
|---|
| 110 | | m = mind.getMind() |
|---|
| 111 | | m.completeDownloadRequest(data) |
|---|
| | 124 | print data |
|---|
| 112 | 125 | |
|---|
| 113 | | self.sem.release() |
|---|
| | 126 | m = mind.getMind() |
|---|
| | 127 | m.completeDownloadRequest(data) |
|---|
| 114 | 128 | |
|---|
| | 129 | self.in_progress_lock.acquire() |
|---|
| | 130 | try: |
|---|
| | 131 | del self.in_progress[data['bodyOfferId']] |
|---|
| | 132 | finally: |
|---|
| | 133 | self.in_progress_lock.release() |
|---|
| | 134 | |
|---|