lifenews.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # encoding: 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. unified_strdate,
  8. ExtractorError,
  9. )
  10. class LifeNewsIE(InfoExtractor):
  11. IE_NAME = 'lifenews'
  12. IE_DESC = 'LIFE | NEWS'
  13. _VALID_URL = r'http://lifenews\.ru/(?:mobile/)?(?P<section>news|video)/(?P<id>\d+)'
  14. _TESTS = [{
  15. 'url': 'http://lifenews.ru/news/126342',
  16. 'md5': 'e1b50a5c5fb98a6a544250f2e0db570a',
  17. 'info_dict': {
  18. 'id': '126342',
  19. 'ext': 'mp4',
  20. 'title': 'МВД разыскивает мужчин, оставивших в IKEA сумку с автоматом',
  21. 'description': 'Камеры наблюдения гипермаркета зафиксировали троих мужчин, спрятавших оружейный арсенал в камере хранения.',
  22. 'thumbnail': 're:http://.*\.jpg',
  23. 'upload_date': '20140130',
  24. }
  25. }, {
  26. # video in <iframe>
  27. 'url': 'http://lifenews.ru/news/152125',
  28. 'md5': '77d19a6f0886cd76bdbf44b4d971a273',
  29. 'info_dict': {
  30. 'id': '152125',
  31. 'ext': 'mp4',
  32. 'title': 'В Сети появилось видео захвата «Правым сектором» колхозных полей ',
  33. 'description': 'Жители двух поселков Днепропетровской области не простили радикалам угрозу лишения плодородных земель и пошли в лобовую. ',
  34. 'upload_date': '20150402',
  35. 'uploader': 'embed.life.ru',
  36. }
  37. }, {
  38. 'url': 'http://lifenews.ru/news/153461',
  39. 'md5': '9b6ef8bc0ffa25aebc8bdb40d89ab795',
  40. 'info_dict': {
  41. 'id': '153461',
  42. 'ext': 'mp4',
  43. 'title': 'В Москве спасли потерявшегося медвежонка, который спрятался на дереве',
  44. 'description': 'Маленький хищник не смог найти дорогу домой и обрел временное убежище на тополе недалеко от жилого массива, пока его не нашла соседская собака.',
  45. 'upload_date': '20150505',
  46. 'uploader': 'embed.life.ru',
  47. }
  48. }, {
  49. 'url': 'http://lifenews.ru/video/13035',
  50. 'only_matching': True,
  51. }]
  52. def _real_extract(self, url):
  53. mobj = re.match(self._VALID_URL, url)
  54. video_id = mobj.group('id')
  55. section = mobj.group('section')
  56. webpage = self._download_webpage(
  57. 'http://lifenews.ru/%s/%s' % (section, video_id),
  58. video_id, 'Downloading page')
  59. videos = re.findall(r'<video.*?poster="(?P<poster>[^"]+)".*?src="(?P<video>[^"]+)".*?></video>', webpage)
  60. iframe_link = self._html_search_regex(
  61. '<iframe[^>]+src=["\']([^"\']+)["\']', webpage, 'iframe link', default=None)
  62. if not videos and not iframe_link:
  63. raise ExtractorError('No media links available for %s' % video_id)
  64. title = self._og_search_title(webpage)
  65. TITLE_SUFFIX = ' - Первый по срочным новостям — LIFE | NEWS'
  66. if title.endswith(TITLE_SUFFIX):
  67. title = title[:-len(TITLE_SUFFIX)]
  68. description = self._og_search_description(webpage)
  69. view_count = self._html_search_regex(
  70. r'<div class=\'views\'>\s*(\d+)\s*</div>', webpage, 'view count', fatal=False)
  71. comment_count = self._html_search_regex(
  72. r'<div class=\'comments\'>\s*<span class=\'counter\'>\s*(\d+)\s*</span>', webpage, 'comment count', fatal=False)
  73. upload_date = self._html_search_regex(
  74. r'<time datetime=\'([^\']+)\'>', webpage, 'upload date', fatal=False)
  75. if upload_date is not None:
  76. upload_date = unified_strdate(upload_date)
  77. common_info = {
  78. 'description': description,
  79. 'view_count': int_or_none(view_count),
  80. 'comment_count': int_or_none(comment_count),
  81. 'upload_date': upload_date,
  82. }
  83. def make_entry(video_id, media, video_number=None):
  84. cur_info = dict(common_info)
  85. cur_info.update({
  86. 'id': video_id,
  87. 'url': media[1],
  88. 'thumbnail': media[0],
  89. 'title': title if video_number is None else '%s-video%s' % (title, video_number),
  90. })
  91. return cur_info
  92. if iframe_link:
  93. iframe_link = self._proto_relative_url(iframe_link, 'http:')
  94. cur_info = dict(common_info)
  95. cur_info.update({
  96. '_type': 'url_transparent',
  97. 'id': video_id,
  98. 'title': title,
  99. 'url': iframe_link,
  100. })
  101. return cur_info
  102. if len(videos) == 1:
  103. return make_entry(video_id, videos[0])
  104. else:
  105. return [make_entry(video_id, media, video_number + 1) for video_number, media in enumerate(videos)]