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.

147 lines
4.8 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. int_or_none,
  7. unescapeHTML,
  8. url_or_none,
  9. )
  10. class TVNetIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:[^/]+)\.tvnet\.gov\.vn/[^/]+/(?:\d+/)?(?P<id>\d+)(?:/|$)'
  12. _TESTS = [{
  13. # video
  14. 'url': 'http://de.tvnet.gov.vn/video/109788/vtv1---bac-tuyet-tai-lao-cai-va-ha-giang/tin-nong-24h',
  15. 'md5': 'b4d7abe0252c9b47774760b7519c7558',
  16. 'info_dict': {
  17. 'id': '109788',
  18. 'ext': 'mp4',
  19. 'title': 'VTV1 - Bắc tuyết tại Lào Cai và Hà Giang',
  20. 'thumbnail': r're:(?i)https?://.*\.(?:jpg|png)',
  21. 'is_live': False,
  22. 'view_count': int,
  23. },
  24. }, {
  25. # audio
  26. 'url': 'http://vn.tvnet.gov.vn/radio/27017/vov1---ban-tin-chieu-10062018/doi-song-va-xa-hoi',
  27. 'md5': 'b5875ce9b0a2eecde029216d0e6db2ae',
  28. 'info_dict': {
  29. 'id': '27017',
  30. 'ext': 'm4a',
  31. 'title': 'VOV1 - Bản tin chiều (10/06/2018)',
  32. 'thumbnail': r're:(?i)https?://.*\.(?:jpg|png)',
  33. 'is_live': False,
  34. },
  35. }, {
  36. 'url': 'http://us.tvnet.gov.vn/video/118023/129999/ngay-0705',
  37. 'info_dict': {
  38. 'id': '129999',
  39. 'ext': 'mp4',
  40. 'title': 'VTV1 - Quốc hội với cử tri (11/06/2018)',
  41. 'thumbnail': r're:(?i)https?://.*\.(?:jpg|png)',
  42. 'is_live': False,
  43. },
  44. 'params': {
  45. 'skip_download': True,
  46. },
  47. }, {
  48. # live stream
  49. 'url': 'http://us.tvnet.gov.vn/kenh-truyen-hinh/1011/vtv1',
  50. 'info_dict': {
  51. 'id': '1011',
  52. 'ext': 'mp4',
  53. 'title': r're:^VTV1 \| LiveTV [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  54. 'thumbnail': r're:(?i)https?://.*\.(?:jpg|png)',
  55. 'is_live': True,
  56. },
  57. 'params': {
  58. 'skip_download': True,
  59. },
  60. }, {
  61. # radio live stream
  62. 'url': 'http://vn.tvnet.gov.vn/kenh-truyen-hinh/1014',
  63. 'info_dict': {
  64. 'id': '1014',
  65. 'ext': 'm4a',
  66. 'title': r're:VOV1 \| LiveTV [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  67. 'thumbnail': r're:(?i)https?://.*\.(?:jpg|png)',
  68. 'is_live': True,
  69. },
  70. 'params': {
  71. 'skip_download': True,
  72. },
  73. }, {
  74. 'url': 'http://us.tvnet.gov.vn/phim/6136/25510/vtv3---ca-mot-doi-an-oan-tap-1-50/phim-truyen-hinh',
  75. 'only_matching': True,
  76. }]
  77. def _real_extract(self, url):
  78. video_id = self._match_id(url)
  79. webpage = self._download_webpage(url, video_id)
  80. title = self._og_search_title(
  81. webpage, default=None) or self._html_search_meta(
  82. 'title', webpage, default=None) or self._search_regex(
  83. r'<title>([^<]+)<', webpage, 'title')
  84. title = re.sub(r'\s*-\s*TV Net\s*$', '', title)
  85. if '/video/' in url or '/radio/' in url:
  86. is_live = False
  87. elif '/kenh-truyen-hinh/' in url:
  88. is_live = True
  89. else:
  90. is_live = None
  91. data_file = unescapeHTML(self._search_regex(
  92. r'data-file=(["\'])(?P<url>(?:https?:)?//.+?)\1', webpage,
  93. 'data file', group='url'))
  94. stream_urls = set()
  95. formats = []
  96. for stream in self._download_json(data_file, video_id):
  97. if not isinstance(stream, dict):
  98. continue
  99. stream_url = url_or_none(stream.get('url'))
  100. if stream_url in stream_urls or not stream_url:
  101. continue
  102. stream_urls.add(stream_url)
  103. formats.extend(self._extract_m3u8_formats(
  104. stream_url, video_id, 'mp4',
  105. entry_protocol='m3u8' if is_live else 'm3u8_native',
  106. m3u8_id='hls', fatal=False))
  107. self._sort_formats(formats)
  108. # better support for radio streams
  109. if title.startswith('VOV'):
  110. for f in formats:
  111. f.update({
  112. 'ext': 'm4a',
  113. 'vcodec': 'none',
  114. })
  115. thumbnail = self._og_search_thumbnail(
  116. webpage, default=None) or unescapeHTML(
  117. self._search_regex(
  118. r'data-image=(["\'])(?P<url>(?:https?:)?//.+?)\1', webpage,
  119. 'thumbnail', default=None, group='url'))
  120. if is_live:
  121. title = self._live_title(title)
  122. view_count = int_or_none(self._search_regex(
  123. r'(?s)<div[^>]+\bclass=["\'].*?view-count[^>]+>.*?(\d+).*?</div>',
  124. webpage, 'view count', default=None))
  125. return {
  126. 'id': video_id,
  127. 'title': title,
  128. 'thumbnail': thumbnail,
  129. 'is_live': is_live,
  130. 'view_count': view_count,
  131. 'formats': formats,
  132. }