ign.py 9.2 KB

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