ign.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_parse_qs,
  6. compat_urllib_parse_urlparse,
  7. )
  8. from ..utils import (
  9. HEADRequest,
  10. determine_ext,
  11. int_or_none,
  12. parse_iso8601,
  13. strip_or_none,
  14. try_get,
  15. )
  16. class IGNBaseIE(InfoExtractor):
  17. def _call_api(self, slug):
  18. return self._download_json(
  19. 'http://apis.ign.com/{0}/v3/{0}s/slug/{1}'.format(self._PAGE_TYPE, slug), slug)
  20. class IGNIE(IGNBaseIE):
  21. """
  22. Extractor for some of the IGN sites, like www.ign.com, es.ign.com de.ign.com.
  23. Some videos of it.ign.com are also supported
  24. """
  25. _VALID_URL = r'https?://(?:.+?\.ign|www\.pcmag)\.com/videos/(?:\d{4}/\d{2}/\d{2}/)?(?P<id>[^/?&#]+)'
  26. IE_NAME = 'ign.com'
  27. _PAGE_TYPE = 'video'
  28. _TESTS = [{
  29. 'url': 'http://www.ign.com/videos/2013/06/05/the-last-of-us-review',
  30. 'md5': 'd2e1586d9987d40fad7867bf96a018ea',
  31. 'info_dict': {
  32. 'id': '8f862beef863986b2785559b9e1aa599',
  33. 'ext': 'mp4',
  34. 'title': 'The Last of Us Review',
  35. 'description': 'md5:c8946d4260a4d43a00d5ae8ed998870c',
  36. 'timestamp': 1370440800,
  37. 'upload_date': '20130605',
  38. 'tags': 'count:9',
  39. }
  40. }, {
  41. 'url': 'http://www.pcmag.com/videos/2015/01/06/010615-whats-new-now-is-gogo-snooping-on-your-data',
  42. 'md5': 'f1581a6fe8c5121be5b807684aeac3f6',
  43. 'info_dict': {
  44. 'id': 'ee10d774b508c9b8ec07e763b9125b91',
  45. 'ext': 'mp4',
  46. 'title': 'What\'s New Now: Is GoGo Snooping on Your Data?',
  47. 'description': 'md5:817a20299de610bd56f13175386da6fa',
  48. 'timestamp': 1420571160,
  49. 'upload_date': '20150106',
  50. 'tags': 'count:4',
  51. }
  52. }, {
  53. 'url': 'https://www.ign.com/videos/is-a-resident-evil-4-remake-on-the-way-ign-daily-fix',
  54. 'only_matching': True,
  55. }]
  56. def _real_extract(self, url):
  57. display_id = self._match_id(url)
  58. video = self._call_api(display_id)
  59. video_id = video['videoId']
  60. metadata = video['metadata']
  61. title = metadata.get('longTitle') or metadata.get('title') or metadata['name']
  62. formats = []
  63. refs = video.get('refs') or {}
  64. m3u8_url = refs.get('m3uUrl')
  65. if m3u8_url:
  66. formats.extend(self._extract_m3u8_formats(
  67. m3u8_url, video_id, 'mp4', 'm3u8_native',
  68. m3u8_id='hls', fatal=False))
  69. f4m_url = refs.get('f4mUrl')
  70. if f4m_url:
  71. formats.extend(self._extract_f4m_formats(
  72. f4m_url, video_id, f4m_id='hds', fatal=False))
  73. for asset in (video.get('assets') or []):
  74. asset_url = asset.get('url')
  75. if not asset_url:
  76. continue
  77. formats.append({
  78. 'url': asset_url,
  79. 'tbr': int_or_none(asset.get('bitrate'), 1000),
  80. 'fps': int_or_none(asset.get('frame_rate')),
  81. 'height': int_or_none(asset.get('height')),
  82. 'width': int_or_none(asset.get('width')),
  83. })
  84. mezzanine_url = try_get(video, lambda x: x['system']['mezzanineUrl'])
  85. if mezzanine_url:
  86. formats.append({
  87. 'ext': determine_ext(mezzanine_url, 'mp4'),
  88. 'format_id': 'mezzanine',
  89. 'preference': 1,
  90. 'url': mezzanine_url,
  91. })
  92. self._sort_formats(formats)
  93. thumbnails = []
  94. for thumbnail in (video.get('thumbnails') or []):
  95. thumbnail_url = thumbnail.get('url')
  96. if not thumbnail_url:
  97. continue
  98. thumbnails.append({
  99. 'url': thumbnail_url,
  100. })
  101. tags = []
  102. for tag in (video.get('tags') or []):
  103. display_name = tag.get('displayName')
  104. if not display_name:
  105. continue
  106. tags.append(display_name)
  107. return {
  108. 'id': video_id,
  109. 'title': title,
  110. 'description': strip_or_none(metadata.get('description')),
  111. 'timestamp': parse_iso8601(metadata.get('publishDate')),
  112. 'duration': int_or_none(metadata.get('duration')),
  113. 'display_id': display_id,
  114. 'thumbnails': thumbnails,
  115. 'formats': formats,
  116. 'tags': tags,
  117. }
  118. class IGNVideoIE(InfoExtractor):
  119. _VALID_URL = r'https?://.+?\.ign\.com/(?:[a-z]{2}/)?[^/]+/(?P<id>\d+)/(?:video|trailer)/'
  120. _TESTS = [{
  121. 'url': 'http://me.ign.com/en/videos/112203/video/how-hitman-aims-to-be-different-than-every-other-s',
  122. 'md5': 'dd9aca7ed2657c4e118d8b261e5e9de1',
  123. 'info_dict': {
  124. 'id': 'e9be7ea899a9bbfc0674accc22a36cc8',
  125. 'ext': 'mp4',
  126. 'title': 'How Hitman Aims to Be Different Than Every Other Stealth Game - NYCC 2015',
  127. 'description': 'Taking out assassination targets in Hitman has never been more stylish.',
  128. 'timestamp': 1444665600,
  129. 'upload_date': '20151012',
  130. }
  131. }, {
  132. 'url': 'http://me.ign.com/ar/angry-birds-2/106533/video/lrd-ldyy-lwl-lfylm-angry-birds',
  133. 'only_matching': True,
  134. }, {
  135. # Youtube embed
  136. 'url': 'https://me.ign.com/ar/ratchet-clank-rift-apart/144327/trailer/embed',
  137. 'only_matching': True,
  138. }, {
  139. # Twitter embed
  140. 'url': 'http://adria.ign.com/sherlock-season-4/9687/trailer/embed',
  141. 'only_matching': True,
  142. }, {
  143. # Vimeo embed
  144. 'url': 'https://kr.ign.com/bic-2018/3307/trailer/embed',
  145. 'only_matching': True,
  146. }]
  147. def _real_extract(self, url):
  148. video_id = self._match_id(url)
  149. req = HEADRequest(url.rsplit('/', 1)[0] + '/embed')
  150. url = self._request_webpage(req, video_id).geturl()
  151. ign_url = compat_parse_qs(
  152. compat_urllib_parse_urlparse(url).query).get('url', [None])[0]
  153. if ign_url:
  154. return self.url_result(ign_url, IGNIE.ie_key())
  155. return self.url_result(url)
  156. class IGNArticleIE(IGNBaseIE):
  157. _VALID_URL = r'https?://.+?\.ign\.com/(?:articles(?:/\d{4}/\d{2}/\d{2})?|(?:[a-z]{2}/)?feature/\d+)/(?P<id>[^/?&#]+)'
  158. _PAGE_TYPE = 'article'
  159. _TESTS = [{
  160. 'url': 'http://me.ign.com/en/feature/15775/100-little-things-in-gta-5-that-will-blow-your-mind',
  161. 'info_dict': {
  162. 'id': '524497489e4e8ff5848ece34',
  163. 'title': '100 Little Things in GTA 5 That Will Blow Your Mind',
  164. },
  165. 'playlist': [
  166. {
  167. 'info_dict': {
  168. 'id': '5ebbd138523268b93c9141af17bec937',
  169. 'ext': 'mp4',
  170. 'title': 'GTA 5 Video Review',
  171. 'description': 'Rockstar drops the mic on this generation of games. Watch our review of the masterly Grand Theft Auto V.',
  172. 'timestamp': 1379339880,
  173. 'upload_date': '20130916',
  174. },
  175. },
  176. {
  177. 'info_dict': {
  178. 'id': '638672ee848ae4ff108df2a296418ee2',
  179. 'ext': 'mp4',
  180. 'title': '26 Twisted Moments from GTA 5 in Slow Motion',
  181. 'description': 'The twisted beauty of GTA 5 in stunning slow motion.',
  182. 'timestamp': 1386878820,
  183. 'upload_date': '20131212',
  184. },
  185. },
  186. ],
  187. 'params': {
  188. 'playlist_items': '2-3',
  189. 'skip_download': True,
  190. },
  191. }, {
  192. 'url': 'http://www.ign.com/articles/2014/08/15/rewind-theater-wild-trailer-gamescom-2014?watch',
  193. 'info_dict': {
  194. 'id': '53ee806780a81ec46e0790f8',
  195. 'title': 'Rewind Theater - Wild Trailer Gamescom 2014',
  196. },
  197. 'playlist_count': 2,
  198. }, {
  199. # videoId pattern
  200. 'url': 'http://www.ign.com/articles/2017/06/08/new-ducktales-short-donalds-birthday-doesnt-go-as-planned',
  201. 'only_matching': True,
  202. }, {
  203. # Youtube embed
  204. 'url': 'https://www.ign.com/articles/2021-mvp-named-in-puppy-bowl-xvii',
  205. 'only_matching': True,
  206. }, {
  207. # IMDB embed
  208. 'url': 'https://www.ign.com/articles/2014/08/07/sons-of-anarchy-final-season-trailer',
  209. 'only_matching': True,
  210. }, {
  211. # Facebook embed
  212. 'url': 'https://www.ign.com/articles/2017/09/20/marvels-the-punisher-watch-the-new-trailer-for-the-netflix-series',
  213. 'only_matching': True,
  214. }, {
  215. # Brightcove embed
  216. 'url': 'https://www.ign.com/articles/2016/01/16/supergirl-goes-flying-with-martian-manhunter-in-new-clip',
  217. 'only_matching': True,
  218. }]
  219. def _real_extract(self, url):
  220. display_id = self._match_id(url)
  221. article = self._call_api(display_id)
  222. def entries():
  223. media_url = try_get(article, lambda x: x['mediaRelations'][0]['media']['metadata']['url'])
  224. if media_url:
  225. yield self.url_result(media_url, IGNIE.ie_key())
  226. for content in (article.get('content') or []):
  227. for video_url in re.findall(r'(?:\[(?:ignvideo\s+url|youtube\s+clip_id)|<iframe[^>]+src)="([^"]+)"', content):
  228. yield self.url_result(video_url)
  229. return self.playlist_result(
  230. entries(), article.get('articleId'),
  231. strip_or_none(try_get(article, lambda x: x['metadata']['headline'])))