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.

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