filmon.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from .common import InfoExtractor
  2. from ..networking.exceptions import HTTPError
  3. from ..utils import (
  4. ExtractorError,
  5. int_or_none,
  6. qualities,
  7. strip_or_none,
  8. )
  9. class FilmOnIE(InfoExtractor):
  10. IE_NAME = 'filmon'
  11. _VALID_URL = r'(?:https?://(?:www\.)?filmon\.com/vod/view/|filmon:)(?P<id>\d+)'
  12. _TESTS = [{
  13. 'url': 'https://www.filmon.com/vod/view/24869-0-plan-9-from-outer-space',
  14. 'info_dict': {
  15. 'id': '24869',
  16. 'ext': 'mp4',
  17. 'title': 'Plan 9 From Outer Space',
  18. 'description': 'Dead human, zombies and vampires',
  19. },
  20. }, {
  21. 'url': 'https://www.filmon.com/vod/view/2825-1-popeye-series-1',
  22. 'info_dict': {
  23. 'id': '2825',
  24. 'title': 'Popeye Series 1',
  25. 'description': 'The original series of Popeye.',
  26. },
  27. 'playlist_mincount': 8,
  28. }]
  29. def _real_extract(self, url):
  30. video_id = self._match_id(url)
  31. try:
  32. response = self._download_json(
  33. f'https://www.filmon.com/api/vod/movie?id={video_id}',
  34. video_id)['response']
  35. except ExtractorError as e:
  36. if isinstance(e.cause, HTTPError):
  37. errmsg = self._parse_json(e.cause.response.read().decode(), video_id)['reason']
  38. raise ExtractorError(f'{self.IE_NAME} said: {errmsg}', expected=True)
  39. raise
  40. title = response['title']
  41. description = strip_or_none(response.get('description'))
  42. if response.get('type_id') == 1:
  43. entries = [self.url_result('filmon:' + episode_id) for episode_id in response.get('episodes', [])]
  44. return self.playlist_result(entries, video_id, title, description)
  45. QUALITY = qualities(('low', 'high'))
  46. formats = []
  47. for format_id, stream in response.get('streams', {}).items():
  48. stream_url = stream.get('url')
  49. if not stream_url:
  50. continue
  51. formats.append({
  52. 'format_id': format_id,
  53. 'url': stream_url,
  54. 'ext': 'mp4',
  55. 'quality': QUALITY(stream.get('quality')),
  56. 'protocol': 'm3u8_native',
  57. })
  58. thumbnails = []
  59. poster = response.get('poster', {})
  60. thumbs = poster.get('thumbs', {})
  61. thumbs['poster'] = poster
  62. for thumb_id, thumb in thumbs.items():
  63. thumb_url = thumb.get('url')
  64. if not thumb_url:
  65. continue
  66. thumbnails.append({
  67. 'id': thumb_id,
  68. 'url': thumb_url,
  69. 'width': int_or_none(thumb.get('width')),
  70. 'height': int_or_none(thumb.get('height')),
  71. })
  72. return {
  73. 'id': video_id,
  74. 'title': title,
  75. 'formats': formats,
  76. 'description': description,
  77. 'thumbnails': thumbnails,
  78. }
  79. class FilmOnChannelIE(InfoExtractor):
  80. IE_NAME = 'filmon:channel'
  81. _VALID_URL = r'https?://(?:www\.)?filmon\.com/(?:tv|channel)/(?P<id>[a-z0-9-]+)'
  82. _TESTS = [{
  83. # VOD
  84. 'url': 'http://www.filmon.com/tv/sports-haters',
  85. 'info_dict': {
  86. 'id': '4190',
  87. 'ext': 'mp4',
  88. 'title': 'Sports Haters',
  89. 'description': 'md5:dabcb4c1d9cfc77085612f1a85f8275d',
  90. },
  91. }, {
  92. # LIVE
  93. 'url': 'https://www.filmon.com/channel/filmon-sports',
  94. 'only_matching': True,
  95. }, {
  96. 'url': 'https://www.filmon.com/tv/2894',
  97. 'only_matching': True,
  98. }]
  99. _THUMBNAIL_RES = [
  100. ('logo', 56, 28),
  101. ('big_logo', 106, 106),
  102. ('extra_big_logo', 300, 300),
  103. ]
  104. def _real_extract(self, url):
  105. channel_id = self._match_id(url)
  106. try:
  107. channel_data = self._download_json(
  108. 'http://www.filmon.com/api-v2/channel/' + channel_id, channel_id)['data']
  109. except ExtractorError as e:
  110. if isinstance(e.cause, HTTPError):
  111. errmsg = self._parse_json(e.cause.response.read().decode(), channel_id)['message']
  112. raise ExtractorError(f'{self.IE_NAME} said: {errmsg}', expected=True)
  113. raise
  114. channel_id = str(channel_data['id'])
  115. is_live = not channel_data.get('is_vod') and not channel_data.get('is_vox')
  116. title = channel_data['title']
  117. QUALITY = qualities(('low', 'high'))
  118. formats = []
  119. for stream in channel_data.get('streams', []):
  120. stream_url = stream.get('url')
  121. if not stream_url:
  122. continue
  123. if not is_live:
  124. formats.extend(self._extract_wowza_formats(
  125. stream_url, channel_id, skip_protocols=['dash', 'rtmp', 'rtsp']))
  126. continue
  127. quality = stream.get('quality')
  128. formats.append({
  129. 'format_id': quality,
  130. # this is an m3u8 stream, but we are deliberately not using _extract_m3u8_formats
  131. # because it doesn't have bitrate variants anyway
  132. 'url': stream_url,
  133. 'ext': 'mp4',
  134. 'quality': QUALITY(quality),
  135. })
  136. thumbnails = []
  137. for name, width, height in self._THUMBNAIL_RES:
  138. thumbnails.append({
  139. 'id': name,
  140. 'url': f'http://static.filmon.com/assets/channels/{channel_id}/{name}.png',
  141. 'width': width,
  142. 'height': height,
  143. })
  144. return {
  145. 'id': channel_id,
  146. 'display_id': channel_data.get('alias'),
  147. 'title': title,
  148. 'description': channel_data.get('description'),
  149. 'thumbnails': thumbnails,
  150. 'formats': formats,
  151. 'is_live': is_live,
  152. }