logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

expressen.py (3865B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. int_or_none,
  8. unescapeHTML,
  9. unified_timestamp,
  10. )
  11. class ExpressenIE(InfoExtractor):
  12. _VALID_URL = r'''(?x)
  13. https?://
  14. (?:www\.)?(?:expressen|di)\.se/
  15. (?:(?:tvspelare/video|videoplayer/embed)/)?
  16. tv/(?:[^/]+/)*
  17. (?P<id>[^/?#&]+)
  18. '''
  19. _TESTS = [{
  20. 'url': 'https://www.expressen.se/tv/ledare/ledarsnack/ledarsnack-om-arbetslosheten-bland-kvinnor-i-speciellt-utsatta-omraden/',
  21. 'md5': '2fbbe3ca14392a6b1b36941858d33a45',
  22. 'info_dict': {
  23. 'id': '8690962',
  24. 'ext': 'mp4',
  25. 'title': 'Ledarsnack: Om arbetslösheten bland kvinnor i speciellt utsatta områden',
  26. 'description': 'md5:f38c81ff69f3de4d269bbda012fcbbba',
  27. 'thumbnail': r're:^https?://.*\.jpg$',
  28. 'duration': 788,
  29. 'timestamp': 1526639109,
  30. 'upload_date': '20180518',
  31. },
  32. }, {
  33. 'url': 'https://www.expressen.se/tv/kultur/kulturdebatt-med-expressens-karin-olsson/',
  34. 'only_matching': True,
  35. }, {
  36. 'url': 'https://www.expressen.se/tvspelare/video/tv/ditv/ekonomistudion/experterna-har-ar-fragorna-som-avgor-valet/?embed=true&external=true&autoplay=true&startVolume=0&partnerId=di',
  37. 'only_matching': True,
  38. }, {
  39. 'url': 'https://www.expressen.se/videoplayer/embed/tv/ditv/ekonomistudion/experterna-har-ar-fragorna-som-avgor-valet/?embed=true&external=true&autoplay=true&startVolume=0&partnerId=di',
  40. 'only_matching': True,
  41. }, {
  42. 'url': 'https://www.di.se/videoplayer/embed/tv/ditv/borsmorgon/implantica-rusar-70--under-borspremiaren-hor-styrelsemedlemmen/?embed=true&external=true&autoplay=true&startVolume=0&partnerId=di',
  43. 'only_matching': True,
  44. }]
  45. @staticmethod
  46. def _extract_urls(webpage):
  47. return [
  48. mobj.group('url') for mobj in re.finditer(
  49. r'<iframe[^>]+\bsrc=(["\'])(?P<url>(?:https?:)?//(?:www\.)?(?:expressen|di)\.se/(?:tvspelare/video|videoplayer/embed)/tv/.+?)\1',
  50. webpage)]
  51. def _real_extract(self, url):
  52. display_id = self._match_id(url)
  53. webpage = self._download_webpage(url, display_id)
  54. def extract_data(name):
  55. return self._parse_json(
  56. self._search_regex(
  57. r'data-%s=(["\'])(?P<value>(?:(?!\1).)+)\1' % name,
  58. webpage, 'info', group='value'),
  59. display_id, transform_source=unescapeHTML)
  60. info = extract_data('video-tracking-info')
  61. video_id = info['videoId']
  62. data = extract_data('article-data')
  63. stream = data['stream']
  64. if determine_ext(stream) == 'm3u8':
  65. formats = self._extract_m3u8_formats(
  66. stream, display_id, 'mp4', entry_protocol='m3u8_native',
  67. m3u8_id='hls')
  68. else:
  69. formats = [{
  70. 'url': stream,
  71. }]
  72. self._sort_formats(formats)
  73. title = info.get('titleRaw') or data['title']
  74. description = info.get('descriptionRaw')
  75. thumbnail = info.get('socialMediaImage') or data.get('image')
  76. duration = int_or_none(info.get('videoTotalSecondsDuration')
  77. or data.get('totalSecondsDuration'))
  78. timestamp = unified_timestamp(info.get('publishDate'))
  79. return {
  80. 'id': video_id,
  81. 'display_id': display_id,
  82. 'title': title,
  83. 'description': description,
  84. 'thumbnail': thumbnail,
  85. 'duration': duration,
  86. 'timestamp': timestamp,
  87. 'formats': formats,
  88. }