ant1newsgr.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import urllib.parse
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. HEADRequest,
  5. ExtractorError,
  6. determine_ext,
  7. scale_thumbnails_to_max_format_width,
  8. )
  9. class Ant1NewsGrBaseIE(InfoExtractor):
  10. def _download_and_extract_api_data(self, video_id, netloc, cid=None):
  11. url = f'{self.http_scheme()}//{netloc}{self._API_PATH}'
  12. info = self._download_json(url, video_id, query={'cid': cid or video_id})
  13. try:
  14. source = info['url']
  15. except KeyError:
  16. raise ExtractorError('no source found for %s' % video_id)
  17. formats, subs = (self._extract_m3u8_formats_and_subtitles(source, video_id, 'mp4')
  18. if determine_ext(source) == 'm3u8' else ([{'url': source}], {}))
  19. thumbnails = scale_thumbnails_to_max_format_width(
  20. formats, [{'url': info['thumb']}], r'(?<=/imgHandler/)\d+')
  21. return {
  22. 'id': video_id,
  23. 'title': info.get('title'),
  24. 'thumbnails': thumbnails,
  25. 'formats': formats,
  26. 'subtitles': subs,
  27. }
  28. class Ant1NewsGrWatchIE(Ant1NewsGrBaseIE):
  29. IE_NAME = 'ant1newsgr:watch'
  30. IE_DESC = 'ant1news.gr videos'
  31. _VALID_URL = r'https?://(?P<netloc>(?:www\.)?ant1news\.gr)/watch/(?P<id>\d+)/'
  32. _API_PATH = '/templates/data/player'
  33. _TESTS = [{
  34. 'url': 'https://www.ant1news.gr/watch/1506168/ant1-news-09112021-stis-18-45',
  35. 'md5': '95925e6b32106754235f2417e0d2dfab',
  36. 'info_dict': {
  37. 'id': '1506168',
  38. 'ext': 'mp4',
  39. 'title': 'md5:0ad00fa66ecf8aa233d26ab0dba7514a',
  40. 'description': 'md5:18665af715a6dcfeac1d6153a44f16b0',
  41. 'thumbnail': 'https://ant1media.azureedge.net/imgHandler/640/26d46bf6-8158-4f02-b197-7096c714b2de.jpg',
  42. },
  43. }]
  44. def _real_extract(self, url):
  45. video_id, netloc = self._match_valid_url(url).group('id', 'netloc')
  46. webpage = self._download_webpage(url, video_id)
  47. info = self._download_and_extract_api_data(video_id, netloc)
  48. info['description'] = self._og_search_description(webpage)
  49. return info
  50. class Ant1NewsGrArticleIE(Ant1NewsGrBaseIE):
  51. IE_NAME = 'ant1newsgr:article'
  52. IE_DESC = 'ant1news.gr articles'
  53. _VALID_URL = r'https?://(?:www\.)?ant1news\.gr/[^/]+/article/(?P<id>\d+)/'
  54. _TESTS = [{
  55. 'url': 'https://www.ant1news.gr/afieromata/article/549468/o-tzeims-mpont-sta-meteora-oi-apeiles-kai-o-xesikomos-ton-kalogeron',
  56. 'md5': '294f18331bb516539d72d85a82887dcc',
  57. 'info_dict': {
  58. 'id': '_xvg/m_cmbatw=',
  59. 'ext': 'mp4',
  60. 'title': 'md5:a93e8ecf2e4073bfdffcb38f59945411',
  61. 'timestamp': 1603092840,
  62. 'upload_date': '20201019',
  63. 'thumbnail': 'https://ant1media.azureedge.net/imgHandler/640/756206d2-d640-40e2-b201-3555abdfc0db.jpg',
  64. },
  65. }, {
  66. 'url': 'https://ant1news.gr/Society/article/620286/symmoria-anilikon-dikigoros-thymaton-ithelan-na-toys-apoteleiosoyn',
  67. 'info_dict': {
  68. 'id': '620286',
  69. 'title': 'md5:91fe569e952e4d146485740ae927662b',
  70. },
  71. 'playlist_mincount': 2,
  72. 'params': {
  73. 'skip_download': True,
  74. },
  75. }]
  76. def _real_extract(self, url):
  77. video_id = self._match_id(url)
  78. webpage = self._download_webpage(url, video_id)
  79. info = self._search_json_ld(webpage, video_id, expected_type='NewsArticle')
  80. embed_urls = list(Ant1NewsGrEmbedIE._extract_embed_urls(url, webpage))
  81. if not embed_urls:
  82. raise ExtractorError('no videos found for %s' % video_id, expected=True)
  83. return self.playlist_from_matches(
  84. embed_urls, video_id, info.get('title'), ie=Ant1NewsGrEmbedIE.ie_key(),
  85. video_kwargs={'url_transparent': True, 'timestamp': info.get('timestamp')})
  86. class Ant1NewsGrEmbedIE(Ant1NewsGrBaseIE):
  87. IE_NAME = 'ant1newsgr:embed'
  88. IE_DESC = 'ant1news.gr embedded videos'
  89. _BASE_PLAYER_URL_RE = r'(?:https?:)?//(?:[a-zA-Z0-9\-]+\.)?(?:antenna|ant1news)\.gr/templates/pages/player'
  90. _VALID_URL = rf'{_BASE_PLAYER_URL_RE}\?([^#]+&)?cid=(?P<id>[^#&]+)'
  91. _EMBED_REGEX = [rf'<iframe[^>]+?src=(?P<_q1>["\'])(?P<url>{_BASE_PLAYER_URL_RE}\?(?:(?!(?P=_q1)).)+)(?P=_q1)']
  92. _API_PATH = '/news/templates/data/jsonPlayer'
  93. _TESTS = [{
  94. 'url': 'https://www.antenna.gr/templates/pages/player?cid=3f_li_c_az_jw_y_u=&w=670&h=377',
  95. 'md5': 'dfc58c3a11a5a9aad2ba316ed447def3',
  96. 'info_dict': {
  97. 'id': '3f_li_c_az_jw_y_u=',
  98. 'ext': 'mp4',
  99. 'title': 'md5:a30c93332455f53e1e84ae0724f0adf7',
  100. 'thumbnail': 'https://ant1media.azureedge.net/imgHandler/640/bbe31201-3f09-4a4e-87f5-8ad2159fffe2.jpg',
  101. },
  102. }]
  103. def _real_extract(self, url):
  104. video_id = self._match_id(url)
  105. canonical_url = self._request_webpage(
  106. HEADRequest(url), video_id,
  107. note='Resolve canonical player URL',
  108. errnote='Could not resolve canonical player URL').geturl()
  109. _, netloc, _, _, query, _ = urllib.parse.urlparse(canonical_url)
  110. cid = urllib.parse.parse_qs(query)['cid'][0]
  111. return self._download_and_extract_api_data(video_id, netloc, cid=cid)