tvnet.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. int_or_none,
  8. unescapeHTML,
  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 = stream.get('url')
  100. if (stream_url in stream_urls or not stream_url or
  101. not isinstance(stream_url, compat_str)):
  102. continue
  103. stream_urls.add(stream_url)
  104. formats.extend(self._extract_m3u8_formats(
  105. stream_url, video_id, 'mp4',
  106. entry_protocol='m3u8' if is_live else 'm3u8_native',
  107. m3u8_id='hls', fatal=False))
  108. self._sort_formats(formats)
  109. # better support for radio streams
  110. if title.startswith('VOV'):
  111. for f in formats:
  112. f.update({
  113. 'ext': 'm4a',
  114. 'vcodec': 'none',
  115. })
  116. thumbnail = self._og_search_thumbnail(
  117. webpage, default=None) or unescapeHTML(
  118. self._search_regex(
  119. r'data-image=(["\'])(?P<url>(?:https?:)?//.+?)\1', webpage,
  120. 'thumbnail', default=None, group='url'))
  121. if is_live:
  122. title = self._live_title(title)
  123. view_count = int_or_none(self._search_regex(
  124. r'(?s)<div[^>]+\bclass=["\'].*?view-count[^>]+>.*?(\d+).*?</div>',
  125. webpage, 'view count', default=None))
  126. return {
  127. 'id': video_id,
  128. 'title': title,
  129. 'thumbnail': thumbnail,
  130. 'is_live': is_live,
  131. 'view_count': view_count,
  132. 'formats': formats,
  133. }