[teamcoco] Parse the xml file and extract all the formats

This commit is contained in:
Jaime Marquínez Ferrándiz 2013-11-03 17:48:12 +01:00
parent 2a1a8ffe41
commit e7e6b54d8a

View File

@ -1,9 +1,9 @@
import re import re
import xml.etree.ElementTree
from .common import InfoExtractor from .common import InfoExtractor
from ..utils import ( from ..utils import (
ExtractorError, ExtractorError,
RegexNotFoundError,
) )
@ -32,29 +32,40 @@ class TeamcocoIE(InfoExtractor):
self.report_extraction(video_id) self.report_extraction(video_id)
data_url = 'http://teamcoco.com/cvp/2.0/%s.xml' % video_id data_url = 'http://teamcoco.com/cvp/2.0/%s.xml' % video_id
data = self._download_webpage(data_url, video_id, 'Downloading data webpage') data_xml = self._download_webpage(data_url, video_id, 'Downloading data webpage')
data = xml.etree.ElementTree.fromstring(data_xml.encode('utf-8'))
qualities = [ '1080p', '720p', '1000k', '480p', '500k' ] qualities = ['500k', '480p', '1000k', '720p', '1080p']
best_quality_idx = len(qualities)+1 # First regex match may not be optimal formats = []
for idx, quality in enumerate(qualities): for file in data.findall('files/file'):
regex = r'<file [^>]*type="(?:high|standard)".*?>(.*%s.*)</file>' % quality if file.attrib.get('playmode') == 'all':
# it just duplicates one of the entries
break
file_url = file.text
m_format = re.search(r'(\d+(k|p))\.mp4', file_url)
if m_format is not None:
format_id = m_format.group(1)
else:
format_id = file.attrib['bitrate']
formats.append({
'url': file_url,
'ext': 'mp4',
'format_id': format_id,
})
def sort_key(f):
try: try:
url = self._html_search_regex(regex, data, u'video URL') return qualities.index(f['format_id'])
if idx < best_quality_idx: except ValueError:
video_url = url return -1
best_quality_idx = idx formats.sort(key=sort_key)
except RegexNotFoundError: if not formats:
# Just catch fatal exc. Don't want the fatal=False warning
continue
if not video_url:
raise RegexNotFoundError(u'Unable to extract video URL') raise RegexNotFoundError(u'Unable to extract video URL')
return [{ return {
'id': video_id, 'id': video_id,
'url': video_url, 'formats': formats,
'ext': 'mp4',
'title': self._og_search_title(webpage), 'title': self._og_search_title(webpage),
'thumbnail': self._og_search_thumbnail(webpage), 'thumbnail': self._og_search_thumbnail(webpage),
'description': self._og_search_description(webpage), 'description': self._og_search_description(webpage),
}] }