teamcoco.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. ExtractorError,
  6. )
  7. class TeamcocoIE(InfoExtractor):
  8. _VALID_URL = r'http://teamcoco\.com/video/(?P<video_id>\d*)?/?(?P<url_title>.*)'
  9. _TEST = {
  10. 'url': 'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
  11. 'file': '19705.mp4',
  12. 'md5': 'cde9ba0fa3506f5f017ce11ead928f9a',
  13. 'info_dict': {
  14. "description": "Louis C.K. got starstruck by George W. Bush, so what? Part one.",
  15. "title": "Louis C.K. Interview Pt. 1 11/3/11"
  16. }
  17. }
  18. def _real_extract(self, url):
  19. mobj = re.match(self._VALID_URL, url)
  20. if mobj is None:
  21. raise ExtractorError('Invalid URL: %s' % url)
  22. url_title = mobj.group('url_title')
  23. webpage = self._download_webpage(url, url_title)
  24. video_id = mobj.group("video_id")
  25. if video_id == '':
  26. video_id = self._html_search_regex(
  27. r'<article class="video" data-id="(\d+?)"',
  28. webpage, 'video id')
  29. self.report_extraction(video_id)
  30. data_url = 'http://teamcoco.com/cvp/2.0/%s.xml' % video_id
  31. data = self._download_xml(data_url, video_id, 'Downloading data webpage')
  32. qualities = ['500k', '480p', '1000k', '720p', '1080p']
  33. formats = []
  34. for filed in data.findall('files/file'):
  35. if filed.attrib.get('playmode') == 'all':
  36. # it just duplicates one of the entries
  37. break
  38. file_url = filed.text
  39. m_format = re.search(r'(\d+(k|p))\.mp4', file_url)
  40. if m_format is not None:
  41. format_id = m_format.group(1)
  42. else:
  43. format_id = filed.attrib['bitrate']
  44. tbr = (
  45. int(filed.attrib['bitrate'])
  46. if filed.attrib['bitrate'].isdigit()
  47. else None)
  48. try:
  49. quality = qualities.index(format_id)
  50. except ValueError:
  51. quality = -1
  52. formats.append({
  53. 'url': file_url,
  54. 'ext': 'mp4',
  55. 'tbr': tbr,
  56. 'format_id': format_id,
  57. 'quality': quality,
  58. })
  59. self._sort_formats(formats)
  60. return {
  61. 'id': video_id,
  62. 'formats': formats,
  63. 'title': self._og_search_title(webpage),
  64. 'thumbnail': self._og_search_thumbnail(webpage),
  65. 'description': self._og_search_description(webpage),
  66. }