You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
11 KiB

  1. from __future__ import unicode_literals
  2. import base64
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_urllib_parse_unquote
  6. from ..utils import (
  7. ExtractorError,
  8. clean_html,
  9. determine_ext,
  10. int_or_none,
  11. js_to_json,
  12. parse_age_limit,
  13. parse_duration,
  14. )
  15. class ViewLiftBaseIE(InfoExtractor):
  16. _DOMAINS_REGEX = r'(?:snagfilms|snagxtreme|funnyforfree|kiddovid|winnersview|(?:monumental|lax)sportsnetwork|vayafilm)\.com|hoichoi\.tv'
  17. class ViewLiftEmbedIE(ViewLiftBaseIE):
  18. _VALID_URL = r'https?://(?:(?:www|embed)\.)?(?:%s)/embed/player\?.*\bfilmId=(?P<id>[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12})' % ViewLiftBaseIE._DOMAINS_REGEX
  19. _TESTS = [{
  20. 'url': 'http://embed.snagfilms.com/embed/player?filmId=74849a00-85a9-11e1-9660-123139220831&w=500',
  21. 'md5': '2924e9215c6eff7a55ed35b72276bd93',
  22. 'info_dict': {
  23. 'id': '74849a00-85a9-11e1-9660-123139220831',
  24. 'ext': 'mp4',
  25. 'title': '#whilewewatch',
  26. }
  27. }, {
  28. # invalid labels, 360p is better that 480p
  29. 'url': 'http://www.snagfilms.com/embed/player?filmId=17ca0950-a74a-11e0-a92a-0026bb61d036',
  30. 'md5': '882fca19b9eb27ef865efeeaed376a48',
  31. 'info_dict': {
  32. 'id': '17ca0950-a74a-11e0-a92a-0026bb61d036',
  33. 'ext': 'mp4',
  34. 'title': 'Life in Limbo',
  35. }
  36. }, {
  37. 'url': 'http://www.snagfilms.com/embed/player?filmId=0000014c-de2f-d5d6-abcf-ffef58af0017',
  38. 'only_matching': True,
  39. }]
  40. @staticmethod
  41. def _extract_url(webpage):
  42. mobj = re.search(
  43. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:embed\.)?(?:%s)/embed/player.+?)\1' % ViewLiftBaseIE._DOMAINS_REGEX,
  44. webpage)
  45. if mobj:
  46. return mobj.group('url')
  47. def _real_extract(self, url):
  48. video_id = self._match_id(url)
  49. webpage = self._download_webpage(url, video_id)
  50. if '>This film is not playable in your area.<' in webpage:
  51. raise ExtractorError(
  52. 'Film %s is not playable in your area.' % video_id, expected=True)
  53. formats = []
  54. has_bitrate = False
  55. sources = self._parse_json(self._search_regex(
  56. r'(?s)sources:\s*(\[.+?\]),', webpage,
  57. 'sources', default='[]'), video_id, js_to_json)
  58. for source in sources:
  59. file_ = source.get('file')
  60. if not file_:
  61. continue
  62. type_ = source.get('type')
  63. ext = determine_ext(file_)
  64. format_id = source.get('label') or ext
  65. if all(v in ('m3u8', 'hls') for v in (type_, ext)):
  66. formats.extend(self._extract_m3u8_formats(
  67. file_, video_id, 'mp4', 'm3u8_native',
  68. m3u8_id='hls', fatal=False))
  69. else:
  70. bitrate = int_or_none(self._search_regex(
  71. [r'(\d+)kbps', r'_\d{1,2}x\d{1,2}_(\d{3,})\.%s' % ext],
  72. file_, 'bitrate', default=None))
  73. if not has_bitrate and bitrate:
  74. has_bitrate = True
  75. height = int_or_none(self._search_regex(
  76. r'^(\d+)[pP]$', format_id, 'height', default=None))
  77. formats.append({
  78. 'url': file_,
  79. 'format_id': 'http-%s%s' % (format_id, ('-%dk' % bitrate if bitrate else '')),
  80. 'tbr': bitrate,
  81. 'height': height,
  82. })
  83. if not formats:
  84. hls_url = self._parse_json(self._search_regex(
  85. r'filmInfo\.src\s*=\s*({.+?});',
  86. webpage, 'src'), video_id, js_to_json)['src']
  87. formats = self._extract_m3u8_formats(
  88. hls_url, video_id, 'mp4', 'm3u8_native',
  89. m3u8_id='hls', fatal=False)
  90. field_preference = None if has_bitrate else ('height', 'tbr', 'format_id')
  91. self._sort_formats(formats, field_preference)
  92. title = self._search_regex(
  93. [r"title\s*:\s*'([^']+)'", r'<title>([^<]+)</title>'],
  94. webpage, 'title')
  95. return {
  96. 'id': video_id,
  97. 'title': title,
  98. 'formats': formats,
  99. }
  100. class ViewLiftIE(ViewLiftBaseIE):
  101. _VALID_URL = r'https?://(?:www\.)?(?P<domain>%s)/(?:films/title|show|(?:news/)?videos?)/(?P<id>[^?#]+)' % ViewLiftBaseIE._DOMAINS_REGEX
  102. _TESTS = [{
  103. 'url': 'http://www.snagfilms.com/films/title/lost_for_life',
  104. 'md5': '19844f897b35af219773fd63bdec2942',
  105. 'info_dict': {
  106. 'id': '0000014c-de2f-d5d6-abcf-ffef58af0017',
  107. 'display_id': 'lost_for_life',
  108. 'ext': 'mp4',
  109. 'title': 'Lost for Life',
  110. 'description': 'md5:ea10b5a50405ae1f7b5269a6ec594102',
  111. 'thumbnail': r're:^https?://.*\.jpg',
  112. 'duration': 4489,
  113. 'categories': 'mincount:3',
  114. 'age_limit': 14,
  115. 'upload_date': '20150421',
  116. 'timestamp': 1429656819,
  117. }
  118. }, {
  119. 'url': 'http://www.snagfilms.com/show/the_world_cut_project/india',
  120. 'md5': 'e6292e5b837642bbda82d7f8bf3fbdfd',
  121. 'info_dict': {
  122. 'id': '00000145-d75c-d96e-a9c7-ff5c67b20000',
  123. 'display_id': 'the_world_cut_project/india',
  124. 'ext': 'mp4',
  125. 'title': 'India',
  126. 'description': 'md5:5c168c5a8f4719c146aad2e0dfac6f5f',
  127. 'thumbnail': r're:^https?://.*\.jpg',
  128. 'duration': 979,
  129. 'categories': 'mincount:2',
  130. 'timestamp': 1399478279,
  131. 'upload_date': '20140507',
  132. }
  133. }, {
  134. # Film is not playable in your area.
  135. 'url': 'http://www.snagfilms.com/films/title/inside_mecca',
  136. 'only_matching': True,
  137. }, {
  138. # Film is not available.
  139. 'url': 'http://www.snagfilms.com/show/augie_alone/flirting',
  140. 'only_matching': True,
  141. }, {
  142. 'url': 'http://www.winnersview.com/videos/the-good-son',
  143. 'only_matching': True,
  144. }, {
  145. # Was once Kaltura embed
  146. 'url': 'https://www.monumentalsportsnetwork.com/videos/john-carlson-postgame-2-25-15',
  147. 'only_matching': True,
  148. }]
  149. def _real_extract(self, url):
  150. domain, display_id = re.match(self._VALID_URL, url).groups()
  151. webpage = self._download_webpage(url, display_id)
  152. if ">Sorry, the Film you're looking for is not available.<" in webpage:
  153. raise ExtractorError(
  154. 'Film %s is not available.' % display_id, expected=True)
  155. initial_store_state = self._search_regex(
  156. r"window\.initialStoreState\s*=.*?JSON\.parse\(unescape\(atob\('([^']+)'\)\)\)",
  157. webpage, 'Initial Store State', default=None)
  158. if initial_store_state:
  159. modules = self._parse_json(compat_urllib_parse_unquote(base64.b64decode(
  160. initial_store_state).decode()), display_id)['page']['data']['modules']
  161. content_data = next(m['contentData'][0] for m in modules if m.get('moduleType') == 'VideoDetailModule')
  162. gist = content_data['gist']
  163. film_id = gist['id']
  164. title = gist['title']
  165. video_assets = content_data['streamingInfo']['videoAssets']
  166. formats = []
  167. mpeg_video_assets = video_assets.get('mpeg') or []
  168. for video_asset in mpeg_video_assets:
  169. video_asset_url = video_asset.get('url')
  170. if not video_asset:
  171. continue
  172. bitrate = int_or_none(video_asset.get('bitrate'))
  173. height = int_or_none(self._search_regex(
  174. r'^_?(\d+)[pP]$', video_asset.get('renditionValue'),
  175. 'height', default=None))
  176. formats.append({
  177. 'url': video_asset_url,
  178. 'format_id': 'http%s' % ('-%d' % bitrate if bitrate else ''),
  179. 'tbr': bitrate,
  180. 'height': height,
  181. 'vcodec': video_asset.get('codec'),
  182. })
  183. hls_url = video_assets.get('hls')
  184. if hls_url:
  185. formats.extend(self._extract_m3u8_formats(
  186. hls_url, film_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False))
  187. self._sort_formats(formats, ('height', 'tbr', 'format_id'))
  188. info = {
  189. 'id': film_id,
  190. 'display_id': display_id,
  191. 'title': title,
  192. 'description': gist.get('description'),
  193. 'thumbnail': gist.get('videoImageUrl'),
  194. 'duration': int_or_none(gist.get('runtime')),
  195. 'age_limit': parse_age_limit(content_data.get('parentalRating')),
  196. 'timestamp': int_or_none(gist.get('publishDate'), 1000),
  197. 'formats': formats,
  198. }
  199. for k in ('categories', 'tags'):
  200. info[k] = [v['title'] for v in content_data.get(k, []) if v.get('title')]
  201. return info
  202. else:
  203. film_id = self._search_regex(r'filmId=([\da-f-]{36})"', webpage, 'film id')
  204. snag = self._parse_json(
  205. self._search_regex(
  206. r'Snag\.page\.data\s*=\s*(\[.+?\]);', webpage, 'snag', default='[]'),
  207. display_id)
  208. for item in snag:
  209. if item.get('data', {}).get('film', {}).get('id') == film_id:
  210. data = item['data']['film']
  211. title = data['title']
  212. description = clean_html(data.get('synopsis'))
  213. thumbnail = data.get('image')
  214. duration = int_or_none(data.get('duration') or data.get('runtime'))
  215. categories = [
  216. category['title'] for category in data.get('categories', [])
  217. if category.get('title')]
  218. break
  219. else:
  220. title = self._search_regex(
  221. r'itemprop="title">([^<]+)<', webpage, 'title')
  222. description = self._html_search_regex(
  223. r'(?s)<div itemprop="description" class="film-synopsis-inner ">(.+?)</div>',
  224. webpage, 'description', default=None) or self._og_search_description(webpage)
  225. thumbnail = self._og_search_thumbnail(webpage)
  226. duration = parse_duration(self._search_regex(
  227. r'<span itemprop="duration" class="film-duration strong">([^<]+)<',
  228. webpage, 'duration', fatal=False))
  229. categories = re.findall(r'<a href="/movies/[^"]+">([^<]+)</a>', webpage)
  230. return {
  231. '_type': 'url_transparent',
  232. 'url': 'http://%s/embed/player?filmId=%s' % (domain, film_id),
  233. 'id': film_id,
  234. 'display_id': display_id,
  235. 'title': title,
  236. 'description': description,
  237. 'thumbnail': thumbnail,
  238. 'duration': duration,
  239. 'categories': categories,
  240. 'ie_key': 'ViewLiftEmbed',
  241. }