Skip to content
Browse files

Moved Push-related code to a separate class, preparatory to allowing

inheritance from Video without Push.
  • Loading branch information...
1 parent 4b1c63c commit 6928141c0970d80bece08bd60d94d80d4f863a15 @wmcbrine committed Mar 11, 2012
Showing with 102 additions and 100 deletions.
  1. +102 −100 plugins/video/video.py
View
202 plugins/video/video.py
@@ -64,7 +64,108 @@ def isodt(iso):
def isogm(iso):
return int(calendar.timegm(uniso(iso)))
-class Video(Plugin):
+class Pushable(object):
+
+ def push_one_file(self, f):
+ file_info = VideoDetails()
+ file_info['valid'] = transcode.supported_format(f['path'])
+
+ mime = 'video/mpeg'
+ if config.isHDtivo(f['tsn']):
+ for m in ['video/mp4', 'video/bif']:
+ if transcode.tivo_compatible(f['path'], f['tsn'], m)[0]:
+ mime = m
+ break
+
+ if (mime == 'video/mpeg' and
+ transcode.mp4_remuxable(f['path'], f['tsn'])):
+ new_path = transcode.mp4_remux(f['path'], f['name'])
+ if new_path:
+ mime = 'video/mp4'
+ f['name'] = new_path
+
+ if file_info['valid']:
+ file_info.update(self.metadata_full(f['path'], f['tsn'], mime))
+
+ url = f['url'] + quote(f['name'])
+
+ title = file_info['seriesTitle']
+ if not title:
+ title = file_info['title']
+
+ source = file_info['seriesId']
+ if not source:
+ source = title
+
+ subtitle = file_info['episodeTitle']
+ try:
+ m = mind.getMind(f['tsn'])
+ m.pushVideo(
+ tsn = f['tsn'],
+ url = url,
+ description = file_info['description'],
+ duration = file_info['duration'] / 1000,
+ size = file_info['size'],
+ title = title,
+ subtitle = subtitle,
+ source = source,
+ mime = mime,
+ tvrating = file_info['tvRating'])
+ except Exception, msg:
+ logger.error(msg)
+
+ def process_queue(self):
+ while queue:
+ time.sleep(5)
+ item = queue.pop(0)
+ self.push_one_file(item)
+
+ def readip(self):
+ """ returns your external IP address by querying dyndns.org """
+ f = urllib.urlopen('http://checkip.dyndns.org/')
+ s = f.read()
+ m = re.search('([\d]*\.[\d]*\.[\d]*\.[\d]*)', s)
+ return m.group(0)
+
+ def Push(self, handler, query):
+ tsn = query['tsn'][0]
+ for key in config.tivo_names:
+ if config.tivo_names[key] == tsn:
+ tsn = key
+ break
+ tivo_name = config.tivo_names.get(tsn, tsn)
+
+ container = quote(query['Container'][0].split('/')[0])
+ ip = config.get_ip(tsn)
+ port = config.getPort()
+
+ baseurl = 'http://%s:%s/%s' % (ip, port, container)
+ if config.getIsExternal(tsn):
+ exturl = config.get_server('externalurl')
+ if exturl:
+ baseurl = exturl
+ else:
+ ip = self.readip()
+ baseurl = 'http://%s:%s/%s' % (ip, port, container)
+
+ path = self.get_local_base_path(handler, query)
+
+ files = query.get('File', [])
+ for f in files:
+ file_path = path + os.path.normpath(f)
+ queue.append({'path': file_path, 'name': f, 'tsn': tsn,
+ 'url': baseurl})
+ if len(queue) == 1:
+ thread.start_new_thread(Video.process_queue, (self,))
+
+ logger.info('[%s] Queued "%s" for Push to %s' %
+ (time.strftime('%d/%b/%Y %H:%M:%S'),
+ unicode(file_path, 'utf-8'), tivo_name))
+
+ files = [unicode(f, 'utf-8') for f in files]
+ handler.redir(PUSHED % (tivo_name, '<br>'.join(files)), 5)
+
+class Video(Plugin, Pushable):
CONTENT_TYPE = 'x-container/tivo-videos'
@@ -437,105 +538,6 @@ def TVBusQuery(self, handler, query):
handler.end_headers()
handler.wfile.write(details)
- def push_one_file(self, f):
- file_info = VideoDetails()
- file_info['valid'] = transcode.supported_format(f['path'])
-
- mime = 'video/mpeg'
- if config.isHDtivo(f['tsn']):
- for m in ['video/mp4', 'video/bif']:
- if transcode.tivo_compatible(f['path'], f['tsn'], m)[0]:
- mime = m
- break
-
- if (mime == 'video/mpeg' and
- transcode.mp4_remuxable(f['path'], f['tsn'])):
- new_path = transcode.mp4_remux(f['path'], f['name'])
- if new_path:
- mime = 'video/mp4'
- f['name'] = new_path
-
- if file_info['valid']:
- file_info.update(self.metadata_full(f['path'], f['tsn'], mime))
-
- url = f['url'] + quote(f['name'])
-
- title = file_info['seriesTitle']
- if not title:
- title = file_info['title']
-
- source = file_info['seriesId']
- if not source:
- source = title
-
- subtitle = file_info['episodeTitle']
- try:
- m = mind.getMind(f['tsn'])
- m.pushVideo(
- tsn = f['tsn'],
- url = url,
- description = file_info['description'],
- duration = file_info['duration'] / 1000,
- size = file_info['size'],
- title = title,
- subtitle = subtitle,
- source = source,
- mime = mime,
- tvrating = file_info['tvRating'])
- except Exception, msg:
- logger.error(msg)
-
- def process_queue(self):
- while queue:
- time.sleep(5)
- item = queue.pop(0)
- self.push_one_file(item)
-
- def Push(self, handler, query):
- tsn = query['tsn'][0]
- for key in config.tivo_names:
- if config.tivo_names[key] == tsn:
- tsn = key
- break
- tivo_name = config.tivo_names.get(tsn, tsn)
-
- container = quote(query['Container'][0].split('/')[0])
- ip = config.get_ip(tsn)
- port = config.getPort()
-
- baseurl = 'http://%s:%s/%s' % (ip, port, container)
- if config.getIsExternal(tsn):
- exturl = config.get_server('externalurl')
- if exturl:
- baseurl = exturl
- else:
- ip = self.readip()
- baseurl = 'http://%s:%s/%s' % (ip, port, container)
-
- path = self.get_local_base_path(handler, query)
-
- files = query.get('File', [])
- for f in files:
- file_path = path + os.path.normpath(f)
- queue.append({'path': file_path, 'name': f, 'tsn': tsn,
- 'url': baseurl})
- if len(queue) == 1:
- thread.start_new_thread(Video.process_queue, (self,))
-
- logger.info('[%s] Queued "%s" for Push to %s' %
- (time.strftime('%d/%b/%Y %H:%M:%S'),
- unicode(file_path, 'utf-8'), tivo_name))
-
- files = [unicode(f, 'utf-8') for f in files]
- handler.redir(PUSHED % (tivo_name, '<br>'.join(files)), 5)
-
- def readip(self):
- """ returns your external IP address by querying dyndns.org """
- f = urllib.urlopen('http://checkip.dyndns.org/')
- s = f.read()
- m = re.search('([\d]*\.[\d]*\.[\d]*\.[\d]*)', s)
- return m.group(0)
-
class VideoDetails(DictMixin):
def __init__(self, d=None):

0 comments on commit 6928141

Please sign in to comment.
Something went wrong with that request. Please try again.