cammodels.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. ExtractorError,
  7. int_or_none,
  8. )
  9. class CamModelsIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www\.)?cammodels\.com/cam/(?P<id>[^/?#&]+)'
  11. _TESTS = [{
  12. 'url': 'https://www.cammodels.com/cam/AutumnKnight/',
  13. 'only_matching': True,
  14. }]
  15. def _real_extract(self, url):
  16. user_id = self._match_id(url)
  17. webpage = self._download_webpage(url, user_id)
  18. manifest_root = self._html_search_regex(
  19. r'manifestUrlRoot=([^&\']+)', webpage, 'manifest', default=None)
  20. if not manifest_root:
  21. ERRORS = (
  22. ("I'm offline, but let's stay connected", 'This user is currently offline'),
  23. ('in a private show', 'This user is in a private show'),
  24. ('is currently performing LIVE', 'This model is currently performing live'),
  25. )
  26. for pattern, message in ERRORS:
  27. if pattern in webpage:
  28. error = message
  29. expected = True
  30. break
  31. else:
  32. error = 'Unable to find manifest URL root'
  33. expected = False
  34. raise ExtractorError(error, expected=expected)
  35. manifest = self._download_json(
  36. '%s%s.json' % (manifest_root, user_id), user_id)
  37. formats = []
  38. for format_id, format_dict in manifest['formats'].items():
  39. if not isinstance(format_dict, dict):
  40. continue
  41. encodings = format_dict.get('encodings')
  42. if not isinstance(encodings, list):
  43. continue
  44. vcodec = format_dict.get('videoCodec')
  45. acodec = format_dict.get('audioCodec')
  46. for media in encodings:
  47. if not isinstance(media, dict):
  48. continue
  49. media_url = media.get('location')
  50. if not media_url or not isinstance(media_url, compat_str):
  51. continue
  52. format_id_list = [format_id]
  53. height = int_or_none(media.get('videoHeight'))
  54. if height is not None:
  55. format_id_list.append('%dp' % height)
  56. f = {
  57. 'url': media_url,
  58. 'format_id': '-'.join(format_id_list),
  59. 'width': int_or_none(media.get('videoWidth')),
  60. 'height': height,
  61. 'vbr': int_or_none(media.get('videoKbps')),
  62. 'abr': int_or_none(media.get('audioKbps')),
  63. 'fps': int_or_none(media.get('fps')),
  64. 'vcodec': vcodec,
  65. 'acodec': acodec,
  66. }
  67. if 'rtmp' in format_id:
  68. f['ext'] = 'flv'
  69. elif 'hls' in format_id:
  70. f.update({
  71. 'ext': 'mp4',
  72. # hls skips fragments, preferring rtmp
  73. 'preference': -1,
  74. })
  75. else:
  76. continue
  77. formats.append(f)
  78. self._sort_formats(formats)
  79. return {
  80. 'id': user_id,
  81. 'title': self._live_title(user_id),
  82. 'is_live': True,
  83. 'formats': formats,
  84. }